In our work, we often encounter examples of the need to draw statistical charts on the Web page. In fact, there are many ways to achieve, we can personally write all the graphics generated code, such as using Flash, or with SVG, so that the amount of code is very large, but also error-prone, very annoying. You can also take advantage of out-of-the-box Java Statistical Chart libraries. For example, with Jclass (http://java.quest.com/jclass/jclass.shtml), but this is a charge. For our open source programmers, there is an open source of statistical graphics library. Yes, it is www.jfree.org launched the Jfreechart package, which from the column, pie, to radar, candle map and so on all inclusive, can be in c/s,b/s, even real-time environment can be a show of skill. The key thing is that it's open source! I'm here to give a simple example of drawing a pie chart on a Web page.
1, to www.jfree.org download a new Java library, place Jfreechart.jar and Jcommon.jar.jar in Classpath
2, write a Java bean, used to generate pie chart. (Gspiechart.java)
package gov.gwssi.tax.nspg.datadisposal;
import java.awt.Insets;
import Java.awt.Image;
import Java.io.PrintWriter;
import Java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import Java.util.Iterator;
import Java.util.Locale;
import Java.text.NumberFormat;
import javax.servlet.http.HttpSession;
Import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import Org.jfree.chart.renderer.VerticalBarRenderer;
import Org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.chart.plot.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.tooltips.*;
import org.jfree.chart.urls.*;
import org.jfree.chart.servlet.*;
import Java.awt.Font;
/**
* <p>title: </p>
* <p>description: </p>
* <p>copyright:copyright (c) 2003</p>
* <p>company: </p>
* @author Jagie
* @version 1.0
*/
public class Gspiechart
{
public static void Main (string[] args)
{
long[] TestData =
{
New Long (a), new long, new Long (40)
} ;
Generatepiechart (TestData, "", NULL, New PrintWriter (System.out), 580, 250);
}
/**
* Generate image file
* @param datas long[] Array
* @param the pie shape on the targeturl point, the URL is directed to
* @param session HttpSession
* @param pw printwriter
* @param w the width of the graph generated
* @param h The height of the graph generated
* @return The path of the image file generated
*/
public static String Generatepiechart
(long[] datas, String TargetUrl,
HttpSession Session,
printwriter pw, int w, int h)
{
String filename = null;
Try
{
Defaultpiedataset data = new Defaultpiedataset ();
data.setvalue ("Normal taxpayer", datas[0]);
Data.setvalue ("exceptional taxpayer", datas[1]);
data.setvalue ("Data not full taxpayer", datas[2]);
Data.setvalue ("Not treated taxpayer", datas[3]);
//Create the Chart object
pie3dplot plot = new Pie3dplot (data);
plot.setinsets (New Insets (0, 5, 5, 5));
Plot.setforegroundalpha (0.6f);
Plot.setsectionlabeltype (plot.no_labels);
Plot.seturlgenerator (New Standardpieurlgenerator (TargetUrl, "type"));
Plot.settooltipgenerator (New Standardpietooltipgenerator ());
Jfreechart chart = new Jfreechart ("Data processing results statistic graph", Jfreechart.default_title_font, Plot, true);
Chart.settitle (New Texttitle ("Data Processing results Statistics graph", New Font ("bold", Font.Bold, 15));
Standardlegend SL = (standardlegend) chart.getlegend ();
Sl.setitemfont (New Font ("bold", Font.truetype_font, 12));
Chart.setbackgroundpaint (New Java.awt.Color (254, 254, 141));
//Because Jfreechart to save the generated PNG file in the system's temporary folder, you need to be in the appropriate
//Time call Session.removeattribute ("Jfreechart_deleter"), so that the PNG text can be guaranteed
//pieces are deleted
Chartrenderinginfo info = new Chartrenderinginfo (new Standardentitycollection ());
filename = servletutilities.savechartaspng (chart, W, h, info, session);
//Write the image map to the PrintWriter
chartutilities.writeimagemap (PW, filename, info); Pw.flush ();
} catch (Exception e)
{
System.out.println ("Exception-" + e.tostring ());
E.printstacktrace (System.out);
filename = "Public_error_500x300.png";
}
return filename;
}
}