JFreeChart drawing + jsp page display implementation statistics

1 development environment:

1. eclipse (replaceable)


2 Description:

(1) source directory: it's the source directory of jfreechart; I won't see it here. Because his documents are charged.

(2) lib Directory: it is the package directory. The packages we need to pay attention to are jfreechart-1.0.10.jar, gnujaxp.jar and jcommon-1.0.13.jar;

(3) jfreechart-1.0.10-demo.jar in the root directory is an example program. You can see the running results of many examples after double clicking.

Java web development process

3.1 import jar package

Import all jar packages under jfreechart-1.0.19/lib into the directory "/ WEB-INF/lib"

3.2 modify configuration file

Add the following configuration in the web.xml file, and try to configure it in front of other servlet configurations with low priority:

      org.jfree.chart.servlet.DisplayChart   <!--This is fixed-->

3.3 write jsp file

<%@ page language="java" contentType="text/html; charset=UTF-8"
<%@ page import="org.jfree.data.general.DefaultPieDataset" %>
<%@ page import="org.jfree.chart.ChartFactory" %>
<%@ page import="org.jfree.chart.JFreeChart" %>
<%@ page import="org.jfree.chart.servlet.*" %>
<%@ page import="org.jfree.chart.StandardChartTheme" %>
<%@ page import="java.awt.Font" %>
<%@page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@ page import="org.jfree.chart.plot.PlotOrientation"%>          
<%@ page import="org.jfree.data.category.DefaultCategoryDataset"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Data analysis</title>
    <!-- Pie chart -->
    DefaultPieDataset dpd = new DefaultPieDataset();
    dpd.setValue("Management", 25);
    dpd.setValue("Market personnel", 25);
    dpd.setValue("Developer", 45);
    dpd.setValue("Other personnel", 10);
    //This section is to prevent the use of random code
    StandardChartTheme standardChartTheme=new StandardChartTheme("CN");  //Create a theme style     
    standardChartTheme.setExtraLargeFont(new Font("Official script",Font.BOLD,20));  //Set title font
    standardChartTheme.setRegularFont(new Font("Song Shu",Font.PLAIN,15));    //Set font for Legend
    standardChartTheme.setLargeFont(new Font("Song Shu",Font.PLAIN,15));      //Set axial font
    ChartFactory.setChartTheme(standardChartTheme);                        //Apply theme styles
    JFreeChart chart = ChartFactory.createPieChart("Organization chart of a company",dpd, true, false, false);
    String fileName = ServletUtilities.saveChartAsPNG(chart,800,600,session); 
    //ServletUtilities Is oriented web Development tool class, return a string filename,The file name is generated automatically, and the generated image will be placed on the server automatically( tomcat)Under the temporary documents of( temp)
    String url = request.getContextPath() + "/DisplayChart?filename=" + fileName;
    //According to the file name, go to the temporary directory to find the picture, here/DisplayChart The path should be consistent with the user-defined path in the configuration file<url-pattern>Agreement
     <img src="<%= url %>" width="400" height="350"> 

    <!-- Histogram -->
     DefaultCategoryDataset dataset = new DefaultCategoryDataset();
     dataset.addValue(0.2, "Guangzhou", "Pork");
     dataset.addValue(0.4, "Guangzhou", "beef");
     dataset.addValue(0.1, "Guangzhou", "chicken");
     dataset.addValue(0.1, "Guangzhou", "fish");
     // Create a theme style
     StandardChartTheme standardChartTheme1 = new StandardChartTheme("CN");
     // Set title font
     standardChartTheme1.setExtraLargeFont(new Font("Official script", Font.BOLD, 20));
     // Set font for Legend
     standardChartTheme1.setRegularFont(new Font("Song Shu", Font.PLAIN, 15));
     // Set axial font
     standardChartTheme1.setLargeFont(new Font("Song Shu", Font.PLAIN, 15));
     // Apply theme styles
     JFreeChart chart1 = ChartFactory.createBarChart3D("Statistics of meat sales", 
                       "% of sales(%)",
     String filename = ServletUtilities.saveChartAsPNG(chart1, 1200, 300, null, session);
     String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
     System.out.println(graphURL + "\n"+ filename);
 <img src="<%= graphURL %>"width=1200 height=300 border=0 usemap="#<%= filename %>">


4 Effect Picture

Attached jar download address: http://www.java2s.com/Code/Jar/CatalogJar.htm

Tags: Java Web Development Session Eclipse

Posted on Tue, 03 Dec 2019 22:47:29 -0500 by Plagel