用servlet生成图片,源码如下:
package com.test.chart;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.jfree.data.time.TimeSeries;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Month;
import org.jfree.chart.axis.DateAxis;
import org.jfree.data.time.Minute;
import org.jfree.chart.ChartFactory;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtilities;
public class ChartServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
// service
public void service(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {
OutputStream out = response.getOutputStream(); // 定义输出流
try {
JFreeChart chart = null; // 创建图标工具
chart = createTimeSeriesChart(); // 产生折线图
if (chart != null) {
response.setContentType("image/png"); // 定义类型
ChartUtilities.writeChartAsPNG(out, chart, 600, 300); // 输出
}
} catch (Exception e) {
System.err.println(e.toString());
} finally {
out.close(); // 关闭流
}
}
private JFreeChart createTimeSeriesChart() {
TimeSeries series = new TimeSeries("实时状态图", Minute.class); // 第一条折线
TimeSeries series2 = new TimeSeries("实时状态图2", Minute.class); // 第二条折线
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(); // 定义折线集合
timeseriescollection.addSeries(series); // 添加第一条曲线
timeseriescollection.addSeries(series2); // 添加第二条曲线
// Day day = new Day(1, Month.JANUARY, 2001); // 定义日期
// Minute(int minute, int hour, int day, int month, int year)
Minute current = new Minute(2, 14, 25, Month.JANUARY, 2007); // 图形使用的时间
// 添加测试数据
for (int i = 0; i < 50; i++) {
series.add(current, Math.random() * 100); // 添加数据
series2.add(current, Math.random() * 100);
current = (Minute) current.next();
}
//XYDataset data = new TimeSeriesCollection(series);
// 创建图形
// createTimeSeriesChart(java.lang.String title, java.lang.String timeAxisLabel, java.lang.String valueAxisLabel, XYDataset dataset, boolean legend, boolean tooltips, boolean urls)
// Parameters:
//title - the chart title (null permitted).
//timeAxisLabel - a label for the time axis (null permitted).
//valueAxisLabel - a label for the value axis (null permitted).
//dataset - the dataset for the chart (null permitted).
//legend - a flag specifying whether or not a legend is required.
//tooltips - configure chart to generate tool tips?
//urls - configure chart to generate URLs?
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"实时状态图",
"时间(分钟)",
"数值",
timeseriescollection,
true,
true,
false
);
XYPlot xyplot = chart.getXYPlot();
// valueaxis.setAutoRange(true); // 是否自动调整显示区域
// valueaxis.setFixedAutoRange(60000000D); // 设置固定宽度
// valueaxis.setAutoRangeMinimumSize( 300000D ); // 设置最小刻度
// valueaxis.setLowerBound( 1179473079718D ); //最小显示的值
DateAxis dateAxis = (DateAxis) xyplot.getDomainAxis();
// public Date(int year, int month, int date, int hrs, int min) {
// year - 减 1900 的年份。
//month - 0-11 之间的月份。
//date - 一月中 1-31 之间的某一天。
//hrs - 0-23 之间的小时数。
//min - 0-59 之间的分钟数。
Date dt = new Date(107, 0, 25, 14, 0);
Date dts = new Date(107, 0, 25, 15, 0);
// 横坐标设置
dateAxis.setMaximumDate(dts); // 日期轴上的最小日期
dateAxis.setMinimumDate(dt); // 日期轴上的最大日期
// dateAxis.setAutoRange( true ); // 自动调整
//dateAxis.setAutoRangeMinimumSize( 30000D );
// AbstractRenderer
//void setSeriesPaint(int series,Paint paint)指定分类图形的颜色
// AbstractRenderer renderer = (AbstractRenderer)chart.getCategoryPlot().getRenderer();
// Color c = new Color();
//renderer.setSeriesPaint(1 , Color.BLUE );
// 纵坐标设定
ValueAxis valueaxis = null;
valueaxis = xyplot.getRangeAxis();
valueaxis.setRange(0.0D, 200D); // 高度范围为 0 - 200
// xyplot.setDataset(1, timeseriescollection1);
// xyplot.setRenderer(1,new DefaultXYItemRenderer());
return chart;
}
//Clean up resources
public void destroy() {
}
}
新建网页index.html,源码如下 :
<html>
<head>
</head>
<script type="text/javascript">
var req;
setInterval( "castVote()" , 3000 )
function castVote1() {
document.getElementById( "votes" ).innerHTML = "<IMG src='chartservlet' BORDER=1 WIDTH=600 HEIGHT=300/>"
}
function castVote(rank) {
// This demo uses simple Html/JS only, so we load a static html page
var url = "ii.jsp";
var callback = processAjaxResponse;
executeXhr(callback, url);
}
function executeXhr(callback, url) {
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = callback;
req.open("GET", url, true);
req.send(null);
} // branch for IE/Windows ActiveX version
else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = callback;
req.open("GET", url, true);
req.send();
}
}
}
function processAjaxResponse() {
// only if req shows "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('votes').innerHTML = req.responseText;
} else {
alert("There was a problem retrieving the XML data:/n" +
req.statusText);
}
}
}
</script>
<body>
<div id="votes"><IMG src='chartservlet' BORDER=1 WIDTH=600 HEIGHT=300/></div>
</body>
</html>
将web.xml添加如下内容:
<servlet>
<servlet-name>chartservlet</servlet-name>
<servlet-class>com.test.chart.ChartServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>chartservlet</servlet-name>
<url-pattern>/chartservlet</url-pattern>
</servlet-mapping>
原创开山之作,希望大家提出意见建议