Jsp JFreeChart to generate data pie implementation code

Source: Internet
Author: User
The code is as follows: Copy code

 
WebChart. java

Package net. sentom. chart;
       
Import java. awt. Insets;
Import java. awt. Font;
Import java. io. PrintWriter;
Import javax. servlet. http. HttpSession;
Import org. jfree. data .*;
Import org. jfree. chart .*;
Import org. jfree. chart. plot .*;
Import org. jfree. chart. entity .*;
Import org. jfree. chart. urls .*;
Import org. jfree. chart. servlet .*;
Import org. jfree. chart. labels. StandardPieToolTipGenerator;
       
/**
* @ Author sentom
        *
* To change the template for this generated type comment go
* Window> Preferences> Java> Code Generation> Code and Comments
*/
Public class WebChart {
Private defapipiedataset data = new DefaultPieDataset ();
       
Public void setValue (String key, double value ){
Data. setValue (key, value );
        }
       
Public String generatePieChart (String title, HttpSession session, PrintWriter pw ){
String filename = null;
Try {
// Create a chart object
PiePlot plot = new PiePlot (data );
Plot. setInsets (new Insets (0, 5, 5, 5 ));
// Create a link on the statistical Image
Plot. setURLGenerator (new StandardPieURLGenerator ("link. jsp", "section "));
Plot. setToolTipGenerator (new StandardPieToolTipGenerator ());
JFreeChart chart = new JFreeChart ("", JFreeChart. DEFAULT_TITLE_FONT,
       
Plot, true );
            
Chart. setBackgroundPaint (java. awt. Color. white); // you can specify the background Color of an image.
            
Font font = new Font ("", Font. CENTER_BASELINE, 20); // set the Font of the image title
       
And size
TextTitle _ title = new TextTitle (title );
_ Title. setFont (font );
Chart. setTitle (_ title );
       
// Put the generated image in a temporary directory
ChartRenderingInfo info = new ChartRenderingInfo (new
       
StandardEntityCollection ());
// 500 indicates the image length and 300 indicates the image height.
Filename = ServletUtilities. saveChartAsPNG (chart, 500,300, info,
       
Session );
       
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;
        }
        }
        

JSP file

 
<% @ Page contentType = "text/html; charset = GBK" %>
<% @ Page import = "java. io. PrintWriter" %>
<% @ Page import = "net. sentom. chart. WebChart" %>
<%
       
WebChart chart = new WebChart ();
Chart. setValue ("June", 500 );
Chart. setValue ("July", 580 );
Chart. setValue ("August", 828 );
       
String filename = chart. generatePieChart ("www.111cn.net website access statistics table", session, new
       
PrintWriter (out ));
String graphURL = request. getContextPath () + "/servlet/DisplayChart? Filename = "+ filename;
       
%>
<HTML>
<HEAD>
<TITLE> www.sentom.net </TITLE>
</HEAD>
<BODY>
<P align = "CENTER">
" width = 500 height = 300 border = 0 usemap = "# <% =
       
Filename %> ">
</P>
</BODY>
</HTML>

The latest version is 0.9.11, and the right side is a statistical chart generated by JFreeChart. This article explains how to use JFreeChart to generate pie chart on the Web.


Http://heanet.dl.sourceforge.net/sourceforge/jfreechart/jfreechart-0.9.11.zip
Http://heanet.dl.sourceforge.net/sourceforge/jfreechart/jfreechart-0.9.11.tar.gz

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.