jfreechart 应用讲解,案例

      jfreechart是一项数据处理成图像的技术,随着现在的发展,jfreechart的优势越来越明显。现在用一个小小的案例,来讲解一下jfreechart的柱形图和饼图的基本用法和设置。由于我们经常要在不同的界面生成图,总不能有一个页面我们写一回吧,那样代码的冗余度太大了,所以,我把它封装成了一个类,便于后面调用。废话不说了,直接上代码。


<1>在webInF/lib下导入两个jar包,免费的下载址:http://download.csdn.net/detail/zz20104534/4988315
<2>配置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>


<3>新建一个类


package com.xlh.ndyy.jfreechart;
/*
 * author Zhao yangyang 
 * */
import java.awt.Font;
import java.io.IOException;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;


import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;


public class jfreechart {
 
@SuppressWarnings("null")
public String bar(int icount,int icount2,int icount4,String a,String b,String c,String title) throws Exception
{

/*
         * bar图
         * */
        
        DefaultCategoryDataset dataset =new DefaultCategoryDataset();
        dataset.addValue(icount,"",a);
        dataset.addValue(icount2,"",b);
        dataset.addValue(icount4,"",c);
        JFreeChart chart=ChartFactory.createBarChart3D(title,"学员状态","人数",dataset,PlotOrientation.VERTICAL,false,false,false);
        chart.setBackgroundPaint(java.awt.Color.white);
      //设置图片标题的字体和大小
        Font font2 = new Font("黑体",Font.CENTER_BASELINE,20);     
        TextTitle _title = new TextTitle(title);
        _title.setFont(font2);
        chart.setTitle(_title);
        Font font = new Font("黑体",Font.BOLD,24); 
        
        Font font1 = new Font("宋体",Font.BOLD,14);        
      //  LegendTitle legend = new LegendTitle(source) ;  
        
       // legend.setItemFont(font1);//设置注释字体 
        CategoryPlot plot = chart.getCategoryPlot();
        plot.getRangeAxis().setLabelFont(font1);//设置y轴字体           
        plot.getRangeAxis().setTickLabelFont(font1);//设置刻度字体           
        plot.getDomainAxis().setLabelFont(font1);//设置X轴字体           
        plot.getDomainAxis().setTickLabelFont(font1);//设置刻度字体
        
        HttpSession session=null;
        HttpServletRequest request=null;
        String filename=ServletUtilities.saveChartAsPNG(chart,500,300,null,session);
return filename;
}
 /*
     * pie图
     * 
     * */
@SuppressWarnings("null")
public String pie(String m,String n,String p,float rate1,float rate2) throws Exception{
org.jfree.data.general.DefaultPieDataset data = new org.jfree.data.general.DefaultPieDataset();
   data.setValue(m,rate1);
   data.setValue(n,rate2);
   JFreeChart chart_pie = ChartFactory.createPieChart3D(p,data,true,false,false);
    chart_pie.setBackgroundPaint(java.awt.Color.white);
     //设置图片标题的字体和大小
       Font font = new Font("黑体",Font.CENTER_BASELINE,20);
       TextTitle _title2 = new TextTitle(p);
       _title2.setFont(font);
       chart_pie.setTitle(_title2);
       PiePlot pieplot = (PiePlot) chart_pie.getPlot();
       pieplot.setLabelFont(new Font("黑体",0,12));
       pieplot.setLabelFont(new Font("黑体", Font.BOLD, 10));
       chart_pie.getLegend().setItemFont(new Font("黑体", Font.BOLD, 10));
 //没有数据的时候显示的内容
   pieplot.setNoDataMessage("无数据显示");
 
   pieplot.setCircular(false);
  
   pieplot.setLabelGap(0.02D);
   HttpSession session=null;
       HttpServletRequest request=null; 
   String filename_pie;
            filename_pie = ServletUtilities.saveChartAsJPEG(chart_pie,500,300, session);
return filename_pie;

   
  
}
   


}
<4>调用这个类的不同方法 在写一个text.java


下面的参数,如icount, icount2,icount4,updoor_rate, m,自己定义变量,设定其值,随便设置。注意他们对应的类型。


 /*
         * bar图
         * */
        jfreechart jfreechart=new jfreechart();
        String filename=jfreechart.bar(icount, icount2, icount4,"苹果","香蕉","柿子","统计图");
        String graphURL=request.getContextPath()+"/DisplayChart?filename="+filename;
        request.setAttribute("filename",filename);
        request.setAttribute("graphURL",graphURL);
        /*
         * pie图
         */
   
        String filename_pie=jfreechart.pie("苹果","香蕉", "统计图", updoor_rate, m);
        String graphURL_pie = request.getContextPath()+"/DisplayChart?filename="+filename_pie;
   request.setAttribute("filename_pie",filename_pie);
   request.setAttribute("graphURL_pie",graphURL_pie);
<5>新建text.jsp


添加如下:
   <td align="center" class="tdbg"><img src="<%=request.getAttribute("graphURL")%>"width=550 height=300 border=0 usemap="#<%=request.getAttribute("filename")%>"></td>
 <tr> <td   align="center"><img src="<%=request.getAttribute("graphURL_pie")%>"width=500 height=300 border=0 usemap="#<%=request.getAttribute("filename_pie")%>"></td></tr>

你可能感兴趣的:(jfreechart 应用讲解,案例)