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