posts - 57, comments - 4, trackbacks - 0, articles - 10
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理
以前上级让整理的,花了挺多工夫,现在估计已经落后了.供初学者参考.

1.    download JFreeChart ZIP
 download  jfreechart-1.0.0-rc3.zip
 
2. jfreechart config and install
    1. Extract the file jfreechart-1.0.0-rc3.zip to some place:package source is the souce code of jfreechart;jfreechart-0.9.21-demo.jar is the example ,you can run it directly;package lib is which we need ,copy them to the package lib of the structs.
    2. Add  codes as below  to WEB-INF"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>
 3 .programming   
 
⑴constitute DataSet :
Create dateset such as DefaultPieDataset DefaultCategoryDataset, XYSeriesCollection and so on,and then fill data in them.
⑵ create jfreechart plot :
Create plot such as  XYPlot, PiePlot3D, CategoryPlot and so on.
⑶ create JFreeChart :
There are two ways.one is through build a jfreechart class by plot.The other is to create by using Class ChartFactory,this way can omit step 2.
show chart: save Chart As pictures and come out the URLs of the pictures.And the show them through image property.
package yjf.biz.jchart;
import  org.jfree.data.general.DefaultPieDataset;
import  org.jfree.data.category.DefaultCategoryDataset;
import  org.jfree.data.xy.DefaultTableXYDataset;
import  org.jfree.data.xy.CategoryTableXYDataset;
import  org.jfree.data.xy.XYSeries;
import  org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.*;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.urls.TimeSeriesURLGenerator;
import org.jfree.ui.TextAnchor;
import java.util.Locale;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.awt.Insets;
import java.awt.Font;
import java.awt.Color;
import java.awt.geom.Ellipse2D;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class generatechart {
public static String generateXYChart(HttpSession session,PrintWriter pw) {
            String filename = null;
         try {
                  ArrayList site = new ArrayList();
                    site.add("aaa");
                        ....
                  
                 Iterator iter = site.listIterator();
                 CategoryTableXYDataset xydataset= new CategoryTableXYDataset();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd",Locale.UK);
           while (iter.hasNext())
           {
             String section = (String)iter.next();
             //dataset自添加
            if  (  dataset.getRowCount()>0)
             {
               for (int i=0;i<dataset.getRowCount();i++)
              {
                xydataset.add((sdf.parse(dataset.getCells(i,0))).getTime(),
                Double.valueOf((String)dataset.getCells(i,1)).doubleValue(),section);
                System.out.print((sdf.parse(dataset.getCells(i,0))).getTime()+"$"+dataset.getCells(i,0)+"*"+
                               Double.valueOf((String)dataset.getCells(i,1)).doubleValue()+" ");
              }
             }
            else{}
            }
             //  Create tooltip and URL generators
           StandardXYToolTipGenerator ttg = new StandardXYToolTipGenerator(
                                                StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                                                sdf, NumberFormat.getInstance());
           TimeSeriesURLGenerator urlg = new TimeSeriesURLGenerator(
                                             sdf, "barchart3D.jsp", "series", "hitDate");

          JFreeChart chart = ChartFactory.createTimeSeriesChart("TimeSeriesChart", "date", "smt_nt",xydataset,true, true,false);
          XYPlot plot=chart.getXYPlot();
          StandardXYItemRenderer renderer = new StandardXYItemRenderer(
                                                           StandardXYItemRenderer.LINES + StandardXYItemRenderer.SHAPES,
                                                           ttg, urlg);
          plot.setRenderer(renderer);

         chart.setBackgroundPaint(java.awt.Color.white);

              filename = ServletUtilities.saveChartAsPNG(chart,780,500, null, session);
              //  Write the image map to the PrintWriter
            }
         catch (Exception e) {
            System.out.println("Exception - " + e.toString());
            e.printStackTrace(System.out);
            filename = "public_error_500x300.png";
          }
         return filename;
      }
   public String generatePieChart(String title, HttpSession session)
     {
       DefaultPieDataset piedata = new DefaultPieDataset();
       String filename = null;
       try {
               //dataset自添加
           if  (  dataset.getRowCount()>0)
             {
               for (int i=0;i<dataset.getRowCount();i++)
                 {
                     piedata.setValue(dataset.getCells(i,0),Double.valueOf((String)dataset.getCells(i,1)).doubleValue());
                 }
               }
           else{}
        PiePlot3D  plot = new PiePlot3D(piedata);
           plot.setDepthFactor(0.05);
           plot.setLabelGenerator(new StandardPieItemLabelGenerator(StandardPieItemLabelGenerator.DEFAULT_TOOLTIP_FORMAT));
           plot.setLabelGenerator(new StandardPieItemLabelGenerator("{0}/{1}k/{2}"));
        JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,plot, true);
        filename = ServletUtilities.saveChartAsPNG(chart, 750, 500, null,session);

  }
     catch (Exception e)
             {
                     System.out.println("Exception - " + e.toString());
                     e.printStackTrace(System.out);
                     filename = "public_error_500x300.png";
             }
     return filename;
 }

public String generateStackedBarChart3D1(HttpSession session)
{
   DefaultCategoryDataset stackedDateset= new DefaultCategoryDataset();
   String filename = null;
 try {
            //dataset自添加
     if  (  dataset.getRowCount()>0)
      {
         for (int i=0;i<dataset.getRowCount();i++)
        {
             stackedDateset.addValue(Double.valueOf((String)dataset.getCells(i,2)).
             doubleValue(),dataset.getCells(i,1),dataset.getCells(i,0));
        }
      }
     else{}

  JFreeChart chart = ChartFactory.createStackedBarChart3D("StackedBarChart3D1", "date", "smt_nt",stackedDateset, PlotOrientation.VERTICAL,true, true,true);
  filename = ServletUtilities.saveChartAsPNG(chart,750,500, null,session);
  }
catch (Exception e)
  {
      System.out.println("Exception - " + e.toString());
       e.printStackTrace(System.out);
       filename = "public_error_500x300.png";
  }
 return filename;
}
public String generateBarChart3D(HttpSession session)
  {
     DefaultCategoryDataset stacked1Dateset= new DefaultCategoryDataset();
     String filename = null;
    try {
         //dataset自添加
       if  (  dataset.getRowCount()>0)
           {
              for (int i=0;i<dataset.getRowCount();i++)
              {
                  stacked1Dateset.addValue(Double.valueOf((String)dataset.getCells(i,2)).
                  doubleValue(),dataset.getCells(i,1),dataset.getCells(i,0));
              }
          }
       else{}

   JFreeChart chart = ChartFactory.createBarChart3D("BarChart3D1", "date", "smt_nt",stacked1Dateset, PlotOrientation.VERTICAL,true, true,true);
   filename = ServletUtilities.saveChartAsPNG(chart,750,500, null,session);
   }
   catch (Exception e)
          {
                  System.out.println("Exception - " + e.toString());
                  e.printStackTrace(System.out);
                  filename = "public_error_500x300.png";
          }
    return filename;
  }

public String generateStackedBarChart3D2(HttpSession session)
{
   String filename = null;
   DefaultCategoryDataset stacked1Dateset= new DefaultCategoryDataset();
   try {
      
       if  (  dataset.getRowCount()>0)
        {
           for (int i=0;i<dataset.getRowCount();i++)
           {
                  stacked1Dateset.addValue(Double.valueOf((String)dataset.
                  getCells(i,2)).doubleValue(),dataset.getCells(i,1),dataset.getCells(i,0));
           }
        }
       else{}

     JFreeChart chart = ChartFactory.createStackedBarChart3D
                         ("StackedBarChart3D1", "date", "smt_nt",stacked1Dateset, PlotOrientation.VERTICAL,true, true,true);
     filename = ServletUtilities.saveChartAsPNG(chart,750,500, null,session);

     }
     catch (Exception e)
     {
         System.out.println("Exception - " + e.toString());
         e.printStackTrace(System.out);
         filename = "public_error_500x300.png";
     }
   return filename;
 }
}
  jsp:
<%
generatechart chart = new generatechart();
String piechart = chart.generatePieChart("PieChart",session);
String piechartURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + piechart;
String barchart3D = chart.generateBarChart3D(session);
String barchart3DURL = request.getContextPath() + "/servlet/DisplayChart?filename=" +barchart3D;
String stackedbarchart3D1 = chart.generateStackedBarChart3D1(session);
String stackedbarchart3D1URL = request.getContextPath() + "/servlet/DisplayChart?filename=" +stackedbarchart3D1;
String stackedbarchart3D2 = chart.generateStackedBarChart3D2(session);
String stackedbarchart3D2URL = request.getContextPath() + "/servlet/DisplayChart?filename=" + stackedbarchart3D2;
String linechart = chart.generateXYChart(session,new PrintWriter(out));
String linechartURL= request.getContextPath() + "/servlet/DisplayChart?filename=" + linechart;
%>
<HTML>
<HEAD>
           <TITLE>yjf/jchart/jfreechart</TITLE>
</HEAD>
<BODY>
<P ALIGN="CENTER">
           <img src="<%= piechartURL %>" width=750 height=500 border=0 ">
</P>
<P ALIGN="CENTER">
           <img src="<%= barchart3DURL%>" width=750 height=500 border=0 ">
</P>
<P ALIGN="CENTER">
           <img src="<%= stackedbarchart3D1URL %>" width=750 height=500 border=0 ">
</P>
<P ALIGN="CENTER">
           <img src="<%=stackedbarchart3D2URL%>" width=750 height=500 border=0 ">
</P>
<P ALIGN="CENTER">
           <img src="<%= linechartURL%>" width=700 height=500 border=0 usemap="#<%=linechart%>">
</P>

发表于 @ 2006年09月05日 13:45:00|评论(0 )

新一篇: 动 | 旧一篇: Treeview


只有注册用户登录后才能发表评论。


网站导航: