Set in 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>
Methods in java classes:
Public String (String series [], double score [] [], String type [], String name ){
Final int num = 8;
DefaultCategoryDataset defacategcategorydataset = new defaultcategorydataset ();
For (int I = 0; I <type. length; I ++ ){
Type [I] = type [I]. substring (0, (type [I]. length () <num )? Type [I]. length (): num );
}
For (int j = 0; j <series. length; j ++ ){
Int I = 0;
For (I = 0; I <type. length; I ++ ){
Defaultcategorydataset. addValue (score [j] [I], series [j], type [I]);
}
}
JFreeChart jfreechart = ChartFactory. createLineChart (name, null, null, defaultcategorydataset, PlotOrientation. VERTICAL, true, true, false );
Jfreechart. getLegend (). setPosition (RectangleEdge. RIGHT );
Jfreechart. setBackgroundPaint (Color. white );
CategoryPlot categoryplot = (CategoryPlot) jfreechart. getPlot ();
Categoryplot. setNoDataMessage ("No data available for display! ");
Categoryplot. setBackgroundPaint (Color. white );
Categoryplot. setDomainGridlinesVisible (true );
Categoryplot. setRangeGridlinesVisible (true );
Categoryplot. setRangeGridlinePaint (Color. gray );
Categoryplot. setDomainGridlinePaint (Color. gray );
Categoryplot. setBackgroundAlpha (0.8f );
Font font1 = new Font ("", Font. BOLD, 14 );
Jfreechart. getTitle (). setFont (font1 );
Font font3 = new Font ("", Font. BOLD, 12 );
Jfreechart. getLegend (). setItemFont (font3 );
CategoryAxis categoryaxis = categoryplot. getDomainAxis ();
// Categoryaxis. setCategoryLabelPositions (CategoryLabelPositions. UP_45 );
Categoryaxis. setMaximumCategoryLabelLines (10); // number of rows, set as needed
Categoryaxis. setMaximumCategoryLabelWidthRatio (0.5f); // The width of each line. Here, set a Chinese character width.
NumberAxis numberaxis = (NumberAxis) categoryplot. getRangeAxis ();
Numberaxis. setStandardTickUnits (NumberAxis. createIntegerTickUnits ());
Numberaxis. setAutoRangeIncludesZero (true );
Numberaxis. setRangeWithMargins (0, 3 );
Numberaxis. setUpperMargin (0.8); // you can specify the distance between the top of an Item and the image.
Numberaxis. setUpperBound (3.5); // maximum ordinate Value
Categoryaxis. setTickLabelFont (new Font ("", Font. BOLD, 12 ));
Numberaxis. setTickLabelFont (new Font ("", Font. BOLD, 12 ));
Font font2 = new Font ("SimSun", Font. BOLD, 16 );
Categoryaxis. setLabelFont (font2 );
Numberaxis. setLabelFont (font2 );
Categoryplot. setAxisOffset (new RectangleInsets (0D, 0D, 0D, 10D); // you can specify the distance between the curve and the xy axis.
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot. getRenderer ();
Lineandshaperenderer. setShapesVisible (true); // visible to data points
Lineandshaperenderer. setSeriesStroke (0, new BasicStroke (2.0F, 1, 1, 1.0F, new float [] {
10F, 6F
}, 0.0F); // defines the line between series points. The line is a dotted line. The default line is a straight line.
Lineandshaperenderer. setSeriesStroke (1, new BasicStroke (2.0F, 1, 1, 1.0F, new float [] {
6F, 6F
}, 0.0F ));
Lineandshaperenderer. setBaseItemLabelsVisible (true );
Lineandshaperenderer. setBaseItemLabelGenerator (new StandardCategoryItemLabelGenerator ());
ChartRenderingInfo info = new ChartRenderingInfo (new StandardEntityCollection ());
String fileName = null;
Try
{
FileName = ServletUtilities. saveChartAsPNG (jfreechart, 700,300, info, null); // generate an image
}
Catch (IOException e)
{
E. printStackTrace ();
}
String graphURL = "/projectname/DisplayChart? Filename = "+ fileName; // projectname indicates the path of the corresponding project, which is generally the project name.
// Use this in jsp: String graphURL = request. getContextPath () + "/DisplayChart? Filename = "+ filename;
Return graphURL; // return the address of the generated image.
}