Jfreechart generates different statistical charts

Source: Internet
Author: User
Tags dateformat

Package com. jfc. ww. servlet;

Import java. AWT. color;
Import java. AWT. Font;
Import java. Io. file;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import java. Text. dateformat;
Import java. Text. decimalformat;
Import java. Text. numberformat;
Import java. Text. simpledateformat;

Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;

Import org. jfree. Chart. chartfactory;
Import org. jfree. Chart. chartframe;
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. axis. dateaxis;
Import org. jfree. Chart. axis. datetickunit;
Import org. jfree. Chart. axis. datetickunittype;
Import org. jfree. Chart. axis. numberaxis;
Import org. jfree. Chart. axis. valueaxis;
Import org. jfree. Chart. entity. standardentitycollection;
Import org. jfree. Chart. Labels. itemlabelanchor;
Import org. jfree. Chart. Labels. itemlabelposition;
Import org. jfree. Chart. Labels. standardcategoryitemlabelgenerator;
Import org. jfree. Chart. Labels. standardpiesectionlabelgenerator;
Import org. jfree. Chart. Plot. categoryplot;
Import org. jfree. Chart. Plot. pieplot;
Import org. jfree. Chart. Plot. plotorientation;
Import org. jfree. Chart. Plot. xyplot;
Import org. jfree. Chart. Renderer. Category. barrenderer;
Import org. jfree. Chart. Renderer. Category. barrenderer3d;
Import org. jfree. Chart. Renderer. Category. lineandshaperenderer;
Import org. jfree. Chart. servlet. servletutilities;
Import org. jfree. Chart. Title. legendtitle;
Import org. jfree. Chart. Title. texttitle;
Import org. jfree. Data. Category. categorydataset;
Import org. jfree. Data. Category. defaultcategorydataset;
Import org. jfree. Data. General. datasetgroup;
Import org. jfree. Data. General. datasetutilities;
Import org. jfree. Data. General. defaultpiedataset;
Import org. jfree. Data. Time. Day;
Import org. jfree. Data. Time. Timeseries;
Import org. jfree. Data. Time. timeseriescollection;
Import org. jfree. Data. XY. xydataset;
Import org. jfree. Date. serialdateutilities;
Import org. jfree. UI. textanchor;

