这里使用jfreechart-1.5.0.jar
1. jsp中使用jfreechart
<%@page import="com.bee.BarChart"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String fileName=BarChart.genBarChart(session);
System.out.println(fileName);
%>
<img src="DisplayChart?filename=<%=fileName %>" width="700" height="500" border="0"/>
</body>
</html>
2. servlet中使用jfreechart
- 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>
- servlet
package com.bee;
import java.awt.Font;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
public class BarChart {
public static String genBarChart(HttpSession session) throws Exception {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(510, "深圳", "苹果");
dataset.addValue(320, "深圳", "香蕉");
dataset.addValue(580, "深圳", "橘子");
dataset.addValue(390, "深圳", "梨子");
// 创建主题样式
StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
// 设置标题字体
standardChartTheme.setLargeFont(new Font("隶书", Font.BOLD, 20));
// 设置图例的字体
standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));
// 设置轴向的字体
standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
// 应用主题样式
ChartFactory.setChartTheme(standardChartTheme);
JFreeChart chart = ChartFactory.createBarChart("水果销售统计图", "水果", "销售", dataset, PlotOrientation.VERTICAL, true,
true, true);
TextTitle title = chart.getTitle();
title.setFont(new Font("宋体", Font.BOLD, 20));
String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, null, session);
return fileName;
}
}
3. JFreeChart 1.0.x版本的使用
- 相关jar包库
jfreechart-1.0.19.jar
jcommon-1.0.23.jar
或者
jfreechart-1.0.10.jar
jcommon-1.0.13.jar
- Demo
看这里