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 的颜色
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);
// 设置柱的透明度
plot.setForegroundAlpha(0.6f);
// 设置地区、销量的显示位置
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;
}