Public class jfreecharservilet extends httpservlet {
 

Public void doget (httpservletrequest request, httpservletresponse response ){
Response. setcontenttype ("text/html ");
This. dopost (request, response );
}

 
Public void dopost (httpservletrequest request, httpservletresponse response ){
Response. setcontenttype ("text/html ");
Httpsession session = request. getsession ();
String filetype = request. getparameter ("filetype ");
String filename = NULL;
If (filetype. Equals ("pie ")){
Filename = This. pietest (session );
} Else if (filetype. Equals ("bar ")){
Filename = This. bartest (session );
} Else if (filetype. Equals ("stackedbar ")){
Filename = This. stackedbartest (session );
} Else if (filetype. Equals ("line ")){
Filename = This. linetest (session );
} Else if (filetype. Equals ("Timeseries ")){
Filename = This. timeseriestest (session );
}
Request. setattribute ("FILENAME", filename );
Try {
Request. getrequestdispatcher ("content. jsp"). Forward (request, response );
} Catch (servletexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
 
// Pie chart
Public String pietest (httpsession session ){
String filename = NULL;
Defaultpiedataset DFD = new defaultpiedataset ();
DFD. setvalue ("Zhang San", 10 );
DFD. setvalue ("Li Si", 15 );
DFD. setvalue ("CCC", 30 );
DFD. setvalue ("DDD", 25 );
DFD. setvalue ("eee", 5 );
Jfreechart jfc = chartfactory. createpiechart3d ("pie chart Statistics", DFD, true );
Jfc. gettitle (). setfont (new font ("", Font. Bold, 20); // set the title Font to prevent Chinese garbled characters
Pieplot plot = (pieplot) jfc. getplot (); // set the font in the pie to prevent Chinese garbled characters
Plot. setlabelfont (new font ("", Font. Bold, 20 ));
Plot. setnodatamessage ("no data ");
Plot. setcircular (true );
Plot. setlabelgenerator (New standardpiesectionlabelgenerator ("{0} {2}", numberformat. getnumberinstance (), new decimalformat ("0.00% ")));
Plot. setforegroundalpha (0.7f); // transparency
Legendtitle = jfc. getlegend (0); // set the low-column font to prevent Chinese garbled characters
Legendtitle. setitemfont (new font ("", Font. italic, 20 ));
Chartrenderinginfo info = new chartrenderinginfo (New standardentitycollection ());
Try {
Filename = servletutilities. savechartaspng (jfc, 800,500, info, session );
} Catch (ioexception e ){
E. printstacktrace ();
}
Return filename;
}
 
 
// Bar chart
Public String bartest (httpsession session ){
String filename = NULL;
Defaultcategorydataset dataset = new defaultcategorydataset ();
Dataset. addvalue (300, "Guangzhou", "apple ");
Dataset. addvalue (200, "Guangzhou", "Pears ");
Dataset. addvalue (500, "Guangzhou", "grape ");
Dataset. addvalue (340, "Guangzhou", "Mango ");
Dataset. addvalue (280, "Guangzhou", "litchi ");
Jfreechart jfc = chartfactory. createbarchart3d ("bar chart Statistics", "Fruit", "sales", dataset, plotorientation. Vertical, true );
Jfc. settitle (New texttitle ("bar chart Statistics", new font ("", Font. Bold + font. italic, 20 )));
Categoryplot plot = (categoryplot) jfc. getplot ();
Categoryaxis domainaxis = plot. getdomainaxis ();
Domainaxis. setvisible (true );
Domainaxis. setlowermargin (0.1 );
Domainaxis. setuppermargin (0.1 );
Domainaxis. setlabelfont (new font ("", Font. Bold, 12); // horizontal axis coordinate title Font
Domainaxis. setticklabelfont (new font ("", Font. Bold, 12); // horizontal bar title Font
Numberaxis = (numberaxis) plot. getrangeaxis ();
Numberaxis. setlabelfont (new font ("", Font. Bold, 12); // font of the vertical axis coordinate title
Valueaxis = plot. getrangeaxis ();
Valueaxis. setpositivearrowvisible (true); // whether to display the Y axis coordinates. Only the headers are required.
Barrenderer Renderer = (barrenderer) plot. getrenderer ();
// Display the value of each column and modify the font attribute of the value
Renderer. setitemlabelgenerator (New standardcategoryitemlabelgenerator ());
Renderer. setitemlabelfont (new font ("", Font. Plain, 9 ));
Renderer. setitemlabelsvisible (true );
Plot. setrenderer (Renderer );
Plot. setforegroundalpha (0.8f); // transparency
Legendtitle = jfc. getlegend (0); // set the low-column font to prevent Chinese garbled characters
Legendtitle. setitemfont (new font ("", Font. italic, 20 ));
Try {
Filename = servletutilities. savechartaspng (jfc, 800,500, null, session );
} Catch (ioexception e ){
E. printstacktrace ();
}
Return filename;
}
 
 
// Heap column chart
Public String stackedbartest (httpsession session ){
String filename = NULL;
Double [] [] DATA = new double [] [] {365,321,257,156,147}, {369,214,587,263,666}, {236,987,546,258,418}, {369,258,147,456,122}, {759,153,751,359,951 }};
String [] rowkeys = {"apple", "Pear", "grape", "banana", "orange "};
String [] columnkeys = {"Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Dalian "};
Categorydataset dataset = datasetutilities. createcategorydataset (rowkeys, columnkeys, data );
Jfreechart jfc = chartfactory. createstackedbarchart3d ("heap bar chart Statistics", "Fruit", "sales", dataset, plotorientation. Vertical, true );
Jfc. settitle (New texttitle ("heap bar chart Statistics", new font ("", Font. italic + font. italic, 20 )));
Categoryplot plot = (categoryplot) jfc. getplot ();
Categoryaxis domainaxis = plot. getdomainaxis ();
Domainaxis. setvisible (true );
Domainaxis. setlowermargin (0.05 );
Domainaxis. setuppermargin (0.05 );
Domainaxis. setlabelfont (new font ("", Font. Bold, 12); // horizontal axis coordinate title Font
Domainaxis. setticklabelfont (new font ("", Font. Bold, 12); // horizontal bar title Font
Numberaxis = (numberaxis) plot. getrangeaxis ();
Numberaxis. setlabelfont (new font ("", Font. Bold, 12); // font of the vertical axis coordinate title
Valueaxis = plot. getrangeaxis ();
Valueaxis. setpositivearrowvisible (true); // whether to display the vertical axis coordinates pointing to the scissors
Barrenderer Renderer = new barrenderer3d ();
Renderer. setmaximumbarwidth (0.5); // The width of the bar.
Renderer. setitemmargin (0.05); // group column Interval
// Display the value of each column and modify the font attribute of the value
Renderer. setitemlabelgenerator (New standardcategoryitemlabelgenerator ());
Renderer. setitemlabelfont (new font ("", Font. Bold, 8); // bold on the 6 th
Renderer. setitemlabelpaint (color. Black); // The font is black.
Renderer. setitemlabelsvisible (true );
// Display the value of each column and modify the font attribute of the value
Renderer. setitemmargin (0.01 );
Plot. setrenderer (Renderer );
Plot. setforegroundalpha (0.8f); // transparency
Legendtitle = jfc. getlegend (0); // set the low-column font to prevent Chinese garbled characters
Legendtitle. setitemfont (new font ("", Font. italic, 20 ));
Try {
Filename = servletutilities. savechartaspng (jfc, 800,500, null, session );
} Catch (ioexception e ){
E. printstacktrace ();
}
Return filename;
}
 
// Line chart
Public String linetest (httpsession session ){
String filename = NULL;
Defaultcategorydataset linedataset = new defaultcategorydataset ();
// Curve name
String Series1 = "refrigerator ";
String series2 = "Color TV ";
String series3 = "washing machine ";

// Horizontal axis name (column name)
String type1 = "January ";
String type2 = "February ";
String type3 = "March ";
String type4 = "April ";
String type5 = "May ";
String type6 = "June ";

Linedataset. addvalue (0.0, Series1, type1 );
Linedataset. addvalue (4.2, Series1, type2 );
Linedataset. addvalue (3.9, Series1, type3 );
Linedataset. addvalue (3.5, Series1, type4 );
Linedataset. addvalue (5.4, Series1, type5 );
Linedataset. addvalue (8.0, Series1, type6 );

Linedataset. addvalue (1.0, series2, type1 );
Linedataset. addvalue (5.2, series2, type2 );
Linedataset. addvalue (7.9, series2, type3 );
Linedataset. addvalue (2.2, series2, type4 );
Linedataset. addvalue (9.0, series2, type5 );
Linedataset. addvalue (3.5, series2, type6 );

Linedataset. addvalue (2.0, series3, type1 );
Linedataset. addvalue (9.2, series3, type2 );
Linedataset. addvalue (8.9, series3, type3 );
Linedataset. addvalue (5.4, series3, type4 );
Linedataset. addvalue (4.1, series3, type5 );
Linedataset. addvalue (2.8, series3, type6 );

Jfreechart jfc = chartfactory. createlinechart ("line chart", "time", "sales (millions)", linedataset, plotorientation. Vertical, true, true, false );
Jfc. settitle (New texttitle ("line chart Statistics", new font ("", Font. Bold + font. italic, 20 )));
Categoryplot plot = (categoryplot) jfc. getplot ();
Categoryaxis domainaxis = plot. getdomainaxis ();
Domainaxis. setvisible (true );
Domainaxis. setlowermargin (0.05 );
Domainaxis. setuppermargin (0.05 );
Domainaxis. setlabelfont (new font ("", Font. Bold, 12); // horizontal axis coordinate title Font
Domainaxis. setticklabelfont (new font ("", Font. Bold, 12); // horizontal bar title Font
Numberaxis = (numberaxis) plot. getrangeaxis ();
Numberaxis. setlabelfont (new font ("", Font. Bold, 12); // font of the vertical axis coordinate title
Numberaxis. setstandardtickunits (numberaxis. createintegertickunits ());
Numberaxis. setautorangeincludeszero (true );
Numberaxis. setuppermargin (0.10 );
Numberaxis. setlabelangle (math. PI/2.0 );
Valueaxis = plot. getrangeaxis ();
Valueaxis. setpositivearrowvisible (true); // whether to display the vertical axis coordinates pointing to the scissors
Legendtitle = jfc. getlegend (0); // set the low-column font to prevent Chinese garbled characters
Legendtitle. setitemfont (new font ("", Font. italic, 20 ));
// Set the discount point to be highlighted
Lineandshaperenderer Renderer = (lineandshaperenderer) plot. getrenderer ();
Renderer. setshapesvisible (true );
Renderer. setdrawoutlines (true );
Renderer. setusefillpaint (true );
Renderer. setfillpaint (Java. AWT. color. White );
// Display the discount value
Renderer. setitemlabelgenerator (New standardcategoryitemlabelgenerator ());
Renderer. setitemlabelfont (new font ("", Font. Bold, 10); // bold on the 6 th
Renderer. setitemlabelpaint (color. Black); // The font is black.
Renderer. setitemlabelsvisible (true );
Try {
Filename = servletutilities. savechartaspng (jfc, 800,500, null, session );
} Catch (ioexception e ){
E. printstacktrace ();
}
Return filename;
}
 
// Time sequence diagram
Public String timeseriestest (httpsession session ){
String filename = NULL;
Xydataset timeseriescollection = NULL;
Timeseries = new Timeseries ("data ");
Day = New Day (2010,); // instantiate day
Double D = 3000d;
// Add 365 days of data per year
For (INT I = 0; I <365; I ++ ){
D = d + (math. Random ()-0.5) * 10; // create random data
Timeseries. Add (day, d); // Add data to the dataset
Day = (day) day. Next ();
}
// Create a timeseriescollection object
Timeseriescollection = new timeseriescollection (Timeseries );
Jfreechart jfc = chartfactory. createtimeserieschart ("Sequence Chart Statistics", "sales month", "sales volume (parts)", timeseriescollection, false );
Jfc. settitle (New texttitle ("Sequence Chart Statistics", new font ("", Font. Bold + font. italic, 20 )));
// Set the title Font
Jfc. gettitle (). setfont (new font ("", Font. Bold, 26 ));
// Jfc. setbackgroundpaint (new color (252,175,222); // sets the background color.
Xyplot plot = jfc. getxyplot (); // obtain the chart attributes
Plot. setdomaingridlinesvisible (true); // you can specify that the grid is not displayed.
Dateaxis = (dateaxis) plot. getdomainaxis (); // obtain the timeline object
Dateaxis. setlabelfont (new font ("", Font. italic, 18); // set the timeline font
Dateaxis. setticklabelfont (new font ("", Font. Plain, 12); // set the font of the timeline ruler Value
Dateaxis. setlowermargin (0.0); // you can specify the minimum value displayed on the timeline.
Valueaxis = plot. getrangeaxis (); // obtain the data axis object
Valueaxis. setlabelfont (new font ("", Font. italic, 18); // set the data font
Dateformat format = new simpledateformat ("mm Month"); // create Date Format object
Datetickunit DTU = new datetickunit (datetickunittype. Day, 29, format); // create a datetickunit object
Dateaxis. settickunit (DTU); // you can specify a date tag for a date axis.
Try {
Filename = servletutilities. savechartaspng (jfc, 800,500, null, session );
} Catch (ioexception e ){
E. printstacktrace ();
}
Return filename;
}
 

 

 

/**
Public static void main (string [] ARGs ){
Xydataset timeseriescollection = NULL;
Timeseries = new Timeseries ("data ");
Day = New Day (2010,); // instantiate day
Double D = 3000d;
// Add 365 days of data per year
For (INT I = 0; I <365; I ++ ){
D = d + (math. Random ()-0.5) * 10; // create random data
Timeseries. Add (day, d); // Add data to the dataset
Day = (day) day. Next ();
}
// Create a timeseriescollection object
Timeseriescollection = new timeseriescollection (Timeseries );
Jfreechart jfc = chartfactory. createtimeserieschart ("Sequence Chart Statistics", "sales month", "sales volume (parts)", timeseriescollection, false );
Jfc. settitle (New texttitle ("Sequence Chart Statistics", new font ("", Font. Bold + font. italic, 20 )));
// Set the title Font
Jfc. gettitle (). setfont (new font ("", Font. Bold, 26 ));
// Jfc. setbackgroundpaint (new color (252,175,222); // sets the background color.
Xyplot plot = jfc. getxyplot (); // obtain the chart attributes
Plot. setdomaingridlinesvisible (true); // you can specify that the grid is not displayed.
Dateaxis = (dateaxis) plot. getdomainaxis (); // obtain the timeline object
Dateaxis. setlabelfont (new font ("", Font. italic, 18); // set the timeline font
Dateaxis. setticklabelfont (new font ("", Font. Plain, 12); // set the font of the timeline ruler Value
Dateaxis. setlowermargin (0.0); // you can specify the minimum value displayed on the timeline.
Valueaxis = plot. getrangeaxis (); // obtain the data axis object
Valueaxis. setlabelfont (new font ("", Font. italic, 18); // set the data font
Dateformat format = new simpledateformat ("mm Month"); // create Date Format object
Datetickunit DTU = new datetickunit (datetickunittype. Day, 29, format); // create a datetickunit object
Dateaxis. settickunit (DTU); // you can specify a date tag for a date axis.


Chartframe cf = new chartframe ("jfreechar test", jfc, true );
Cf. Pack ();
Cf. setvisible (true );


}

 
**/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Receiving page

</Head>
<%
String filename = (string) request. getattribute ("FILENAME ");
String graphurl = request. getcontextpath () + "/servlet/displaychart? Filename = "+ filename;
%>
<Body>

</Body>
</Html>

 

 

 

 

 

 

 

 

 

XML file configuration

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

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.