Implementation of graph report line chart by JavaWeb
This example describes how to implement graph report line chart on JavaWeb. We will share this with you for your reference. The details are as follows:
Steps:
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 into 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 defacategcategorydataset 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 wPhoto, int hPhoto) {String filename = null; try {final JFreeChart chart = ChartFactory. createLineChart (title, // chart title wfield, // hfield of the horizontal axis display label, // dataset of the vertical axis display label, // dataset PlotOrientation. VERTICAL, // chart direction: horizontal and VERTICAL true, // whether to display the legend true, // whether to generate the prompt tool tooltips false // whether to generate a URL link ); standardLegend legend = (StandardLegend) chart. getLegend (); // generate the legend. setDisplaySeriesShapes (true); // display the legend shape legend. setShapeScaleX (1.5); // set the legend of the X axis of the legend. setShapeScaleY (1.5); // set the legend of the Y axis of the legend. setDisplaySeriesLines (true); // display the horizontal line of the graph item // set the background color chart of the image. setBackgroundPaint (new java. awt. color (189,235,255); CategoryPlot plot = (CategoryPlot) chart. getPlot (); plot. setBackgroundPaint (new Color (239,251,255); // generates the background Color of the wall in the image. setRangeGridlinePaint (Color. black); // generate the color of the grid Child Line in the image // The message plot displayed when no data exists. setNoDataMessage ("no statistical data available"); plot. setNoDataMessageFont (new Font ("", Font. CENTER_BASELINE, 16); plot. setNoDataMessagePaint (Color. RED); NumberAxis rangeAxis = (NumberAxis) plot. getRangeAxis (); rangeAxis. setStandardTickUnits (NumberAxis. createIntegerTickUnits (); // sets the unit of the dial line in the graph rangeAxis. setAutoRangeIncludesZero (true); // Force 0 LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot in the automatically selected data range. getRenderer (); renderer. setDrawShapes (true); // the data points of a broken line use different shapes renderer based on classification. setItemLabelsVisible (true); // a prompt tool that displays the data value on each vertex. Can the data tag see ChartRenderingInfo info = new ChartRenderingInfo (new StandardEntityCollection ()? // 500 is the image length, the Image Height of 300 is 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 and add the following content to 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 the following content to the jsp:
<% @ 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 program design.