生成一张简单的图片
java代码:
package com.lxl.chart;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
public class ChartDemo {
public static String getChartName(HttpSession session) throws Exception {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(200, "包头", "茄子");
dataset.addValue(300, "包头", "辣椒");
dataset.addValue(400, "包头", "皇冠");
dataset.addValue(500, "包头", "苹果");
JFreeChart chart = ChartFactory.createBarChart3D("蔬菜价格表", "", "", dataset, PlotOrientation.VERTICAL, true, true,
true);
String chartName = ServletUtilities.saveChartAsPNG(chart, 700, 500,null, session);
return chartName;
}
}
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>
jsp页面:
<%@page import="com.lxl.chart.ChartDemo"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<%
String name = ChartDemo.getChartName(session);
System.out.println(name);
%>
<body>
<img src="DisplayChart?filename=<%=name%>" width="700" height="500" border="0">
</body>
</html>