About jfreechart forming pie chart and column chart

Source: Internet
Author: User

For a project, I found a jfreechart on the Internet to form a pie chart, column chart, and so on.

1. Introduction to jfreechart

Jfreechart is an open-source Java project. It is mainly used to develop various charts, including pie charts, bar charts (common bar charts and stack bar charts), line chart, Area Chart, distribution chart, hybrid chart, Gantt chart, and some dashboard. These charts can meet the requirements of the current business system. Jfreechart is a chart Development Technology Based on the Java language. Jfreechart can be used in servlet, JSP, applet, and Java APPICATION environments to dynamically display any database data through JDBC, and can be output to PDF files with itext.

Note: Some of the required jfreechart packages can be found inHttp://sourceforge.net/project/showfiles.php(Jfreechart official website) download

2. The following is an example of creating a pie chart and a column chart:

1> writing in the class:

Import java. AWT. color;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import java. Io. Writer;
Import java. SQL. sqlexception;
Import java. Io .*;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
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. Data. Category. defaultcategorydataset;
Import org. jfree. Data. General. defaultpiedataset;
Import com. Sun. rowset. cachedrowsetimpl;

Public class qshuju1 {
DB = new dB (); // class for connecting to the database
Cachedrowsetimpl CRS;
Public defaultpiedataset getdataset () {// value from the database to form a data group with a pie chart
Defaultpiedataset DATA = new defaultpiedataset ();
Try {
CRS = new cachedrowsetimpl ();
CRS = dB. dbquery ("select * From t_tongjitu ");
While (CRS. Next ()){
String name = new string (CRS. getstring ("name"). Trim ()));
String shuliang = new string (CRS. getstring ("shuliang"). Trim ()));
Float SL = float. parsefloat (shuliang );
Data. setvalue (name, SL );
System. Out. println (name + "***********" + SL );
}
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return data;
}
Public defaultcategorydataset getdatacategory () {// value from the database to form a column chart data group

Defaultcategorydataset DATA = new defaultcategorydataset ();
Try {
CRS = new cachedrowsetimpl ();
CRS = dB. dbquery ("select * From t_tongjitu ");
While (CRS. Next ()){
String name = new string (CRS. getstring ("name"). Trim ()));
String shuliang = new string (CRS. getstring ("shuliang"). Trim ()));
Float SL = float. parsefloat (shuliang );
Data. setvalue (SL, name, "Number of workers ");
System. Out. println (name + "***********" + SL );
}
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return data;
}
Public String getbingurl (httpservletrequest request, httpservletresponse response) {// create a pie chart
String filename = NULL;
Try {
Pieplot3d plot = new pieplot3d (getdataset (); // generate a 3D pie chart
// Plot. seturlgenerator (New standardpieurlgenerator ("degreedview. jsp"); // sets the image Link
Jfreechart chart = new jfreechart ("", jfreechart. default_title_font, plot, true );
Chart. setbackgroundpaint (Java. AWT. color. White); // optional, set the background color of the image.
Chart. settitle ("programmer's degree questionnaire-by Alpha"); // optional, set the picture title
Plot. settooltipgenerator (New standardpietooltipgenerator ());
Standardentitycollection sec = new standardentitycollection ();
Chartrenderinginfo info = new chartrenderinginfo (SEC );
// Printwriter W = new printwriter (out); // output map information
Httpsession session = request. getsession ();
Filename = servletutilities. savechartaspng (chart, 500,300, info, session); // 500 indicates the image length, and 300 indicates the Image Height.
// String filename = servletutilities. savechartasjpeg (chart, 500,300, info, session );
// Chartutilities. writeimagemap (W, "map0", info, false );
String graphurl = request. getcontextpath () + "/servlet/displaychart? Filename = "+ filename;
// Formed image address
Return graphurl;
} Catch (exception e ){
System. Out. println ("Exception !!! ");
}

Return NULL;
}
Public String getzhuurl (httpservletrequest request, httpservletresponse response) {// form a column listing
String filename = NULL;
String title = "Programmer degree questionnaire-by Alpha ";
Try {
// Jfreechart chart = chartfactory. createbarchart3d ("Programmer Degree survey-by Alpha", "Programmer Degree survey-by Alpha", "Number of Students", getdataset (), plotorientation. vertical, true, false, false );
Jfreechart chart = chartfactory. createbarchart3d (title, "Programmer Degree survey-by Alpha", "Number of Students", getdatacategory (), plotorientation. Vertical, true, false, false );
// Chartfactory. createbarchart3d ), generate a tool or URL link );
Httpsession session = request. getsession ();
Filename = servletutilities. savechartaspng (chart, 400,200, null, session );
String graphurl = request. getcontextpath () + "/servlet/displaychart? Filename = "+ filename;
// Formed image address
Return graphurl;
} Catch (exception e ){
System. Out. println ("Exception !!! ");
}
Return NULL;
}

}
2> JSP page writing
<JSP: usebean id = "QSJ" class = "com. qshuju1"/>

<Body>
<P align = "center">
<br>

</P> </body>

3> Web. xml configuration

Because I have never touched on it before, the search on the Internet is not comprehensive, and the error caused by web. XML is a waste of half a day to solve it !!!

<Web-app>

<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>

</Web-app>

 

 

Note: The above column chart will contain garbled characters. Please refer to my other article.

Http://www.cn-java.com/www1/index.php? Uid-565002-action-viewspace-itemid-10678

Or

Http://blog.csdn.net/zhaotao_king/archive/2008/12/26/3615096.aspx

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.