Jfreechart is generated in the Java class to return the url address code share for the chart

  • 2020-04-01 02:14:25
  • OfStack

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

public String getChart(String series[],double score[][],String type[],String name){ 
final int num=8;
DefaultCategoryDataset defaultcategorydataset = 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(" blackbody ",Font.BOLD, 14);
jfreechart.getTitle().setFont(font1);
Font font3 = new Font(" Official script ",Font.BOLD, 12);
jfreechart.getLegend().setItemFont(font3);
CategoryAxis categoryaxis = categoryplot.getDomainAxis(); 
//  categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryaxis.setMaximumCategoryLabelLines(10);//Number of rows, as needed
categoryaxis.setMaximumCategoryLabelWidthRatio(0.5f);//The width of each line is one Chinese character wide
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
numberaxis.setRangeWithMargins(0, 3); 
numberaxis.setUpperMargin(0.8);////Sets the distance between the highest Item and the top of the picture
numberaxis.setUpperBound(3.5);//Vertical maximum
categoryaxis.setTickLabelFont(new Font(" Song typeface ", Font.BOLD, 12));
numberaxis.setTickLabelFont(new Font(" Official script ", 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));//Set the distance between the graph and the xy axis
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
lineandshaperenderer.setShapesVisible(true); //Data point visibility
lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
10F, 6F
}, 0.0F)); //Define the line between the series points, this is a dotted line, the default is a 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 images
} 
catch (IOException e) 
{ 
e.printStackTrace(); 
}
String graphURL = "/projectname/DisplayChart?filename=" + fileName; //Projectname is the path of the corresponding project, which is generally the name of the project
//String graphURL = request-getcontextpath () + "/servlet/DisplayChart? Filename = "+ filename;
return graphURL;//Returns the address of the generated image
}

Call the above method to get the url of the generated chart:

   getChart(stus,score_field,type," Total figure ");


Related articles: