zoukankan      html  css  js  c++  java
  • JFreeChart在struts2中实现柱状图 skyCc

    \使用的是JFreeChart在struts2中的插件

    1.将jfreechart的jar包放到项目中的lib文件夹下,servlet.jar和gnujaxp.jar不必放,第一个没有什么用处,因为tomcat中已有,第二个如果放进去了发布的时候可能会出现xml解析之类的错误,原因是由于你的SSH项目中可能已经有解析xml的jar文件了,产生冲突,这时首先:右击项目-->从configure build path中将其移除,再从lib下删除重新发布即可。

    2.struts2与jfreechart的整合还需要struts2-jfreechart-plugin-2.1.8.jar,尽量下新版本的,这样查看其中的struts-plugin.xml,有extends="struts-default"。

    3.假如出现找不到xwork下的LoggerFactory的异常,去下载一个高版本的xwork的jar文件就OK了。

    好了,可以在struts2中制作图形报表了。

    BarChart.java:

    public class BarChart  extends ActionSupport {

     private static final long serialVersionUID = 1L;
     private JFreeChart chart;
       
     public JFreeChart getChart(){  
      
      double[][] data = new double[][]
                                     {
                                     { 672, 766, 223, 540, 126 },
                                     { 222, 540, 456, 678, 345 },
                                     { 512, 412, 621, 426, 532 },
                                    };
      
            String[] rowKeys = { "苹果","香蕉","梨子"};
          
            String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
      CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
      
         JFreeChart chart = ChartFactory.createBarChart3D("柱状图", "x轴", "y轴", dataset, PlotOrientation.VERTICAL, true, true, false);     
         //重新设置图标标题,改变字体
         chart.setTitle(new TextTitle("柱状图", new Font("黑体", Font.ITALIC , 22)));
         CategoryPlot plot = (CategoryPlot)chart.getPlot();
         //取得横轴
         CategoryAxis categoryAxis = plot.getDomainAxis();
         //设置横轴显示标签的字体
         categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));
         //分类标签以45度角倾斜
         categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
         categoryAxis.setTickLabelFont(new Font("宋体" , Font.ITALIC , 15));

         CategoryAxis domainAxis = plot.getDomainAxis();
         //设置距离图片左端距离
         domainAxis.setLowerMargin(0.1);
         //设置距离图片右端距离
         domainAxis.setUpperMargin(0.1);
         
        BarRenderer3D renderer = new BarRenderer3D();
         renderer.setItemMargin(0.1);//组内柱子间隔为组宽的10% 
         renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//显示柱子上的数值
         renderer.setBaseItemLabelsVisible(true);
         plot.setRenderer(renderer);//使用我们设计的效果
         
         //取得纵轴
         NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
         //设置纵轴显示标签的字体
         numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));

      // 设置最高的一个   Item   与图片顶端的距离  
         numberAxis.setUpperMargin(0.15);  
    //     // 设置最低的一个   Item   与图片底端的距离  
    //     numberAxis.setLowerMargin(0.15);  
         plot.setRangeAxis(numberAxis);  

            Font font00 = new Font("宋体",Font.BOLD,18);
      LegendTitle legend = chart.getLegend();
      legend.setItemFont(font00);//设置注释字体
         
      return chart;
     } 
     
    }

    chart-struts.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd">


    <struts>
     <package name="jCuckoo" namespace="/jCuckoo"
      extends="jfreechart-default">
     

      <action name="barChart" class="jCuckoo.BarChart">
       <result type="chart">
        <param name="width">600</param>
        <param name="height">450</param>
       </result>
      </action>
      
     </package>
    </struts>

    struts.xml中加入:

    <include file="/jCuckoo/chart-struts.xml"></include>

    现在就可以通过http://localhost:8080/项目名/jCuckoo/barChart.action进行访问了。

    为了对柱状图更好的操作,可以通过jsp进行访问,在index.jsp中加入:

    <img src="<%=path %>/jCuckoo/barChart.action">

    通过访问http://localhost:8080/就可以看到柱状图了。

    JFreeChart在struts2中实现柱状图 - candy - 好记忆不如烂笔头

    //设置柱子上比例数值的显示,如果按照默认方式显示,数值为方向正常显示
           
            //设置柱子上显示的数据旋转90度,最后一个参数为旋转的角度值/3.14
              ItemLabelPosition itemLabelPosition= new ItemLabelPosition(
              ItemLabelAnchor.INSIDE12,TextAnchor.CENTER_RIGHT,
              TextAnchor.CENTER_RIGHT,-1.57D);
           
            //下面的设置是为了解决,当柱子的比例过小,而导致表示该柱子比例的数值无法显示的问题
             
            //设置不能在柱子上正常显示的那些数值的显示方式,将这些数值显示在柱子外面
              ItemLabelPosition itemLabelPositionFallback=new ItemLabelPosition(
              ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT,
              TextAnchor.HALF_ASCENT_LEFT,-1.57D);
           
            //设置正常显示的柱子label的position
            renderer.setPositiveItemLabelPosition(itemLabelPosition);
            renderer.setNegativeItemLabelPosition(itemLabelPosition);
           
            //设置不能正常显示的柱子label的position
            renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
            renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);

  • 相关阅读:
    简述 JPA 与 Spring Data JPA 与 Hibernate
    idea不识别yml配置文件,怎么办?
    Java 数组如何转成List集合
    git关于 LF 与 CRLF
    git报错:src refspec master does not match any
    蓝桥杯- 基础练习:十六进制转八进制
    蓝桥杯- 基础练习:数列排序
    蓝桥杯-入门训练 :A+B问题
    蓝桥杯-入门训练 :序列求和
    蓝桥杯-入门训练 :圆的面积
  • 原文地址:https://www.cnblogs.com/cmzcheng/p/2437791.html
Copyright © 2011-2022 走看看