A Java novice encountered a problem when writing a web page, that is, to take out the content of a table in the database and generate a bar chart or a line chart, which suddenly became confusing, and I don't know how to complete it, but I stopped and thought about it. Thought, first of all, I should know the ways to generate charts. I found the experience summarized by the great God through the Internet as follows (original):
https://blog.csdn.net/happylee6688/article/details/26960729
- ChartDirector
- JScharts
- JFreeChart
<%@ page pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<%@ page import="java.awt.Color,
org.jfree.chart.ChartFactory,
org.jfree.chart.JFreeChart,
org.jfree.chart.plot.PlotOrientation,
org.jfree.chart.servlet.ServletUtilities,
org.jfree.data.category.CategoryDataset,
org.jfree.data.general.*,
org.jfree.chart.plot.CategoryPlot,
org.jfree.chart.axis.CategoryAxis,
org.jfree.chart.axis.ValueAxis,
org.jfree.chart.renderer.category.BarRenderer3D,
org.jfree.chart.urls.StandardCategoryURLGenerator,
org.jfree.chart.*,
org.jfree.chart.entity.*,
org.jfree.chart.labels.*,
org.jfree.chart.axis.CategoryLabelPositions,
java.awt.Font,
java.awt.GradientPaint,
org.jfree.ui.TextAnchor,
org.jfree.chart.axis.NumberAxis,
org.jfree.chart.axis.NumberTickUnit
"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset"%>
<%@ page import="org.jfree.data.category.CategoryDataset" %>
<%@ page import="org.jfree.data.category.DefaultCategoryDataset" %>
<%@ page import="org.jfree.chart.JFreeChart"%>
<%@ page import="org.jfree.chart.plot.PiePlot"%>
<%@ page import="org.jfree.chart.ChartRenderingInfo"%>
<%@ page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@ page import="org.jfree.chart.urls.StandardPieURLGenerator"%>
<%@ page import="org.jfree.chart.entity.StandardEntityCollection"%>
<%@ page import="org.jfree.chart.encoders.SunPNGEncoderAdapter"%>
<%@ page import="org.jfree.chart.StandardChartTheme"%>
<%@ page import="org.jfree.chart.ChartFactory"%>
<%@ page import="org.jfree.chart.plot.CategoryPlot"%>
<%@ page import="org.jfree.chart.plot.PlotOrientation"%>
<%@ page import="org.jfree.chart.axis.CategoryAxis"%>
<%@ page import="org.jfree.chart.renderer.category.BarRenderer"%>
<%@ page import="org.jfree.chart.labels.StandardCategoryItemLabelGenerator"%>
<%@ page import="java.awt.Font"%>
<%@ page import="java.awt.Color"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.awt.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.jfree.chart.axis.CategoryAxis3D"%>
<%@ page import="org.jfree.chart.axis.CategoryLabelPositions"%>
<%@ page import="org.jfree.chart.axis.CategoryAxis3D"%>
<%@ page import="org.jfree.chart.axis.NumberAxis3D"%>
<%@ page import="org.jfree.chart.axis.NumberTickUnit"%>
<%@ page import="org.jfree.chart.renderer.category.BarRenderer3D"%>
<%@ page import="org.jfree.chart.renderer.category.BarRenderer3D" %>
<%@ page import="org.jfree.chart.renderer.category.LineAndShapeRenderer" %>
<%@ page import="org.jfree.data.category.DefaultCategoryDataset" %>
<%@ page import="org.jfree.chart.plot.DatasetRenderingOrder" %>
//PS: A novice should import classes as much as possible to prevent mistakes...
//The pie chart code is as follows:
<html>
<body>
<%
//Instantiate the core class object
DefaultPieDataset dataset=new DefaultPieDataset();
//Set the chart value, it can be multiple
dataset.setValue("paid",45);
dataset.setValue("Unpaid ",25);
//Create a pie chart based on the factory class org.jfree.chart.ChartFactory provided by
JFreeChart JFreeChart chart=ChartFactory.createPieChart3D("Student payment form",dataset,true,false,false);
//Generate the graph and save it into the session, get the name of the graph saved in the session
String filename=ServletUtilities.saveChartAsPNG(chart,500,400,session);
//Display the generated graph through img on the page, and output the graph through jfreechart's servlet
String graphURL=request.getContextPath()+"/DisplayChart?filename="+filename;
%>
<img src="<%=graphURL %> " >
</body>
</html>
The above method of outputting the chart is to use the serverlet of JFreeChart for output, so the web.xml file should be configured as follows: (If the chart is not displayed, most of the web.xml is not configured)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ceshi</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
In fact, there are two ways to output the chart, the other is through a function output: (for example, the histogram)
//The first step, construct a Dataset: fill in the data to be counted
DefaultCategoryDataset mDataset = new DefaultCategoryDataset();
mDataset.addValue(2000, "Tsinghua University", "Undergraduate"); //The data is generally obtained from the database, using mDataset.addValue
(1500, "Tsinghua University", "Graduate student");
mDataset.addValue(1000, "Tsinghua University", "PhD student");
mDataset.addValue(900, "Tsinghua University", " Lecturer");
mDataset.addValue(800, "Tsinghua University", "Associate Professor");
mDataset.addValue(300, "Tsinghua University", "Professor");
mDataset.addValue(600, "Tsinghua University", "Administrative Staff" ");
mDataset.addValue(400, "Tsinghua University", "Managers");
//The second step, generate graphics, ChartFactory.createBarChart3D to generate a bar chart
JFreeChart mChart = ChartFactory.createBarChart3D(
"School personnel distribution map",// graphic name
"type", // category name, the abscissa name
"quantity", // value name, the ordinate name
mDataset, // data set
PlotOrientation.VERTICAL, // vertical display
true, // whether to display legend
true, // whether to use tooltip
false);// whether to use url
// third, display, there are two methods: streaming output and URL (above Already said) output
response.setContentType("image/png");
//Streaming output
ChartUtilities.writeChartAsPNG(response.getOutputStream(),mChart,800,500);
//////////////////////////////////////////////////////
It stands to reason that the core output of the chart has been learned, but from the above, we know that JFreeChart does not support Chinese, and there will be garbled characters. If the parameters in the chart are in English, the above code is enough to deal with
If you want to output Chinese without displaying garbled characters, you must configure it yourself, as follows:
// Configure font
Font xfont = new Font("宋体", Font.PLAIN, 12);// X-axis
Font yfont = new Font("宋体", Font.PLAIN, 12);// Y-axis
Font kfont = new Font("Arial", Font.PLAIN, 12);// Bottom
Font titleFont = new Font("Arial", Font.BOLD, 25); // Picture title
//Graphic drawing structure object, not applicable to pie chart
CategoryPlot plot = mChart.getCategoryPlot();
// X-axis
CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setLabelFont
(xfont);// Axis title
domainAxis.setTickLabelFont(xfont);// Axis value
domainAxis.setTickLabelPaint(Color. BLACK); // font color
// Y-axis
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(yfont);
rangeAxis.setLabelPaint(Color.BLACK); // font color
rangeAxis.setTickLabelFont(yfont);
This solves the garbled code. The above code is suitable for bar charts and font settings. The code for setting fonts
for line charts and pie charts is not the same. It is necessary to set fonts for different types of graphics separately, but they are very similar.
Some of the ideas above are enough for the general needs of users, but sometimes you want to output some comprehensive charts, such as histograms and line charts, or even histograms and many line charts.
There are also many examples on the Internet, and you can also learn line charts along with the above ideas.
Comprehensive reference link:
http://blog.sina.com.cn/s/blog_906e353d0101241e.html
https://blog.csdn.net/sb_jb/article/details/77867614?fps=1&locationNum=2