jfreechart 线图 柱图 饼图 散点图
- 格式:docx
- 大小:27.15 KB
- 文档页数:25
public abstract class ChartAction extends BaseAction { public JFreeChart chart;
private String[] descriptionKeys; private String[] categoryKeys; private Double[][] datas;
private DefaultCategoryDataset cddata; private DefaultPieDataset dpdata; private XYDataset xydata; private IntervalXYDataset ixydata;
private String title; //标题 private String x; //横坐标名称 private String y; //纵坐标名称 private String y2; // 纵坐标名称2 /** * 线图 */ public void createLineChart() { //设置数据 cddata = new DefaultCategoryDataset(); for (int i=0; iif (datas[i].length==0) { cddata.setValue(null, "", categoryKeys[i]); } else { for (int j=0; jcddata.setValue(datas[i][j], descriptionKeys[j], categoryKeys[i]); } } } /** 数据 System.out.println("**************************datas begin: **************************"); for (int i=0; ifor (int j=0; jSystem.out.println(categoryKeys[i] + " : " + descriptionKeys[j] + " | " + datas[i][j]); } } System.out.println("**************************datas end: ****************************"); */ chart = ChartFactory.createLineChart(title, // 图表标题 x, // 目录轴的显示标签 y, // 数值轴的显示标签 cddata, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例(对于简单的柱状图必须是false) true, // 是否生成工具 false); // 是否生成URL链接 CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(new Color(238, 244, 255));//设置图表的颜色 plot.setDomainGridlinePaint(Color.lightGray);//设置垂直网格线的颜色 plot.setRangeGridlinePaint(Color.lightGray);//设置水平网格线的颜色 plot.setDomainGridlinesVisible(true); //设置垂直网格线是否显示 plot.setRangeGridlinesVisible(true); //设置水平网格线是否显示 LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)plot.getRenderer(); lineandshaperenderer.setBaseShapesVisible(true); lineandshaperenderer.setBaseShapesFilled(true); chart.setBackgroundPaint(Color.white); setChartFont(""); } /** * 饼图 */ public void createPieChart() { dpdata = new DefaultPieDataset(); //生成JFreeChart对象 Double d = 0.0; for (int i=0; ifor (int j=0; jd = d + (datas[j][i]==null?0:datas[j][i]); } dpdata.setValue(descriptionKeys[i], d.doubleValue()); } chart = ChartFactory.createPieChart(title, dpdata, true, true, true); PiePlot plot = (PiePlot)chart.getPlot(); plot.setBackgroundPaint(new Color(238, 244, 255));//设置图表的颜色 plot.setNoDataMessage("没有数据!"); plot.setLabelGenerator(new StandardPieSectionLabelGenerator( ("{0}: ({2})"), NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); chart.setBackgroundPaint(Color.white); setChartFont("PieChart"); }
/** * 饼图 */ public void createPieChartValue(Double[] value) { dpdata = new DefaultPieDataset(); //生成JFreeChart对象
for (int i=0; idpdata.setValue(descriptionKeys[i], value[i]); } chart = ChartFactory.createPieChart(title, dpdata, true, true, true); PiePlot plot = (PiePlot)chart.getPlot(); plot.setBackgroundPaint(new Color(238, 244, 255));//设置图表的颜色 plot.setNoDataMessage("没有数据!"); plot.setLabelGenerator(null); plot.setToolTipGenerator(new StandardPieToolTipGenerator(("{0}"), NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); LegendTitle legend = chart.getLegend(); legend.setBackgroundPaint(new Color(238, 244, 255));//图例背景 legend.setItemFont(new Font("宋体", Font.ITALIC, 12)); // 图例的字体 legend.setPosition(RectangleEdge.RIGHT);//图例位置 legend.setHeight(1000.00); chart.setBackgroundPaint(Color.white); setChartFont("PieChart"); } /** * 柱图 */ public void createBarChart() { //设置数据 cddata = new DefaultCategoryDataset(); for (int i=0; iif (datas[i].length==0) { cddata.setValue(null, "", categoryKeys[i]); } else { for (int j=0; jcddata.setValue(datas[i][j], descriptionKeys[j], categoryKeys[i]); } } }
chart = ChartFactory.createBarChart(title, // 图表标题 x, // 目录轴的显示标签 y, // 数值轴的显示标签 cddata, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例(对于简单的柱状图必须是false) true, // 是否生成工具 false // 是否生成URL链接 ); CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(new Color(238, 244, 255));//设置图表的颜色 chart.setBackgroundPaint(Color.white); setChartFont(""); } /** * 柱图 */ public void createBarChart3D(Object[] data) { //设置数据 cddata = new DefaultCategoryDataset(); for (int i=0; icddata.setValue((Number)data[i], categoryKeys[i], categoryKeys[i]); }
chart = ChartFactory.createBarChart3D(title, // 图表标题 x, // 目录轴的显示标签 y, // 数值轴的显示标签 cddata, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例(对于简单的柱状图必须是false) true, // 是否生成工具 false // 是否生成URL链接 ); CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(new Color(238, 244, 255));//设置图表的颜色 chart.setBackgroundPaint(Color.white);