使用JFreeChart必须的jar:JFreeChart.jar ,JCommon.jar
JFreeChart的使用步骤:
(1)创建JFreeChart的数据集對象
(2)通过JFreeChartFacatory對象创建JFreeChart對象的
(3)設置JFreeChart的各种参数信息
(4)将JFreeChart以图表形式保存或者以流的信息輸出到页面
主要使用:ServletUtilities,ChartUtilities對象的保存图片或輸出流到页面。
在Swing开发中使用JFreeJFrame组件集成自Swing的JFrame的组件。
在Web输入页面使用ServletUtilities如下:
ServletUtilities.saveChartAsPNG(chartB, 500, 300, null, session);
实例:
public JFreeChart CreatePie3D(String chartName,DefaultPieDataset dataset){
//**************收到数据集*******************************
//******************如查数据集不为空则开始显示显示统计图************************
//取出标题信息,数据对象第一维第一个为标题、第二个为x轴名称、第三个Y轴
//String title=temp[0][0];//标题
//String xname=temp[0][1];//X坐标名称
//String yname=temp[0][2];//Y坐标名称
//***************添加数据集(1、类型 2、值 3、区域)*************************************************
// DefaultPieDataset datasetB = new DefaultPieDataset();//创建饼图数据集对象
//
// datasetB.setValue("A",25);//(如:"选择A",300)
// datasetB.setValue("B",25);
// datasetB.setValue("C",25);
// datasetB.setValue("D",25);
// datasetB.setValue("E",3);
JFreeChart chartB = ChartFactory.createPieChart3D(chartName,dataset,true,false,false);
//chart.setBackgroundPaint(java.awt.Color.white);//可选,设置图片背景色
chartB.setBackgroundPaint(Color.WHITE);//设置背景颜色为白色
//chartB.setTitle("3D饼图 by zjun");//可选,设置图片标题
chartB.setTextAntiAlias(true);//设置标题平滑效果
//chartB.setBorderVisible(true);//设置图片外边框显示
chartB.setAntiAlias(false);//设置内边框平滑效果
PiePlot plotB = (PiePlot) chartB.getPlot();
//设置饼图标签显示项,参数1为显示内容("{1}"为文本,"{2}"为数值,"{3}"为百分比),参数2为显示值的格式,参数3为百分比格式
plotB.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}",new DecimalFormat("0"), new DecimalFormat("0.0%")));
//设置饼图参照表标签显示项,参数1为显示内容("{1}"为文本,"{2}"为数值,"{3}"为百分比),参数2为显示值的格式,参数3为百分比格式
plotB.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}",new DecimalFormat("0"), new DecimalFormat("0.0%")));
//设置饼图大小,注意参数1小于参数2
//plotB.setExplodePercent(1,1.9);
//设置标签颜色
plotB.setLabelPaint(new Color(205, 000, 105));
//设置标签阴影颜色
//plotB.setLabelShadowPaint(new Color(255, 100, 255));
//设置标签外边框线颜色
plotB.setLabelOutlinePaint( Color.green);
//设置标签字体
//plotB.setLabelFont(new Font("SansSerif",Font.BOLD,22));
//设置是否显示标签线
//plotB.setLabelLinksVisible(false);
//设置值为null时是否显示该项
//plotB.setIgnoreNullValues(true);
//设置值为0时是否显示该项
//plotB.setIgnoreZeroValues(true);
//设置第一个 section 的开始位置,默认是12点钟方向
//plotB.setStartAngle(0);
//指定 section 轮廓线的厚度
//plotB.setSectionOutlineStroke(new java.awt.BasicStroke(1));
//指定 section 的色彩
plotB.setSectionPaint(0,new Color(205, 100, 205));
//指定显示的饼图上圆形还椭圆形
//plotB.setCircular(true);
//指定 section 按逆时针方向依次显示,默认是顺时针方向
//plotB.setDirection(Rotation.ANTICLOCKWISE);
//设置柱的透明度
//plotB.setForegroundAlpha(0.8f);
//输出图形
// String filename = ServletUtilities.saveChartAsPNG(chartB, 500, 300, null, session);
// String graphURL = request.getContextPath() + "/DisplayChart.do?filename=" + filename;
return chartB;
}