SoFunction
Updated on 2025-03-08

jsp JFreeChart usage experience and examples

The latest version of the current JFreeChart is jfreechart-1.0. You can find it in /jfreechart/
2. Basic use of JFreeChart
Regardless of the graph you created, JFreeChart follows the following steps:
1. Create a Dataset. All data is stored in Dataset. (Create a dataset to contain the data to be displayed in the graph)
2. Create JFreeChart. Import the data from the dataset into JFreeChart. (Create a JFreeChart object to represent the graphics to be displayed)
3. Set the display properties of JFreeChart. This step can be omitted and display properties using the default JFreeChart.
3. Render the chart. That is, generate a picture.
4. Page picture display.
Important classes and interfaces:
Interfaces to be implemented by all data source classes
It generates the JFreeChart object
All adjustments to the graphics are done through it! !
Get it through the JFreeChart object and then adjust it to the outer part of the figure (example: axis) through it
Note: It has many subclasses, and it usually involves its subclasses!
Get it through the JFreeChart object and then adjust the internal parts of the figure (example: the type of polyline). Similarly, for different types of report charts, it has different subclass implementations! Let's call it Renderer in the following

3. Specific examples of JFreeChart use
Examples in web applications
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* Drawing of curve charts
*/
public class LineXYChart
{
/**
* Returns the file name of the generated image
* @param session
* @param pw
* @return The file name of the generated image
*/
public String getLineXYChart(HttpSession session, PrintWriter pw)
{
XYDataset dataset = ();//Create a dataset
String fileName = null;
//Create JFreeChart
JFreeChart chart = (
"JFreeChart Time Curve Sequence Chart", // title
"Date", // x-axis label
"Price", // y-axis label
dataset, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
//Set the display properties of JFreeChart to adjust the external parts of the graphics
();//Set the background color of the curve chart
//Set the font size and shape
Font font = new Font("Song style", , 16);
TextTitle title = new TextTitle("JFreeChart time curve sequence chart", font);
(title);
//subtitle
TextTitle subtitle =
new TextTitle("subtitle", new Font("bold", , 12));
(subtitle);
(title); //Title

//Set the icon title character
//TimeSeries s1 = new TimeSeries("Historical Curve", );The Chinese character
LegendTitle legengTitle = ();
(font);

XYPlot plot = (XYPlot) ();//Get the canvas of the graphics
();//Set the grid background color
();//Set the color of the grid vertical line (Domain axis)
();//Set the grid horizontal line color
(new RectangleInsets(5.0, 5.0, 5.0, 5.0));//Set the distance between the curve chart and the xy axis
(true);
(true);
XYItemRenderer r = ();
if (r instanceof XYLineAndShapeRenderer)
{
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
(true);
(true);
(true);//Set whether the curve displays data points
}
//Set the Y axis
NumberAxis numAxis = (NumberAxis) ();
NumberFormat numFormater = ();
(2);
(numFormater);
//Set prompt information
StandardXYToolTipGenerator tipGenerator = new StandardXYToolTipGenerator(
"Historical information{1} 16:00,{2})", new SimpleDateFormat("MM-dd"),numFormater);
(tipGenerator);
//Set the X-axis (date axis)
DateAxis axis = (DateAxis) ();
(new SimpleDateFormat("MM-dd"));
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
try
{
fileName = (chart, 500, 300, info,
session);// Generate picture
// Write the image map to the PrintWriter
(pw, fileName, info, false);
}
catch (IOException e)
{
();
}
();
return fileName;//Return the file name of the generated image
}
/**
* Create the data set required to generate the graph
* @return Return the dataset
*/
private XYDataset createDateSet()
{
TimeSeriesCollection dataset = new TimeSeriesCollection();//Time curve data collection
TimeSeries s1 = new TimeSeries("Historical Curve", );//Create a time data source, each //TimeSeries is a curve on the graph
//(new Day(day,month,year),value), add data point information
(new Day(1, 2, 2006), 123.51);
(new Day(2, 2, 2006), 122.1);
(new Day(3, 2, 2006), 120.86);
(new Day(4, 2, 2006), 122.50);
(new Day(5, 2, 2006), 123.12);
(new Day(6, 2, 2006), 123.9);
(new Day(7, 2, 2006), 124.47);
(new Day(8, 2, 2006), 124.08);
(new Day(9, 2, 2006), 123.55);
(new Day(10, 2, 2006), 122.53);
(s1);
(true);
return dataset;
}
}


Show pictures in jsp file
First add the following code to the web application deployment file:

<!-- Image display, use a dedicated servlet to display, it will complete the search and mapping of paths -->
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class></servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>
Then display the picture in jsp
Complete jsp file:
<!--File Name:-->
<%@ page contentType="text/html;charset=gb2312" pageEncoding="GB2312"%>
<%@ page import=""%>
<%@ page import = "" %>
<%
LineXYChart xyChart=new LineXYChart();
String fileName=(session,new PrintWriter(out));
String graphURL = () + "/servlet/DisplayChart?filename=" + fileName;
%>
<html>
</head>
<title> JFreeChart usage example</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="<%= fileName %>">
</body>
</html>