Jfreechart fire rate application

Source: Internet
Author: User

In terms of charts in Java, jfreechart is a very good boss. You can refer to the download of a specific English manual class library on the Internet.
Let's give a simple introduction to the example of voting.

First, you can output the voting result to the page in the form of a servlet. The first step is to accept parameters (for example, the type of the image to be displayed, the column chart, the pie chart, or other types of images ),
Input the dataset, set the output type, and output the image.

Import org. jfree. Chart. chartfactory;
Import org. jfree. Chart. chartutilities;
Import org. jfree. Chart. jfreechart;
Import org. jfree. Chart. Plot. plotorientation;
Import org. jfree. Data. Category. defaultcategorydataset;
Import org. jfree. Data. General. defaultpiedataset;

Ublic class chartservlet extends httpservlet
{

Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception
{

// Obtain a vote object instance first.
Vote vote = getvote ();


// Obtain the jfreechart object instance
Jfreechart chart = getchart (vote );

// Set the output object type
Response. setcontenttype ("image/JPEG ");

// Output image
Chartutilities. writechartasjpeg (response. getoutputstream (),
100, chart, 400,300, null );
}
 
 
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception
{
Dopost (request, response );
}

// Obtain the jfreechart object instance
Public static jfreechart getchart (Vote vote)
{
Jfreechart chart = NULL;

// Determine whether it is a pie chart
If (....)
{
// Organize and generate pie chart data
Defaultpiedataset DATA = new defaultpiedataset ();

Iterator it = vote. getvoteitems (). iterator ();

// Traverse the dataset
While (it. hasnext ())
{
Voteitem Vi = (voteitem) it. Next ();
// Set the title of each item in the image. The specific number (here is the number of votes VI. getvotenum)
Data. setvalue (VI. gettitle (), VI. getvotenum ());
}

// Determine whether it is a normal pie chart
If (.....)
{
Chart = chartfactory. createpiechart (vote. gettitle (),
Data, false );
}
Else // 3D pie chart
{
Chart = chartfactory. createpiechart3d (vote. gettitle (),
Data, false );
}
}
Else
{
// Organize the data of the column chart
Defaultcategorydataset DATA = new defaultcategorydataset ();

Iterator it = vote. getvoteitems (). iterator ();
While (it. hasnext ())
{
Voteitem Vi = (voteitem) it. Next ();
Data. addvalue (VI. getvotenum (), "option", Vi. gettitle ());
}

// Determine whether it is a normal bar chart
If (....)
{
Chart = chartfactory. createbarchart (vote. gettitle (),
"Option", "quantity", Data, plotorientation. Vertical,
False, false, false );
}
Else // 3D bar chart
{
Chart = chartfactory. createbarchart3d (vote. gettitle (),
"Option", "quantity", Data, plotorientation. Vertical,
False, false, false );
}
}
Return chart;
}
}

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.