Method of Realizing Line Graph of Graphic Report by JavaWeb

  • 2021-09-12 01:50:08
  • OfStack

In this paper, the method of realizing line chart of graphic report by JavaWeb is described with examples. Share it for your reference, as follows:

Step Description:

1. Import four jar packages: log4j. jar, jfreechart-0. 9.18. jar, jdom. jar, jcommon-0. 9.3. jar

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 wPhoto,int hPhoto)
  {
   String filename = null;
  try
   {
     final JFreeChart chart = ChartFactory.createLineChart
      (
       title, //  Chart title 
       wfield, //  Display label of horizontal axis 
       hfield, //  Display label of vertical axis 
       dataset, // Data set 
       PlotOrientation.VERTICAL, //  Chart Direction: Horizontal, Vertical 
       true, //  Display Legend 
       true, //  Whether to generate prompt tools  tooltips
       false //  Whether to generate URL Link 
     );
     StandardLegend legend = (StandardLegend) chart.getLegend();// Generate Legend 
     legend.setDisplaySeriesShapes(true);// Show Legend Shapes 
     legend.setShapeScaleX(1.5);// Set Legend X Size of Axis 
     legend.setShapeScaleY(1.5);// Set Legend Y Size of Axis 
     legend.setDisplaySeriesLines(true);// Displays the horizontal line of the icon item 
     // Set the background color of the picture 
     chart.setBackgroundPaint(new java.awt.Color(189,235,255));
     CategoryPlot plot = (CategoryPlot) chart.getPlot();
     plot.setBackgroundPaint(new Color(239,251,255));// Generate the background color of the wall in the picture 
     plot.setRangeGridlinePaint(Color.black);// Generate the color of grid lines in the picture 
     //  Message displayed when there is no data 
     plot.setNoDataMessage(" There are no relevant statistics ");
     plot.setNoDataMessageFont(new Font(" Blackbody ", 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 inclusion in the automatically selected data range 0
     LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
     renderer.setDrawShapes(true);// The data points of polygonal lines use different shapes according to classification 
     renderer.setItemLabelsVisible(true);// Prompt tool that displays the data value at each point , Is the data label visible 
     ChartRenderingInfo info = new ChartRenderingInfo(new
       StandardEntityCollection());
     //500 Is the length of the picture, 300 Is the height of the picture 
     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 inner space 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">
<html>
 <head>
 <title> Line chart </title>
 </head>
 <body>
 <%
  Line tt = new Line();
  // No. 1 1 Broken line 
  List ar = (ArrayList) request.getAttribute("arr1");
  for (int i = 0; i < ar.size(); i++) {
  StuBean bean = (StuBean) ar.get(i);
  tt.setValue((int) bean.getS_money(), " Zhang Mingxue's Personal Consumption Proportion Chart ", bean.getS_month()+ "");
  }
  // Article 2 Broken line 
  ar = (ArrayList) request.getAttribute("arr2");
  for (int i = 0; i < ar.size(); i++) {
  StuBean bean = (StuBean) ar.get(i);
  tt.setValue((int) bean.getS_money(), " Mengya Personal Consumption Proportion Chart ", bean.getS_month()+ "");
  }
  // Description :tt.setValue(int a,String b,String c)
  // Among them, the first 2 Parameters b Is the name of the broken line , It is best not to have the name of each broken line 1 Sample .
  String filename = tt.generateLineChart(" Personal consumption ratio chart ", " Month     Portions ",
   " Fee     Use ", session, new PrintWriter(out), 800, 550);
  String graphURL = request.getContextPath()
   + "/DisplayChart?filename=" + filename;
 %>
 <P ALIGN="CENTER">
 <img src="<%=graphURL %>" width=800 height=550 border=1 usemap="#<%=filename%>">
 </body>
</html>

I hope this article is helpful to everyone's JSP programming.


Related articles: