JFreechart 制作柱状图、饼图、折线图、仪表图等统计图的使用方法

      最近,项目里面要用到仪表显示。所以拿过来学习一下JFreechart,其实效果还蛮不错的。本来想用Flash做个交互性的,可是由于时间选择了JFreechart。

下面简要介绍一下JFreechart制作仪表盘的步骤:

 程序的运行效果

                                                                                                   

  1.首先,在Jfreechart官网或者我的资源中下载两个jar包,一个是jcommon-1.0.14.jar和jfreechart-1.0.14.jar,最好是两个jar包版本一致。JFreechart官网 http://www.jfree.org/index.html

 2. 在web.xml中进行配置,servlet-name、servlet-mapping,例子如下: 

       <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>

3. 在程序中就可以调用jar包进行coding了,例子如下:

            //数据集对象,生成仪表盘图
           DefaultValueDataset datasetTemp=new DefaultValueDataset();
          datasetTemp.setValue(25);
          DialPlot plot=new DialPlot();
          plot.setView(0.0d, 0.0d, 1.0d, 1.0d);
         plot.setDataset(0, datasetTemp);
        //设置表盘
        StandardDialFrame frame=new StandardDialFrame();
        plot.setDialFrame(frame);
        DialBackground dialBackground=new DialBackground();
        dialBackground.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
        plot.setBackground(dialBackground);
        DialTextAnnotation textAnnotation=new DialTextAnnotation("温度(℃)");
        textAnnotation.setFont(new Font("Dialog",1,10));
        textAnnotation.setRadius(0.555555555555555555D);
        plot.addLayer(textAnnotation);
        //指针指向的数据用文本表现出来,并指向一个数据集
        DialValueIndicator dial1=new DialValueIndicator(0);
        dial1.setFont(new Font("Dialog",Font.PLAIN,13));
        dial1.setOutlinePaint(Color.darkGray);
        plot.addLayer(dial1);
        //根据表盘直径大小,设置总刻度范围
        StandardDialScale scale1=new StandardDialScale();
        scale1.setLowerBound(-40);
        scale1.setUpperBound(60);
        scale1.setStartAngle(-120D); // 弧度为120,刚好与人的正常视觉对齐  
        scale1.setExtent(-300D); // 弧度为300,刚好与人的正常视觉对齐  
        scale1.setTickRadius(0.88D); // 值越大,与刻度盘框架边缘越近  
        scale1.setTickLabelOffset(0.14999999999999999D); // 值越大,与刻度盘刻度越远0
        scale1.setTickLabelFont(new Font("Dialog", 0, 14)); // 刻度盘刻度字体  
        plot.addScale(0, scale1);
        //设置刻度范围(红色)
        StandardDialRange standarddialrange = new StandardDialRange(40D, 60D, Color.red);
        standarddialrange.setInnerRadius(0.52000000000000002D);
        standarddialrange.setOuterRadius(0.55000000000000004D);
        plot.addLayer(standarddialrange);
        //设置刻度范围(橘黄色)          
        StandardDialRange standarddialrange1 = new StandardDialRange(10D, 40D, Color.orange);
        standarddialrange1.setInnerRadius(0.52000000000000002D);
        standarddialrange1.setOuterRadius(0.55000000000000004D);
        plot.addLayer(standarddialrange1);
        //设置刻度范围(绿色)              
        StandardDialRange standarddialrange2 = new StandardDialRange(-40D, 10D, Color.green);
        standarddialrange2.setInnerRadius(0.52000000000000002D);
        standarddialrange2.setOuterRadius(0.55000000000000004D);
        plot.addLayer(standarddialrange2);

       // 指针一  
        Pointer pointer = new Pointer(0); // 内部内
        pointer.setFillPaint(new Color(144,196,246));
        plot.addPointer(pointer);
        plot.mapDatasetToScale(0, 0);
        DialCap dialcap = new DialCap();  
        dialcap.setRadius(0.0700000000000001D);
        plot.setCap(dialcap);
        JFreeChart chart=new JFreeChart(plot);
        chart.setBackgroundPaint(new Color(234,244,253));
        String filename=ServletUtilities.saveChartAsPNG(chart, 200, 150, request.getSession());
        request.setAttribute("filename", filename);

4.在前台jsp页面调用,示例代码如下:

     String filename=(String)request.getAttribute("filename");

    String graphUrl=request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;

   这样就可以在界面进行调用了<img src="<%=graphUrl%>"/>

  希望大家多多留言啊,像这类统计图还有什么比较好的么,谢谢啦,共同学习啊!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值