This paper illustrates the method of Javaweb to realize the line chart of graph report. Share to everyone for your reference, specific as follows:
Step Description:
1. Import Log4j.jar,jfreechart-0.9.18.jar,jdom.jar,jcommon-0.9.3.jar four jar packages
2. Write the Tuxin.java class in a package
Package com.mengya.util;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.io.PrintWriter;
Import javax.servlet.http.HttpSession;
Import Org.jfree.chart.ChartFactory;
Import Org.jfree.chart.ChartRenderingInfo;
Import org.jfree.chart.ChartUtilities;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.StandardLegend;
Import Org.jfree.chart.axis.NumberAxis;
Import org.jfree.chart.entity.StandardEntityCollection;
Import Org.jfree.chart.plot.CategoryPlot;
Import org.jfree.chart.plot.PlotOrientation;
Import Org.jfree.chart.renderer.LineAndShapeRenderer;
Import org.jfree.chart.servlet.ServletUtilities;
Import Org.jfree.data.DefaultCategoryDataset;
public class Line {private Defaultcategorydataset DataSet = new Defaultcategorydataset ();
public void setValue (int sum, string line, String Wfield) {dataset.addvalue (sum, line, Wfield); public string Generatelinechart (String title,string Wfield, String Hfield, HttpSession session, PrintWriter Pw,int WPH
Oto,int Hphoto) {String filename = null; try {final Jfreechart chart = Chartfactory.createlinechart (title,///Chart title Wfield,//Horizontal axis display label
Check Hfield,//Vertical Display tab DataSet,//DataSet plotorientation.vertical,//Chart direction: Horizontal, vertical true,//show Legend
True,//whether to generate the hint tool tooltips false//whether to generate a URL link); Standardlegend legend = (standardlegend) chart.getlegend ();//Generate Legend Legend.setdisplayseriesshapes (TRUE);//Show Legend shape L Egend.setshapescalex (1.5)//Set the legend x axis size Legend.setshapescaley (1.5);//Set the size of the legend Y-axis Legend.setdisplayserieslines (TRUE);
Displays the horizontal//setting of the picture's background color chart.setbackgroundpaint (new Java.awt.Color (189,235,255));
Categoryplot plot = (Categoryplot) chart.getplot (); Plot.setbackgroundpaint (new color (239,251,255))//Create the background color plot.setrangegridlinepaint (color.black) of the wall in the picture;
Generate the color of the grid line in the picture//Message Plot.setnodatamessage ("No relevant statistics") displayed when there is no data;
Plot.setnodatamessagefont (New Font ("bold", Font.center_baseline, 16)); Plot.setnodatamessagepaint (color.red);
Numberaxis Rangeaxis = (numberaxis) plot.getrangeaxis ();
Rangeaxis.setstandardtickunits (Numberaxis.createintegertickunits ());//Set the units of tick marks in the diagram Rangeaxis.setautorangeincludeszero (TRUE);//force contains 0 lineandshaperenderer renderer = (lineandshaperenderer) in the automatically selected range of data
Plot.getrenderer ();
Renderer.setdrawshapes (TRUE);//The data points of the polyline use different shapes renderer.setitemlabelsvisible (true) according to the taxonomy;//the Hint tool for displaying data values on each point, whether the data labels are visible
Chartrenderinginfo info = new Chartrenderinginfo (new Standardentitycollection ());
500 is the picture length, 300 is the picture height filename = servletutilities.savechartaspng (chart, Wphoto, Hphoto, info, session);
Chartutilities.writeimagemap (PW, filename, info);
Pw.flush ();
catch (Exception e) {e.printstacktrace ();
return filename;
}
}
3. Configure Web.xml to add the following null 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>
4. Add content to the JSP where you want to display the picture:
<%@ page language= "java" import= "Java.util.*,com.mengya.util.line,com.mengya.bean.stubean" pageEncoding= "GBK"% > <% @page import= "Java.io.PrintWriter;" %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
I hope this article will help you with JSP programming.