最近一直在做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;// 返回生成圖片的文件名
}
貼圖:
謝謝參觀!