1、jfreechart是目前最适合java生成统计图的工具,包括饼状图,折线图。柱状图等等,使用jfreechart需要两个jar包,我这个应该是最新的刘,分别是jfreechart-1.0.9.jar和jcommon-1.0.21.jar,
2、使用jfreechart,在action中private JFreeChart chart,生成get,set方法,注意这个名字一定要是chart,在struts.xml中做如下配置
<package name="jfreechart" namespace="/" extends="struts-default">
<result-types>
<result-type name="chart" class="com.common.ChartResult"></result-type>
</result-types>
<action name="chart_*" class="chartAction" method="{1}" >
<result type="chart">
<param name="width">600</param>
<param name="height">300</param>
<param name="imageType">jpg</param>
</result><!--
<result name="sd">/login.jsp</result>
-->
</action>
</package>
这里要声明一下action的result类型chart,这是一个工具类com.common.ChartResult,稍后我会上传上去
还需要一个chartUtil类,他的作用是讲action传入的数据进行遍历解析生成统计图,统计图的具体样式可以参考jfreechart的官方文档,网上也有中文的文档,
比如这里我要生成一个饼状图,先action得到数据,传入chartUtil类中
this.collects=this.collectService.findAllCollects("1");
chart=ChartUtil.createLineChart("练习",collects,"X", "Y");
return SUCCESS;
下面我给出chartUtil类的代码,里面有注释,很容易明白
/**
* 创建饼状图
* @param title 标题
* @param map 键值对
* @return
*/
public static JFreeChart createPieChart(String title,List<CrmCollect> c){
DefaultPieDataset dpd=createPieDataset(c);
// 创建PieChart对象(标题,数据集,是否显示图例,是否生成工具提示,是否生成URL链接)
JFreeChart chart = ChartFactory.createPieChart3D(title, dpd,
true, true, false);
setPieStyle(chart);
return chart;
}
/**
* 创建饼状图数据
* @param map
* @return
*/
private static DefaultPieDataset createPieDataset(List<CrmCollect> c){
DefaultPieDataset dpd = new DefaultPieDataset();
for(CrmCollect g:c){
dpd.setValue(g.getId(), g.getItemAmounts());
}
return dpd;
}
/**
* 设置饼状图样式
* @param chart
*/
private static void setPieStyle(JFreeChart chart){
PiePlot plot = (PiePlot) chart.getPlot();
Font font = new Font("宋体", Font.ITALIC, 12);
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 22));
//方块距离饼的距离 只要负值就能把数据放到饼里
plot.setLabelFont(font);
//去掉label
//plot.setLabelGenerator(null);
//在label上显示百分比
//plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}"));
//如果百分比要显示一位小数
//plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}",new DecimalFormat("0.0"),new DecimalFormat("0.0%")));
//在label上显示实际数值
//plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}"));
//是否显示线 fasle就不显示了
//plot.setLabelLinkMargin(-0.8);
//plot.setLabelLinksVisible(false);
//有时我们想突出某一个来显示,需要加入下面一句:第一个参数是key,第二个参数是突出显示的大小(可以自己调整一下看看效果就明白了)
//plot.setExplodePercent("城管强拆",0.23);
//设置相应的数据显示效果
//StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator("{0}:({1},{2})", new DecimalFormat("0.0"), new DecimalFormat("0.0"));
StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator("{0}:({1},{2})", new DecimalFormat("0.0"), new DecimalFormat("0.0%"));
plot.setLabelGenerator(standarPieIG);
//没有数据的时候所显示的内容
plot.setNoDataMessage("没有相应的数据显示");
chart.getLegend().setItemFont(font);
}
其他的折线图柱状图参考这个模式进行就可以了
3、如果没有看到第三步的同学可能会有点悲剧,因为在web.xml中还需要配置一下,向web.xml中复制如下的代码
<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>/DisplayChart</url-pattern>
</servlet-mapping>
4、页面显示统计图是很简单的,新建一个img标签,src="生成统计图的action地址"
<img title="jfreechart" id="sss" src="<%=path%>/chart_showChart1" style="display: block;" ></img></br>
5、jar包下载地址
http://download.csdn.net/detail/qw463800202/8915247