Jfreechart graph, jfreechart
1. Download the JAR package
Official Website: http://sourceforge.net/projects/jfreechart/files/
2. Introduce the JAR package in lib
Jfreechart-x.x.xx.jar
Jcommon -- x. x. xx. jar
3. Configure web. xml
<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>/DisplayChart </url-pattern>
</Servlet-mapping>
4. source code example
/**
* @ Description: Obtain the height and age line chart.
* @ Param request information
* @ Return height and age line chart
* @ Date 11:37:37 AM, January 1, March 20, 2015
* @ Version 1.0
*/
@ RequestMapping ("getHeightAgeChart. do ")
@ ResponseBody
Public String createHeightAgeChart (HttpServletRequest request ){
String title = "height-age growth curve"; // report title, String type
String xtitle = "Age (month)"; // horizontal axis title
String rangTitle = "height (cm)"; // The name of the vertical (value) axis.
XYSeriesCollection dataset = new XYSeriesCollection ();
XYSeries s1 = new XYSeries ("James ");
S1.add (0, 48 );
S1.add (3,50 );
S1.add (6, 60 );
S1.add (8, 70 );
S1.add (12, 75 );
S1.add (18,82 );
S1.add (24, 88 );
S1.add (30,91 );
S1.add (36,96 );
Dataset. addSeries (s1 );
XYSeries s2 = new XYSeries ("Xiao Fang ");
S2.add (0, 50 );
S2.add (3,55 );
S2.add (6, 65 );
S2.add (8, 75 );
S2.add (12, 80 );
S2.add (18,87 );
S2.add (24,92 );
S2.add (30,98 );
S2.add (36,108 );
Dataset. addSeries (s2 );
Return ReportTool. XYSplineRendererGo (dataset, title, xtitle, rangTitle, request );
}
/**
* @ Description: curve chart tool class
* @ Date 1:38:59, January 1, March 23, 2015
* @ Version 1.0
*/
Public class ReportTool {
/**
* @ Description: curve chart tool Method
* @ Param dataset
* @ Param title name
* @ Param xtitle X axis name
* @ Param rangTitle Y axis name
* @ Param request information
* @ Return returns the chart URL.
* @ Date 1:46:32, January 1, March 23, 2015
* @ Version 1.0
*/
Public static String XYSplineRendererGo (XYSeriesCollection dataset, String title, String xtitle, String rangTitle, HttpServletRequest request ){
JFreeChart chart = ChartFactory. createXYLineChart (
Title, // report question, string type
Xtitle, // horizontal axis title
RangTitle, // vertical (value) axis name
Dataset, // dataset
PlotOrientation. VERTICAL,
True, // display the legend
False,
False
);
// Set the font of the chart title
Font font = new Font ("", Font. CENTER_BASELINE, 20 );
Chart. getTitle (). setFont (font );
// Set the legend font
Font xfont = new Font ("", Font. PLAIN, 12 );
Chart. getLegend (). setItemFont (xfont );
XYPlot plot = (XYPlot) chart. getPlot ();
ValueAxis domainAxis = plot. getDomainAxis ();
DomainAxis. setLowerMargin (0.1); // set the distance from the left end of the image to 10%
DomainAxis. setUpperMargin (0.1); // set the distance from the right side of the image to 10%.
DomainAxis. setLabelFont (xfont); // the title of the X axis.
DomainAxis. setTickLabelFont (xfont); // X axis Value
DomainAxis. setTickLabelPaint (Color. BLUE); // font Color
NumberAxis rangeAxis = (NumberAxis) plot. getRangeAxis ();
RangeAxis. setUpperMargin (0.1); // set the distance between the highest column and the top of the image (10% of the highest column)
RangeAxis. setLabelFont (xfont); // y axis title
RangeAxis. setTickLabelFont (xfont); // y axis Value
RangeAxis. setLabelPaint (Color. BLUE); // font Color
RangeAxis. setTickLabelFont (xfont );
NumberFormat numberformat = new DecimalFormat ("0 ");
RangeAxis. setNumberFormatOverride (numberformat); // you can specify the percentage of the Y axis.
// Smooth curve
XYItemRenderer renderer = new XYSplineRenderer ();
Renderer. setItemLabelPaint (Color. black); // The font is black.
Renderer. setItemLabelsVisible (true );
Renderer. setItemLabelFont (font );
// Percentage of collection points
// NumberFormat nfP = NumberFormat. getPercentInstance ();
// NfP. setMaximumFractionDigits (2); // percent format
// Renderer. setBaseItemLabelGenerator (new StandardXYItemLabelGenerator ("{2}", NumberFormat. getPercentInstance (), NumberFormat. getPercentInstance ()));
Plot. setRenderer (renderer );
String filename;
String graphURL = "";
Int width = 800;
Int height = 600;
Try {
Filename = ServletUtilities. saveChartAsJPEG (chart, width, height, request. getSession ());
GraphURL = request. getContextPath () + "/DisplayChart? Filename = "+ filename;
} Catch (IOException e ){
E. printStackTrace ();
}
Return graphURL;
}
}
The back-end returns the chart address string drawn as an image, which can be received by any element on the front-end.