<%
//设置数据集
DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue(\初中高级程序员\dataset.setValue(\项目经理\dataset.setValue(\系统分析师\dataset.setValue(\软件架构师\dataset.setValue(\其他\
//通过工厂类生成JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart3D(\行业职业分布图\true, false, false);
PiePlot pieplot = (PiePlot) chart.getPlot(); pieplot.setLabelFont(new Font(\宋体\
//没有数据的时候显示的内容
pieplot.setNoDataMessage(\无数据显示\pieplot.setCircular(false); pieplot.setLabelGap(0.02D);
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
String graphURL = request.getContextPath() + \e=\%>
\
可以看出,饼图的绘制与柱状图的绘制类似,该例的运行效果如下:
有时候我们还想知道某块所占的具体分值,或者需要突出显示某一块。这时候需要对上例进行部分修改:dataset.setValue(\其他\, 0.2);后的那段改成:
//通过工厂类生成JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart3D(\行业职业分布图\true, false, false);
PiePlot pieplot = (PiePlot) chart.getPlot(); pieplot.setLabelFont(new Font(\宋体\//没有数据的时候显示的内容
pieplot.setNoDataMessage(\无数据显示\pieplot.setCircular(false);
setExplodePercent方法很重要,它将Label为某名称的某块挖出来突出显示,而后两句实现的效果是在“初中高级程序员”等名称后加上百分比,改成“初中高级程序员=55%”等。加上如上的代码后,同时还需要将相关的两个java包:org.jfree.chart.labels.StandardPieSectionLabelGenerator和java.text.NumberFormat引入到该jsp页面
中。此时的运行结果如下:
通过JFreeChart还可以提供漂亮的水晶饼图效果,接着让我们新建一个sample3.jsp页面来体验一下超炫美图吧。修改sample3.jsp页面如下:
<%@ page contentType=\
<%@ page import=\org.jfree.chart.servlet.ServletUtilities, org.jfree.util.Rotation, org.jfree.data.general.DefaultPieDataset, org.jfree.chart.plot.PiePlot3D\ <%
//设置数据集
DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue(\初中高级程序员\dataset.setValue(\项目经理\dataset.setValue(\系统分析师\dataset.setValue(\软件架构师\dataset.setValue(\其他\
//通过工厂类生成JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart3D(\行业职业分布图\true, true, false);
//获得3D的水晶饼图对象
PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot(); //设置开始角度
pieplot3d.setStartAngle(150D); //设置方向为”顺时针方向“
pieplot3d.setDirection(Rotation.CLOCKWISE); //设置透明度,0.5F为半透明,1为不透明,0为全透明 pieplot3d.setForegroundAlpha(0.5F);
pieplot3d.setNoDataMessage(\无数据显示\
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); String graphURL = request.getContextPath() + \%>
利用setForegroundAlpha()方法可以设置3D饼图的透明度,利用setStartAngle()可以设置其开始角度,利用setDirection()方法可以设置其方向。该例的运行效果如下图所示:
四.曲线图
接着我们来介绍曲线图的使用。在笔者日常的开发工作中,曲线图用得最多,它可以用来绘制趋势图、统计分析等。首先我们在WebRoot下建立一个line目录,用来存放曲线图实例的jsp页面。我们在该目录下建立sample1.jsp页面来做一个简单的曲线图的例子。在开始编码前,让我们来看看与画曲线图密切相关的几个类:
1) TimeSeriesCollection
曲线数据的集合。
2) TimeSeries
曲线信息序列。
3) ChartFactory
可以利用该类的createTimeSeriesChart方法来创建曲线的JFreeChart对象。
在下例中,我们显示阿蜜果的blog在2007年度各月份的访问量情况,修改后的sample1.jsp的内容如下: