struts整合jfreechart

编程技术  /  houtizong 发布于 3年前   107

Struts2与JFreeChart的整合

1、添加相应的jar包(除struts2所必需的包以外的包):

  1. struts2-jfreechart-plugin-2.3.12.jar
  2. jcommon-1.0.17.jar
  3. jfreechart-1.0.14.jar

2、添加相应的Action

 

package cn.hpu.action;import java.awt.Font;import java.util.List;import org.jfree.chart.ChartFactory;import org.jfree.chart.JFreeChart;import org.jfree.chart.StandardChartTheme;import org.jfree.chart.plot.PlotOrientation;import org.jfree.data.category.CategoryDataset;import org.jfree.data.category.DefaultCategoryDataset;import org.jfree.data.general.DefaultPieDataset;import cn.hpu.po.Complain;import cn.hpu.po.DDInfo;import cn.hpu.service.ComplainService;import cn.hpu.service.DDInfoService;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class ComplainAction extends ActionSupport {private ComplainService complainService;private DDInfoService ddInfoService;private List<Complain> complains;private List<DDInfo> satisfications;// 生成图形private JFreeChart chart;/**获取用户满意度饼状报表 * @author qinrui * @see cn.hpu.service.ComplainService complainService * @see cn.hpu.service.DDInfoService ddInfoService * @return java.lang.String */public String pieChart() {DefaultPieDataset dpd = new DefaultPieDataset();complains = complainService.loadComplains();satisfications = ddInfoService.loadCustomerSatisfications();for(DDInfo satisfication : satisfications) {if(satisfication.getName() != null && !"".equals(satisfication.getName().trim())) {int count = 0;for(Complain complain : complains) {if(complain.getSatisfication().getId().equals(satisfication.getId())) {count ++;}}dpd.setValue(satisfication.getName(), count);}}StandardChartTheme standardChartTheme=new StandardChartTheme("CN");            //设置标题字体            standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));            //设置图例的字体            standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));            //设置轴向的字体            standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));            //应用主题样式            ChartFactory.setChartTheme(standardChartTheme);/**生成图形 * 1.title java.lang.String 标题 * 2.dataset org.jfree.data.general.DefaultPieDataset 数据源 * 3.legend java.lang.Boolean 图例 * 4.tooltips java.lang.Boolean 工具提示信息 * 5.locale java.lang.Boolean 本地化 */chart = ChartFactory.createPieChart3D("客户满意度", dpd, true, true, false);return SUCCESS;}/**生成柱状报表 * @author qinrui * @see cn.hpu.service.ComplainService complainService * @see cn.hpu.service.DDInfoService ddInfoService * @return java.lang.String */public String barChart() {DefaultCategoryDataset dcd = new DefaultCategoryDataset();complains = complainService.loadComplains();satisfications = ddInfoService.loadCustomerSatisfications();for(DDInfo satisfication : satisfications) {if(satisfication.getName() != null && !"".equals(satisfication.getName().trim())) {int count = 0;for(Complain complain : complains) {if(complain.getSatisfication().getId().equals(satisfication.getId())) {count ++;}}dcd.setValue(count , "", satisfication.getName());}}StandardChartTheme standardChartTheme=new StandardChartTheme("CN");            //设置标题字体            standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));            //设置图例的字体            standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));            //设置轴向的字体            standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));            //应用主题样式            ChartFactory.setChartTheme(standardChartTheme);/**生成图形 * 1.title java.lang.String 标题 * 2.dataset org.jfree.data.general.DefaultPieDataset 数据源 * 3.legend java.lang.Boolean 图例 * 4.tooltips java.lang.Boolean 工具提示信息 * 5.locale java.lang.Boolean 本地化 */chart = ChartFactory.createBarChart("客户满意度", "满意程度", "投诉数量", dcd, PlotOrientation.VERTICAL, true, true, false);return SUCCESS;}public ComplainService getComplainService() {return complainService;}public void setComplainService(ComplainService complainService) {this.complainService = complainService;}public List<Complain> getComplains() {return complains;}public List<DDInfo> getSatisfications() {return satisfications;}public void setSatisfications(List<DDInfo> satisfications) {this.satisfications = satisfications;}public DDInfoService getDdInfoService() {return ddInfoService;}public void setDdInfoService(DDInfoService ddInfoService) {this.ddInfoService = ddInfoService;}public JFreeChart getChart() {return chart;}public void setChart(JFreeChart chart) {this.chart = chart;}}

 3、配置struts.xml文件(注意要继承jfreechart-default,返回值type为chart)

 

 

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="authority" namespace="/" extends="jfreechart-default"><!-- 生成饼状报表 --><action name="Complain_pieChart" class="cn.hpu.action.ComplainAction" method="pieChart"><result type="chart"><param name="height">300</param><param name="width">400</param></result></action><!-- 生成柱状报表 --><action name="Complain_barChart" class="cn.hpu.action.ComplainAction" method="barChart"><result type="chart"><param name="height">300</param><param name="width">400</param></result></action></package></struts>

 

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客