2010.03.08 - JfreeChart of the pie, as well as the steps and the three results demonstrate the way

sponsored links
2010.03.08 - JfreeChart of the pie, as well as the steps and the three results demonstrate the method reference: http://blog.csdn.net/qjyong/archive/2009/12/08/4967705.aspx

1, JfreeChart the basic steps:
1, the establishment of Dataset, create a data set all data objects are stored in the Dataset. (Create a data source (dataset) to include will be displayed in the graph data)
2, the establishment of JFreeChart, create a chart
3, select the results of the output approach.

Important classes and interfaces:
org.jfree.data.general.Dataset all data source types must implement interface
org.jfree.chart.ChartFactory by it to generate a JFreeChart object
org.jfree.chart.JFreeChart all the right graphics adjustment is through it Oh! !
org.jfree.chart.plot.Plot get it through the JFreeChart object, and then through the outer portion of its graphics (example: axis) Adjustment Note: it has many sub-categories are generally related to it under the subclass!
org.jfree.chart.renderer.AbstractRenderer get it through the JFreeChart object, and then through its internal part of the graph (example: Line type) to adjust. Similarly, for different types of statements of plans, which have different sub-classes to achieve! In the following we may refer to it as Renderer

Second, the results demonstrate three ways:
1. Output Chart to Swing method window, see below drawToFrame
2. Output graph to disk, see below drawToOutputStream method
3. Output chart to the page

3, pie
package test;   
import java.awt.BasicStroke;   
import java.awt.Color;   
import java.awt.Font;   
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.text.DecimalFormat;   
import java.text.NumberFormat;   
import org.jfree.chart.ChartFactory;   
import org.jfree.chart.ChartFrame;   
import org.jfree.chart.ChartUtilities;   
import org.jfree.chart.JFreeChart;   
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;   
import org.jfree.chart.labels.StandardPieToolTipGenerator;   
import org.jfree.chart.plot.PiePlot;   
import org.jfree.chart.title.TextTitle;   
import org.jfree.chart.urls.StandardPieURLGenerator;   
import org.jfree.data.general.DefaultPieDataset;   
import org.jfree.data.general.PieDataset;   
import org.jfree.util.Rotation;   
 *  Draw a pie chart using JFreeChart    
 * @author qiujy  
public class PieChartTest {   
     * step1: Create a DataSet object    
     * @return  
    public static PieDataset createDataSet() {   
        DefaultPieDataset dataset = new DefaultPieDataset();   
        dataset.setValue("java Programming language  ", 10000);   
        dataset.setValue("JSP Detailed description of the basic and case development  ", 20000);   
        dataset.setValue("struts Detailed description of the basic and case development  ", 30000);   
        dataset.setValue(" Proficient in  JSF", 40000);   
        return dataset;   
     * step2: Create a chart    
     * @param dataset  
     * @return  
    public static JFreeChart createChart(PieDataset dataset) {   
         JFreeChart chart = ChartFactory.createPieChart3D(   
        //JFreeChart chart = ChartFactory.createPieChart(   
                " Original book sales statistics ",//chart title     
                dataset, //  A data set     
                true, //  Whether to display the legend     
                true, //  Whether or not ToolTips will show     
                true //  Whether generated  URL   
        // Sets the title font = = in order to prevent the garbled  : You have to set the font     
        chart.setTitle(new TextTitle(" Original books statistics  ", new Font(" Blackbody  ", Font.ITALIC, 22)));   
        // Sets the font of the legend == in order to prevent the garbled  : You have to set the font     
        chart.getLegend().setItemFont(new Font(" Blackbody  ", Font.BOLD, 12));    
        //  Gets the pie chart Plot object  ( The actual chart  )   
        PiePlot plot = (PiePlot) chart.getPlot();    
        // Graphic border color        
        // Graphic border thickness        
        plot.setBaseSectionOutlineStroke(new BasicStroke(0.0f));    
        // Set the drawing direction of a pie chart, you can draw clockwise  , You can also draw counterclockwise        
        // To set a draft angle  ( Graphics rotation angle  )      
        // Set highlight a block of data        
        plot.setExplodePercent("One", 0.1D);     
        // Sets the background color transparency     
        //  Sets the foreground color transparency     
        // Sets the font of the block tag == in order to prevent garbled  : You have to set the font     
        plot.setLabelFont(new Font(" Calligraphy  ", Font.PLAIN, 12));    
        //  Sector separation shows, on the  3D Map is not the onset     
        plot.setExplodePercent(dataset.getKey(3), 0.1D);   
        //  The legend displays percentage  : Custom mode  ,{0}  Represents the options  , {1}  Represents a numeric value  , {2}  Represents the percentage of two-digit after the decimal point,     
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(   
                "{0}:{1}\r\n({2})", NumberFormat.getNumberInstance(),   
                new DecimalFormat("0.00%")));   
        //  The legend displays percentage     
        // plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));   
        //  Specifies the display of a pie chart  : Circular  (true)  Or oval  (false)   
        //  There is no time to display     
        plot.setNoDataMessage(" The available data could not be found  ...");       
        // Sets the mouse-over tips     
        plot.setToolTipGenerator(new StandardPieToolTipGenerator());   
        // Sets the hot links     
        plot.setURLGenerator(new StandardPieURLGenerator("detail.jsp"));   
        return chart;   
     * [color=olive]step3:  Export the chart to the  Swing Frame[/color]  
     * @param chart  
    public static void drawToFrame(JFreeChart chart){   
        // Export the chart to the  Swing Frame   
        ChartFrame frame = new ChartFrame(" Original books statistics  ", chart);   
     * [color=olive]step3:  The output graph to the specified disk  [/color]  
     * @param destPath  
     * @param chart  
    public static void drawToOutputStream(String destPath, JFreeChart chart) {   
        FileOutputStream fos = null;   
        try {   
            fos = new FileOutputStream(destPath);   
            // ChartUtilities.writeChartAsJPEG(   
            ChartUtilities.writeChartAsPNG(fos, //  Specifies the target output stream     
                    chart, //  A chart object     
                    600, //  Width     
                    400, //  High     
                    null); // ChartRenderingInfo Information     
        } catch (IOException e) {   
        } finally {   
            try {   fos.close();       
            } catch (IOException e) {   
     * [color=olive]step3:  Output chart to a Web page  [/color]  
     * @param destPath  
     * @param chart  
    public static String drawToHtml(JFreeChart chart,HttpSession session,PrintWriter out){
        ChartRenderingInfo info = new ChartRenderingInfo(
                new StandardEntityCollection());
        String fileName = "";
            fileName = ServletUtilities.saveChartAsPNG(chart, 500, 300, info,
                    session);// Generate pictures  
//          Write the image map to the PrintWriter
            ChartUtilities.writeImageMap(out, fileName, info, false);
        catch (IOException e)
        return fileName;// Returns the generated name of the picture  
//    public static void main(String[] args) throws FileNotFoundException {   
//        // step1: Create a DataSet object     
//        PieDataset dataset = createDataSet();   
//        // step2: Create a chart     
//        JFreeChart chart = createChart(dataset);   
//        // step3:  Output chart to Swing window     
//        //drawToFrame(chart);   
//        // step3:  Output chart to disk     
//        drawToOutputStream("D:\\mybook-pie.png", chart);   
//    }  

If it is entered into the page


<%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="pojo.PieChartTest"%>
<%@ page import = "java.io.PrintWriter" %>
PieChartTest chart=new PieChartTest();
String fileName=chart.drawToHtml(chart.createChart(chart.createDataSet()),session,new PrintWriter(out));
  String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName; 
<title> JFreeChart Usage example  </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<img src="<%= graphURL %>" width="500" height="300" border="0" usemap="#<%= fileName %>">// Note that this  "#"
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of 2010.03.08 - JfreeChart of the pie, as well as the steps and the three results demonstrate the way

  • JDBC example of a long time do not have JDBC forgot

    A back-up here to stay. The first: The second:

  • In the servlet use Bean

    According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...

  • The EJB3 Persistence

    EJB3 persistence with Hibernate is very similar to the mechanism: Environment: Server: JBOSS5.0 Database: MySQL5.0 1. Set up a data source First of all, in jboss-5.0.0.GA \ server \ default \ deploy, the establishment of a database used to connect the dat

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

blog comments powered by Disqus
Recent Entries
Tag Cloud
Random Entries