springmvc集成jfreechart

JSP文件
 
  <div id="tab_barChart" title="直方图" icon="icon-tip"style="padding:0;display:block;">
       <img id="chart_BAR" src="<c:urlvalue="/system/webanalyticsBAR.chart"/>">
   </div>


servlet.xml
   <bean id="chartViewResolver"class="com.comm.ChartViewResolver">
       <property name="chartSuffix"value=".chart"/>
       <property name="chartView">
           <beanclass="com.comm.ChartView"/>
       </property>
   </bean>


ChartViewResolver
public class ChartViewResolver extends AbstractCachingViewResolver{
    protectedfinal Log logger = LogFactory.getLog(getClass());
    privateString chartSuffix;//后缀
    private ViewchartView;
     @Override
    protectedView loadView(String viewName, Locale locale) throws Exception{
       logger.debug("【解析Chart视图名称】" + viewName);
       View view = null;
       if (viewName.endsWith(this.getChartSuffix())){
           view =this.getChartView();
       }
       return view;
    }
......
}


ChartView.java
public class ChartView extends AbstractView {
    protectedfinal Log logger = LogFactory.getLog(getClass());

    public LoggetLogger() {
       return logger;
    }

   @Override
    protectedvoid renderMergedOutputModel(Map map, HttpServletRequest request,HttpServletResponse response) throws Exception {
       this.getLogger().debug("【运行】Chart 视图,直接以PNG格式输出信息......" +map);
       int iChartType = Integer.parseInt((String)map.get("ChartEntityType"));
       switch (iChartType) {
           //直方图
           case 1:
               ChartBarEntity chartBarEntity = (ChartBarEntity)map.get("ChartEntityBAR");
               ChartUtilities.writeChartAsJPEG(response.getOutputStream(),setBarChart(chartBarEntity), chartBarEntity.getWidth(),chartBarEntity.getHeight());
           //饼图
           case 2:
               ChartPieEntity chartPieEntity = (ChartPieEntity)map.get("ChartEntityPIE");
               ChartUtilities.writeChartAsJPEG(response.getOutputStream(),setPieChart(chartPieEntity), chartPieEntity.getWidth(),chartPieEntity.getHeight());
           //折线图
           case 3:
               //暂缺
       }
    }

   //根据传入信息设置直方图
    privateJFreeChart setBarChart(ChartBarEntity chartBarEntity) {
       JFreeChart chart = ChartFactory.createBarChart3D(
               chartBarEntity.getTitle(), //图表标题
               chartBarEntity.getVerticalLabel(),// 目录轴的显示标签
               chartBarEntity.getHorizontalLabel(),// 数值轴的显示标签 dataset
               chartBarEntity.getDateset(), // 数据集
               PlotOrientation.VERTICAL, // 图表方向:水平、垂直
               true,// 是否显示图例(对于简单的柱状图必须是false)
               true, // 是否生成工具
               false// 是否生成URL链接
       );

       chart.getTitle().setFont(new Font("宋体", Font.BOLD, 20));
       chart.getCategoryPlot().getDomainAxis().setTickLabelFont(newFont("宋体", Font.BOLD, 11));
       chart.getCategoryPlot().getDomainAxis().setLabelFont(new Font("黑体",Font.BOLD, 12));
       chart.getCategoryPlot().getRangeAxis().setTickLabelFont(newFont("宋体", Font.BOLD, 11));
       chart.getCategoryPlot().getRangeAxis().setLabelFont(new Font("黑体",Font.BOLD, 12));

       chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 11));
       return chart;
    }

   //根据传入信息设置饼图
    privateJFreeChart setPieChart(ChartPieEntity chartPieEntity) {
       JFreeChart chart = ChartFactory.createPieChart3D(
               chartPieEntity.getTitle(),
               chartPieEntity.getDataset(),
               true,
               true,
               false
       );
       chart.getTitle().setFont(new Font("宋体", Font.BOLD, 20));

       PiePlot piePlot = (PiePlot) chart.getPlot();
       piePlot.setLabelFont(new Font("宋体", Font.BOLD, 12));
       //设置Pie的边框是否可见
       piePlot.setSectionOutlinesVisible(true);
       // 指定图片的透明度(0.0-1.0)
       piePlot.setForegroundAlpha(1.0f);
       //设置边框的颜色
       piePlot.setBaseSectionOutlinePaint(Color.black);
       //设置边框的粗细,new BasicStroke(2.0f)
       piePlot.setBaseSectionOutlineStroke(new BasicStroke(1));
       //设置空值,0值,负值是否显示出来,如果显示的话就是false
       piePlot.setIgnoreNullValues(true);
       piePlot.setIgnoreZeroValues(true);
       piePlot.setNoDataMessage("无数据显示");

       //设置字体,非常关键不然会出现乱码的,下方的字体
       chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 12));
       return chart;
    }
}


ChartWebClickAnalytics .java
.
.....
   //初始化 chart 数据集,返回直方图
   @RequestMapping(value = "/system/webanalyticsBAR.chart", method =RequestMethod.GET)
    publicString setChartDataSet(@ModelAttribute("pojo")WebClickAnalyticsCommand webClickAnalyticsCommand, ModelMapmodelMap) {
       DefaultCategoryDataset dataset = newDefaultCategoryDataset();
       this.getLogger().debug("【运行】传入参数:" +webClickAnalyticsCommand.toString());
       for (Object obj : this.getDataSet(webClickAnalyticsCommand)){
           ChartBarItem chartBarItem = (ChartBarItem) obj;
           dataset.addValue(chartBarItem.getCountAll(),chartBarItem.getItemType(), chartBarItem.getItemName());
       }
       ChartBarEntity chartEntity = new ChartBarEntity(dataset,"单位用户统计点击次数", "点击次数", "人员", 700, 500);
       modelMap.addAttribute("ChartEntityBAR", chartEntity);
       modelMap.addAttribute("ChartEntityType", "1");
       return "webanalytics.chart";
   
    private ListgetDataSet(WebClickAnalyticsCommand webClickAnalyticsCommand){
       String sql = "select count(*) as countAll,Nvl(b.name,'其他') asitemName,c.name as itemType from sys_click_log a,sys_usersb,sys_depts c where a.usid = b.id(+) and  b.dpid =c.id   and logtime>= to_date(?,'YYYY-MM-DD') and logtime<= to_date(?,'YYYY-MM-DD') group by c.name,b.nameorder by c.name,b.name ";
       this.getLogger().debug("【运行】初始化 chart 数据集......,传入参数:" +webClickAnalyticsCommand.toString());
       SQLQuery query = (SQLQuery)this.getSystemService().getSystemDAO().getCustomSession().createSQLQuery(sql).setString(0,webClickAnalyticsCommand.getDate_begin()).setString(1,webClickAnalyticsCommand.getDate_end());//.setResultTransformer(Transformers.aliasToBean(ChartBarItem.class));
       query.addScalar("countAll", Hibernate.INTEGER);
       query.addScalar("itemType", Hibernate.STRING);
       query.addScalar("itemName", Hibernate.STRING);
       query.setResultTransformer(Transformers.aliasToBean(ChartBarItem.class));
       return query.list();
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值