关键词:jfreechart,web application
在工作中,我们常常碰到需要在网页上绘制统计图的例子。其实实现的办法有很多,我们可以亲自编写全部图形生成代码,比如用flash,或者用svg,这样做代码量很大,还容易出错,很烦的。也可以利用现成的java统计图表库。例如用jclass(http://java.quest.com/jclass/jclass.shtml),可是这玩艺却是要收费的。对于我们这种崇尚open source的程序员来说,有没有一个开源的统计图形库呢。有,它就是www.jfree.org推出的jfreechart包,它从柱形图,饼形图,到雷达图,蜡烛图等等无所不包,可以在c/s,b/s,甚至是实时环境都能一显身手。最关键的是它是open source!我在这里给出一个在网页上绘制饼形图的简单例子。
1。到www.jfree.org下载一个最新的java库,置jfreechart.jar和jcommon.jar.jar于classpath中
2。编写一个java bean,用来生成饼形图。(GSPieChart.java)
package gov.gwssi.tax.nspg.datadisposal;
import java.awt.Insets; import java.awt.Image; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Date; import java.util.ArrayList; import java.util.Iterator; import java.util.Locale; import java.text.NumberFormat; import javax.servlet.http.HttpSession; import org.jfree.data.*; import org.jfree.chart.*; import org.jfree.chart.axis.*; import org.jfree.chart.renderer.VerticalBarRenderer; import org.jfree.chart.renderer.StandardXYItemRenderer; import org.jfree.chart.plot.*; import org.jfree.chart.entity.*; import org.jfree.chart.tooltips.*; import org.jfree.chart.urls.*; import org.jfree.chart.servlet.*; import java.awt.Font;
public class GSPieChart { public static void main(String[] args) { Long[] testData = { new Long(10), new Long(20), new Long(30), new Long(40)}; generatePieChart(testData, , null, new PrintWriter(System.out), 580, 250); }
public static String generatePieChart(Long[] datas, String targetUrl, HttpSession session, PrintWriter pw, int w, int h) { String filename = null; try {
DefaultPieDataset data = new DefaultPieDataset(); data.setValue(, datas[0]); data.setValue(, datas[1]); data.setValue(, datas[2]); data.setValue(, datas[3]);
Pie3DPlot plot = new Pie3DPlot(data); plot.setInsets(new Insets(0, 5, 5, 5)); plot.setForegroundAlpha(0.6f); plot.setSectionLabelType(plot.NO_LABELS);
plot.setURLGenerator(new StandardPieURLGenerator(targetUrl, )); plot.setToolTipGenerator(new StandardPieToolTipGenerator()); JFreeChart chart = new JFreeChart(, JFreeChart.DEFAULT_TITLE_FONT, plot, true); chart.setTitle(new TextTitle(, new Font(, Font.BOLD, 15))); StandardLegend sl = (StandardLegend) chart.getLegend(); sl.setItemFont(new Font(, Font.TRUETYPE_FONT, 12));
chart.setBackgroundPaint(new java.awt.Color(254, 254, 141));
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); filename = ServletUtilities.saveChartAsPNG(chart, w, h, info, session);
ChartUtilities.writeImageMap(pw, filename, info); pw.flush();
} catch (Exception e) { System.out.println( + e.toString()); e.printStackTrace(System.out); filename = ; } return filename; } }
3。在一个jsp文件中使用这个javabean来生成图像,以下是代码片断 .........java代码.................
<%
String targetUrl=+sid; Long[] datas={normals,new Long(totalAbnormal),dataLacks,notProcesses}; String filename=GSPieChart.generatePieChart(datas,targetUrl,session,new PrintWriter(out),580,250); String graphURL = request.getContextPath() + + filename;
%> .......html代码................
<!-- pie chart --> <table width= border= cellspacing= cellpadding=> <tr> <td> <img src= width=580 height=250 border=0 usemap=> </td> </tr> </table>
4。在web.xml中配置jfreechart的图像生成的servlet
<servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet>
<servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/servlet/DisplayChart</url-pattern> </servlet-mapping>
以上就是在b/s中使用jfreechart的大体步骤,在你下载的jfreechart的sample文件夹中找到jfreechart-sample.war文件中也有类似的例子,只不过复杂一些。
最后,希望这篇文章能对你有所启发。如有错误之处,欢迎批评指正。 |