1、jfreechart 线图 柱图 饼图 散点图 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 Interval
2、XYDataset ixydata; private String title; /标题 private String x; /横坐标名称 private String y; /纵坐标名称 private String y2; / 纵坐标名称2 /* * 线图 */ public void createLineChart() /设置数据 cddata = new DefaultCategoryDataset(); for (int i=0; idatas.length; i+) if (datasi.length=0) cddata.setValue(null, , categoryKeysi
3、); else for (int j=0; jdatasi.length; j+) cddata.setValue(datasij, descriptionKeysj, categoryKeysi); /* 数据 System.out.println(*datas begin: *); for (int i=0; idatas.length; i+) for (int j=0; jdatasi.length; j+) System.out.println(categoryKeysi + : + descriptionKeysj + | + datasij); System.out.printl
4、n(*datas end: *); */ chart = ChartFactory.createLineChart(title, / 图表标题 x, / 目录轴的显示标签 y, / 数值轴的显示标签 cddata, / 数据集 PlotOrientation.VERTICAL, / 图表方向:水平、垂直 true, / 是否显示图例(对于简单的柱状图必须是false) true, / 是否生成工具 false); / 是否生成URL链接 CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(new Color(
5、238, 244, 255);/设置图表的颜色 plot.setDomainGridlinePaint(Color.lightGray);/设置垂直网格线的颜色 plot.setRangeGridlinePaint(Color.lightGray);/设置水平网格线的颜色 plot.setDomainGridlinesVisible(true); /设置垂直网格线是否显示 plot.setRangeGridlinesVisible(true); /设置水平网格线是否显示 LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRende
6、rer)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; ides
7、criptionKeys.length; i+) for (int j=0; jdatas.length; j+) d = d + (datasji=null?0:datasji); dpdata.setValue(descriptionKeysi, d.doubleValue(); chart = ChartFactory.createPieChart(title, dpdata, true, true, true); PiePlot plot = (PiePlot)chart.getPlot(); plot.setBackgroundPaint(new Color(238, 244, 25
8、5);/设置图表的颜色 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 =
9、 new DefaultPieDataset(); /生成JFreeChart对象 for (int i=0; idescriptionKeys.length; i+) dpdata.setValue(descriptionKeysi, valuei); chart = ChartFactory.createPieChart(title, dpdata, true, true, true); PiePlot plot = (PiePlot)chart.getPlot(); plot.setBackgroundPaint(new Color(238, 244, 255);/设置图表的颜色 plo
10、t.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 Fo
11、nt(宋体, 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; idatas.length; i+) if (datasi.le
12、ngth=0) cddata.setValue(null, , categoryKeysi); else for (int j=0; jdatasi.length; j+) cddata.setValue(datasij, descriptionKeysj, categoryKeysi); chart = ChartFactory.createBarChart(title, / 图表标题 x, / 目录轴的显示标签 y, / 数值轴的显示标签 cddata, / 数据集 PlotOrientation.VERTICAL, / 图表方向:水平、垂直 true, / 是否显示图例(对于简单的柱状图
13、必须是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()
14、; for (int i=0; idata.length; i+) cddata.setValue(Number)datai, categoryKeysi, categoryKeysi); chart = ChartFactory.createBarChart3D(title, / 图表标题 x, / 目录轴的显示标签 y, / 数值轴的显示标签 cddata, / 数据集 PlotOrientation.VERTICAL, / 图表方向:水平、垂直 true, / 是否显示图例(对于简单的柱状图必须是false) true, / 是否生成工具 false / 是否生成URL链接 ); Cat
15、egoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(new Color(238, 244, 255);/设置图表的颜色 chart.setBackgroundPaint(Color.white); setChartFont(); /* * 对比柱状图 */ public void createBarChart(CategoryDataset dataset) chart = ChartFactory.createBarChart(title, / 图表标题 x, / 目录轴的显示标签 y, / 数值轴的显示标签 d
16、ataset, / 数据集 PlotOrientation.VERTICAL, / 图表方向:水平、垂直 true, / 是否显示图例(对于简单的柱状图必须是false) true, / 是否生成工具 false / 是否生成URL链接 ); CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(new Color(238, 244, 255);/设置图表的颜色 chart.setBackgroundPaint(Color.white); setChartFont(); /* * 面积图 */ public v
17、oid createAreaChart() /设置数据 cddata = new DefaultCategoryDataset(); cddata = new DefaultCategoryDataset(); for (int i=0; idatas.length; i+) if (datasi.length=0) cddata.setValue(null, , categoryKeysi); else for (int j=0; jdatasi.length; j+) cddata.setValue(datasij, descriptionKeysj, categoryKeysi); ch
18、art = ChartFactory.createAreaChart(title, / 图表标题 x, / 目录轴的显示标签 y, / 数值轴的显示标签 cddata, / 数据集 PlotOrientation.VERTICAL, / 图表方向:水平、垂直 true, / 是否显示图例(对于简单的柱状图必须是false) true, / 是否生成工具 false / 是否生成URL链接 ); CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(new Color(238, 244, 255);/设置图表的颜
19、色 chart.setBackgroundPaint(Color.white); setChartFont(); /* * X,Y 线图chart * param chartMap */ public void createXYLineChart(MapString, Map chartMap) /创建XYDataset对象(准备数据) xydata = createXYDataset(chartMap); / 创建JFreeChart对象:ChartFactory.createXYLineChart chart = ChartFactory.createXYLineChart(title,
20、/ 标题 x, / categoryAxisLabel (category轴,横轴,X轴标签) y, / valueAxisLabel(value轴,纵轴,Y轴的标签) xydata, / dataset PlotOrientation.VERTICAL, true, / legend true, / tooltips true); / URLs / 使用CategoryPlot设置各种参数。以下设置可以省略。 XYPlot plot = (XYPlot) chart.getPlot(); /x轴整数显示 NumberAxis na= (NumberAxis)plot.getDomainAxi
21、s(); na.setStandardTickUnits(NumberAxis.createIntegerTickUnits(); XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer(); renderer.setBaseItemLabelsVisible(true); renderer.setBaseShapesVisible(true); renderer.setBaseShapesFilled(true); plot.setRenderer(0, renderer); plot.setBa
22、ckgroundPaint(new Color(238, 244, 255);/设置图表的颜色 plot.setDomainGridlinePaint(Color.lightGray);/设置垂直网格线的颜色 plot.setRangeGridlinePaint(Color.lightGray);/设置水平网格线的颜色 plot.setDomainGridlinesVisible(true); /设置垂直网格线是否显示 plot.setRangeGridlinesVisible(true); /设置水平网格线是否显示 chart.setBackgroundPaint(Color.white);
23、 setChartFont(XYChart); /* * 双y轴 曲线图,没有折线点 * param cddata */ public void createXYsLineChart(DefaultCategoryDataset cddataLeft,DefaultCategoryDataset cddataRight) / 设置字体样式 Font fs = new Font(微软雅黑, Font.BOLD, 12); Font f = new Font(微软雅黑, Font.PLAIN, 12); / 创建JFreeChart对象:ChartFactory.createXYLineChart
24、 chart = ChartFactory.createLineChart(title, / 标题 x, / categoryAxisLabel (category轴,横轴,X轴标签) y, / valueAxisLabel(value轴,纵轴,Y轴的标签) cddataLeft, / dataset PlotOrientation.VERTICAL, true, / legend true, / tooltips true); / URLs / 使用CategoryPlot设置各种参数。以下设置可以省略。 CategoryPlot plot = (CategoryPlot) chart.ge
25、tPlot(); /设置X坐标的显示方式(默认水平) CategoryAxis categoryaxis = plot.getDomainAxis(); categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90); categoryaxis.setCategoryMargin(9);/ 分类轴边距 / 左边y轴显示方式 /NumberAxis numberaxis= (NumberAxis)plotxy.getRangeAxis();/y轴整数显示 NumberAxis numberaxis = new Number
26、Axis(this.y); numberaxis.setLabelFont(fs); numberaxis.setTickLabelFont(f); plot.setRangeAxis(0, numberaxis); / 右边y轴显示方式 NumberAxis numberaxis2 = new NumberAxis(this.y2); numberaxis2.setLabelFont(fs); numberaxis2.setTickLabelFont(f); plot.setRangeAxis(1, numberaxis2); plot.setDataset(1, cddataRight); plot.mapDatasetToRangeAxis(1, 1); /y轴(左边) LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); renderer.setBaseItemLabelsVisible(true); plot.setRenderer(0, renderer); renderer.setSeriesStroke(0, new BasicStroke(1.6F); /y轴(右边),绘制单元对象 LineAndShapeRenderer
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2