最近一直在做Jfreechart的问题,今天终于有点进展,可以松一口气了。最主要的原因是数据集的生成没有做好。导致图形的失败。Jfreechart的图形类还是很强大的。这次引用了jfreechart developer guide source code,也请了其他的人帮忙。谢谢大家了。这里把堆积图和混合图的生成方法贴出来。供以后参考。
public CategoryDataset createStackedBarDateSet(String strTtimePointZone,
String[][] arr) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();// 定义一个柱图的数据集合。
int rowindex = arr[0].length;
int columindex = arr.length;
String ty = "";
String tm = "";
String[] rowKeys = new String[rowindex-1];//定义每个柱子,指标名称
for (int i = 1; i < rowindex; i++) {
rowKeys[i - 1] = arr[0][i];//指标名称
}
String[] columnKeys = new String[columindex-3];//定义统计时间
double[][] data = new double[rowindex-1][columindex-3];
for (int j = 3; j < columindex; j++) {
for (int i = 1; i < rowindex; i++) {
ty = arr[j][0].substring(2, 4);
tm = arr[j][0].substring(4, 6);
columnKeys[j-3] = ty + "/" + tm;//指标时间
if (!arr[j][i].equals("--")) {
data[i - 1][j-3] = Double.parseDouble(arr[j][i]);
}
}
}
for(int n=0;n<data.length;n++){
for(int m = 0;m<data[0].length;m++){
dataset.addValue(data[n][m], rowKeys[n], columnKeys[m]);
}
}
return dataset;
}
图形生成方法:
public String getStackedBarChart(HttpSession session,
String strTtimePointZone, String[][] arr, PrintWriter pw) {
// 1:得到 CategoryDataset
// 2:JFreeChart对象
CategoryDataset dataset = this.createStackedBarDateSet(
strTtimePointZone, arr);// 数据集
JFreeChart chart = ChartFactory.createStackedBarChart("堆积柱图", // 图表标题
"时间", // x-axis label
"值", // y-axis label
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
// 设置JFreeChart的显示属性,对图形外部部分进行调整
chart.setBackgroundPaint(Color.white);// 设置图背景色
TextTitle title = new TextTitle("指标堆积图", font);// 设置字体大小,形状
chart.setTitle(title);
// 图例字体清晰
chart.setTextAntiAlias(true);
CategoryPlot plot = chart.getCategoryPlot();//画布属性
plot.setNoDataMessage("NO Data!");
plot.setRangeGridlinesVisible(true);// 设置横虚线可见
plot.setRangeGridlinePaint(Color.gray);// 虚线色彩
plot.setBackgroundPaint(Color.WHITE);// 设置网格背景色
plot.setDomainGridlinePaint(Color.DARK_GRAY);// 设置网格Y(Domain轴)颜色
plot.setRangeGridlinePaint(Color.DARK_GRAY);// 设置网格X横线颜色
plot.setRangeGridlineStroke(new BasicStroke(0.2f)); // 数据X轴网格线条笔触
plot.setDomainGridlineStroke(new BasicStroke(0.1f)); // 数据Y轴网格线条笔触
plot.setAxisOffset(new RectangleInsets(1.0, 1.0, 1.0, 1.0));// 设置曲线图与xy轴的距离
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
NumberAxis vn = (NumberAxis) plot.getRangeAxis();// 数据轴精度
vn.setAutoRangeIncludesZero(true);// 设置数据轴坐标从0开始
vn.setLabelFont(fontAxis);// 轴标题
DecimalFormat df = new DecimalFormat("");// 数据显示格式是百分比
vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式
// DomainAxis (区域轴,相当于 x 轴), RangeAxis (范围轴,相当于 y 轴)
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(fontAxis);// 轴标题
domainAxis.setTickLabelFont(fontAxis);// 轴数值
domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);// 横轴上的 Lable 是否完整显示
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 7.0));
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);//X轴显示位置为90度
plot.setDomainAxis(domainAxis);
ValueAxis rangeAxis = plot.getRangeAxis();// y轴设置
rangeAxis.setLabelFont(fontText);
rangeAxis.setTickLabelFont(fontText);
rangeAxis.setUpperMargin(0.15);// 设置最高的一个 Item 与图片顶端的距离
rangeAxis.setLowerMargin(0.15);// 设置最低的一个 Item 与图片底端的距离
plot.setRangeAxis(rangeAxis);
// Renderer 对象是图形的绘制单元
// StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
StackedBarRenderer3D renderer = new StackedBarRenderer3D();//just test
renderer.setItemLabelFont(fontText);
renderer.setMaximumBarWidth(0.05);// 设置柱子宽度
renderer.setMinimumBarLength(0.1);// 设置柱子高度
renderer.setBaseOutlinePaint(Color.BLACK);// 设置柱的边框颜色
renderer.setDrawBarOutline(true);// 设置柱的边框可见
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemMargin(0.05);// 设置每个地区所包含的平行柱的之间距离
plot.setRenderer(renderer);
// 取得统计图表的第一个图例
LegendTitle legend = chart.getLegend(0);
//设置图例的字体
legend.setItemFont(fontText);
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
try {
fileName = ServletUtilities.saveChartAsPNG(chart, 720, 540, info,
session);// 生成图片,放到服务器的临时文件夹下
// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, fileName, info, false);
} catch (IOException e) {
e.printStackTrace();
}
pw.flush();
return fileName;
}
public String getBarChart(HttpSession session, String strTimePointZone,
String[][] arr, PrintWriter pw) {
CategoryDataset categorydataset1 = this.createBarDataSet(strTimePointZone, arr);//柱线数据集
CategoryDataset categorydataset = this.createBarDataSet(strTimePointZone, arr);//折线数据集(相同方法)
JFreeChart chart = ChartFactory.createBarChart("指标混合图",
"时间",
"值",
categorydataset1,
PlotOrientation.VERTICAL,
true,
false,
false);
chart.setBackgroundPaint(Color.white);// 设置曲线图背景色
TextTitle title = new TextTitle("指标混合图", font);// 设置字体大小,形状
chart.setTitle(title);
chart.setTextAntiAlias(true);
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
categoryplot.setNoDataMessage("NO Data!");
categoryplot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
//映射折线数据集
categoryplot.setDataset(1, categorydataset);
categoryplot.mapDatasetToRangeAxis(1, 1);
categoryplot.setBackgroundPaint(Color.white);
categoryplot.setRangeGridlinesVisible(true);// 设置横虚线可见
categoryplot.setRangeGridlinePaint(Color.gray);// 虚线色彩
categoryplot.setDomainGridlinePaint(Color.DARK_GRAY);// 设置网格Y(Domain轴)颜色
categoryplot.setRangeGridlinePaint(Color.DARK_GRAY);// 设置网格X横线颜色
categoryplot.setRangeGridlineStroke(new BasicStroke(0.2f)); // 数据X轴网格线条笔触
categoryplot.setDomainGridlineStroke(new BasicStroke(0.1f)); // 数据Y轴网格线条笔触
categoryplot.setAxisOffset(new RectangleInsets(1.0, 1.0, 1.0, 1.0));// 设置图与xy轴的距离
categoryplot.setDomainCrosshairVisible(true);
categoryplot.setRangeCrosshairVisible(true);
CategoryAxis categoryaxis = categoryplot.getDomainAxis();//X轴
categoryaxis.setLabelFont(fontText);
categoryaxis.setTickLabelFont(fontText);
// categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 7.0));
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);//X轴显示位置为90度
NumberAxis left = new NumberAxis("值");//Y轴
NumberAxis numberaxis = new NumberAxis("比率");//Y轴
numberaxis.setLabelFont(fontText);
left.setLabelFont(fontText);
categoryplot.setRangeAxis(0, left);//左侧Y轴
categoryplot.setRangeAxis(1, numberaxis);//右侧Y轴
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.BLACK);
// 设置每个地区所包含的平行柱之间的距离renderer.setItemMargin(0.1);
renderer.setMaximumBarWidth(0.05);// 设置柱子宽度
renderer.setMinimumBarLength(0.1);// 设置柱子高度
renderer.setBaseOutlinePaint(Color.BLACK);// 设置柱的边框颜色
renderer.setDrawBarOutline(true);// 设置柱的边框可见
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemMargin(0.05);
LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();//折线对象
lineandshaperenderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
lineandshaperenderer.setSeriesPaint(0, new Color(0, 0, 255));
categoryplot.setRenderer(0,renderer);
categoryplot.setRenderer(1, lineandshaperenderer);
categoryplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
//图例各个属性
LegendTitle legendtitle = chart.getLegend(0);
legendtitle.setMargin(new RectangleInsets(2D, 2D, 2D, 2D));
legendtitle.setBorder(new BlockBorder());
legendtitle.setItemFont(fontText);
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
try {
fileName = ServletUtilities.saveChartAsPNG(chart, 720, 540, info,
session);// 生成图片
ChartUtilities.writeImageMap(pw, fileName, info, false);
} catch (IOException e) {
e.printStackTrace();
}
pw.flush();
return fileName;// 返回生成图片的文件名
}
贴图: