public abstract class AbstractStatistic implements Statistic {
/**
* 建立統計圖形
*
* @param columnKeys
* 關鍵字數組
* @param data
* 資料數組
* @param title
* 標題
* @param session
* @param rowKeys
* 行關鍵字
* @param showType
* @return
*/
public String createStatisticImg(String[] columnKeys, double[][] data,
String title, HttpSession session, String[] rowKeys, String showType) {
try {
JFreeChart chart = null;
if(showType.equals("1")){
chart = createBarChart3D(data,columnKeys,rowKeys,title);
}
if(showType.equals("2")){
chart = createLineChart(data,columnKeys,rowKeys,title);
}
if(showType.equals("3")){
chart = createAreaChart(data,columnKeys,rowKeys,title);
}
if(showType.equals("4")){
chart = createLineChart3D(data, columnKeys, rowKeys, title);
}
String filename = ServletUtilities.saveChartAsPNG(chart, 800, 450,
null, session);
return filename;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
public JFreeChart createBarChart3D(double[][] data, String[] columnKeys, String[] rowKeys,String title){
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D(title,// "分布與對比圖"
"", null, dataset, PlotOrientation.VERTICAL, true, false,
false);
chart.setBackgroundPaint(Color.WHITE);// 設定整張圖片背景色
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.gray);// 設定繪圖區域背景色
// 設定是否顯示垂直網格線
plot.setDomainGridlinesVisible(true);
// 設定是否顯示水平網格線
plot.setRangeGridlinesVisible(true);
CategoryAxis domainAxis = plot.getDomainAxis();
/*------設定X軸標題的傾斜程度----*/
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
domainAxis.setMaximumCategoryLabelLines(20);
domainAxis.setTickLabelFont(new Font("黑體", Font.PLAIN, 12));
plot.setDomainAxis(domainAxis);
ValueAxis rangeAxis = plot.getRangeAxis();
// 設定最高的一個 Item 與圖片頂端的距離
rangeAxis.setUpperMargin(0.15);
// 設定最低的一個 Item 與圖片底端的距離
rangeAxis.setLowerMargin(0.15);
plot.setRangeAxis(rangeAxis);
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.BLACK);
// 設定 Wall 的顏色<BR>
renderer.setWallPaint(Color.gray);
// 設定每種柱的顏色
renderer.setSeriesPaint(0, new Color(255, 0, 0));
renderer.setSeriesPaint(1, new Color(0, 0, 255));
renderer.setSeriesPaint(2, new Color(0, 255, 0));
// 置每種柱的 Outline 顏色
renderer.setSeriesOutlinePaint(0, Color.BLACK);
renderer.setSeriesOutlinePaint(1, Color.BLACK);
renderer.setSeriesOutlinePaint(2, Color.BLACK);
// 設定每個地區所包含的平行柱的之間距離
renderer.setItemMargin(0.1);
// 顯示每個柱的數值,並修改該數值的字型屬性
// renderer.setSeriesItemLabelGenerator(0,new
// StandardCategoryItemLabelGenerator("##%",java.text.NumberFormat.getPercentInstance()));
renderer
.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelFont(new Font("黑體", Font.PLAIN, 12));
renderer.setItemLabelsVisible(true);
// 設定標籤顯示位置,不加這句將顯示在柱體內
renderer.setPositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));
plot.setRenderer(renderer);
// 設定柱的透明度<BR>
plot.setForegroundAlpha(0.6f);
// 設定地區、銷量的顯示位置<BR>
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
return chart;
}
public JFreeChart createLineChart(double[][] data, String[] columnKeys, String[] rowKeys,String title){
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createLineChart(title,// "地區分布與對比圖"
"", null, dataset, PlotOrientation.VERTICAL, true, false,
false);
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
/*------設定X軸標題的傾斜程度----*/
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
domainAxis.setTickLabelFont(new Font("黑體", Font.PLAIN, 12));
return chart;
}
public JFreeChart createLineChart3D(double[][] data, String[] columnKeys, String[] rowKeys,String title){
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createLineChart3D(title,// "地區分布與對比圖"
"", null, dataset, PlotOrientation.VERTICAL, true, false,
false);
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
/*------設定X軸標題的傾斜程度----*/
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
domainAxis.setTickLabelFont(new Font("黑體", Font.PLAIN, 12));
return chart;
}
public JFreeChart createAreaChart(double[][] data, String[] columnKeys, String[] rowKeys,String title){
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createAreaChart(title, "統計時間", "統計數量", dataset, PlotOrientation.VERTICAL, true, true, false);
chart.setBackgroundPaint(Color.white);
CategoryPlot categoryplot = (CategoryPlot)chart.getPlot();
categoryplot.setForegroundAlpha(0.5F);
categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setDomainGridlinePaint(Color.white);
categoryplot.setRangeGridlinesVisible(true);
categoryplot.setRangeGridlinePaint(Color.white);
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryaxis.setLowerMargin(0.0D);
categoryaxis.setUpperMargin(0.0D);
categoryaxis.addCategoryLabelToolTip("Type 1", "The first type.");
categoryaxis.addCategoryLabelToolTip("Type 2", "The second type.");
categoryaxis.addCategoryLabelToolTip("Type 3", "The third type.");
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setLabelAngle(0.0D);
return chart;
}