JFreeChart生成饼形及柱形图例子

JFreeChart生成饼形及柱形图例子
 
       在Java中利用JFreeChart可轻松生成各种统计分析图形,包括柱形图/饼图/曲线图/折线图/甘特图等,JFreeChart可从 [url]http://www.jfree.org/jfreechart/[/url] 获得jar包,现在最新版本为JFreeChart 1.05。

一、从网站下载相关JAR包,关键包包括jfreechart-1.0.5.jar和jcommon-1.0.9.jar,将这两个JAR加到ECLIPSE项目的构建路径,并放到%TOMCAT_HOME%common\lib目录下,配置WEB项目的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>/servlet/DisplayChart</url-pattern>
</servlet-mapping>
配置以上servlet是为了在网页中正常显示图形,若配置不正确,图形将不会显示。

二、图形生成类ChartGenerater.java
import java.awt.Color;
import java.awt.Font;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

/**
*
* @ 利用JFreeChart生成柱形及饼状图
* @version  JFreeChart 1.05 jcommon-1.0.9
* @author Jojoy
* @ List list 生成图形的源数据
* @ String titles 标题
* @ String x 图形的水平轴注释
* @ String y 图形的垂直轴注释
* @ String charttype 图形类型,piechart为饼图,barchart为柱形图
*/
public class ChartGenerater {
public static String generatePieChart(List list, String titles,
   HttpSession session, PrintWriter pw, String x,String y,String charttype) {
  String filename = null;
  Font font;
  if ("piechart".equals(charttype.trim())) {
   try {
    // 建立PieDataSet
    DefaultPieDataset data = new DefaultPieDataset();
    Iterator it = list.iterator();
    while (it.hasNext()) {
     Object[] rows = (Object[]) it.next();
     String result = (String) rows[0];
     int count = ((Integer) rows[1]).intValue();
     data.setValue(result, count);
    }
    // 生成chart物件
    PiePlot3D plot = new PiePlot3D(data);// 生成一个3D饼图
    plot.setForegroundAlpha(0.6f);
    plot.setToolTipGenerator(new StandardPieToolTipGenerator());
    font = new Font("黑体", Font.CENTER_BASELINE, 20);// 这个地方是设置统计图标题的字体和大小
    JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    TextTitle tt = new TextTitle(titles);
    tt.setFont(font);
    chart.setBackgroundPaint(java.awt.Color.white);// 统计图片的底色
    chart.setTitle(tt);
    // 把生成的文件写入到临时的目录中
    ChartRenderingInfo info = new ChartRenderingInfo(
      new StandardEntityCollection());
    filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
      info, session);
    // 选择存储成png格式的文件,当然你也可以使用saveChartAsJPEG的方法生成jpg图片
    // 把image map 写入到 PrintWriter
    ChartUtilities.writeImageMap(pw, filename, info, false);
    pw.flush();
   } catch (Exception ex) {
    System.out.println("error:" + ex.getMessage());
   }
  }
  if ("barchart".equals(charttype.trim())) {
   try {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    Iterator it = list.iterator();
    while (it.hasNext()) {
     Object[] rows = (Object[]) it.next();
     String author = (String) rows[0];
     int count = ((Integer) rows[1]).intValue();
     dataset.addValue(count, author, author);
    }
    
    //生成柱形图
    JFreeChart chart = ChartFactory.createBarChart3D(titles,
      x, y, dataset, PlotOrientation.VERTICAL,
      true, false, false);
    chart.setBackgroundPaint(Color.WHITE);
    CategoryPlot plot = chart.getCategoryPlot();
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setAxisLineVisible(false);
    plot.setDomainAxis(domainAxis);
    BarRenderer3D renderer = new BarRenderer3D();
    renderer.setBaseOutlinePaint(Color.BLACK);
    // 设置每个地区所包含的平行柱的之间距离
    renderer.setItemMargin(0.1);
    // 显示每个柱的数值,并修改该数值的字体属性
    renderer
      .setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer.setItemLabelsVisible(true);
    plot.setRenderer(renderer);
    // 设置柱的透明度
    plot.setForegroundAlpha(0.8f);
    StandardEntityCollection sec = new StandardEntityCollection();
    ChartRenderingInfo info = new ChartRenderingInfo(sec);
    
    // 500是图片长度,300是图片高度
    // String filename =
    // ServletUtilities.saveChartAsPNG(chart,500,300,info,session);
    filename = ServletUtilities.saveChartAsJPEG(chart, 500, 300,
      info, session);
    ChartUtilities.writeImageMap(pw, filename, info, false);
    pw.flush();    
   }catch (Exception ex) {
    System.out.println("error:" + ex.getMessage());
   }
  }  
  return filename;
}
}

三、JSP页面调用
<%
      List list=(ArrayList)session.getAttribute("datalist");
      String filename = ChartGenerater.generatePieChart(list,"信息发布结果统计",session, new PrintWriter(out),"","","piechart");
     String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
%>
<table ALIGN="CENTER">
    <tr>
    <td>  
  <img src=\'#\'" graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>">  
    </td>
    </tr>
    </table>  

你可能感兴趣的:(jfreechart,职场,休闲)