准备工作在上一篇文章,
jsp代码:
<%
TimeSeriesCollection dataTimeSet = new TimeSeriesCollection();
TimeSeries timesre = new TimeSeries("折线图", Day.class);
timesre.add(new Day(new SimpleDateFormat("yyyy-MM-dd").parse("2009-01-02")), 1000);
timesre.add(new Day(new SimpleDateFormat("yyyy-MM-dd").parse("2009-02-02")), 2000);
timesre.add(new Day(new SimpleDateFormat("yyyy-MM-dd").parse("2009-03-02")), 3000);
timesre.add(new Day(new SimpleDateFormat("yyyy-MM-dd").parse("2009-04-02")), 2000);
timesre.add(new Day(new SimpleDateFormat("yyyy-MM-dd").parse("2009-05-02")), 4000);
timesre.add(new Day(new SimpleDateFormat("yyyy-MM-dd").parse("2009-06-02")), 5000);
timesre.add(new Day(new SimpleDateFormat("yyyy-MM-dd").parse("2009-07-02")), 6000);
timesre.add(new Day(new SimpleDateFormat("yyyy-MM-dd").parse("2009-08-02")), 9000);
timesre.add(new Day(new SimpleDateFormat("yyyy-MM-dd").parse("2009-09-02")), 1000);
timesre.add(new Day(new SimpleDateFormat("yyyy-MM-dd").parse("2009-10-02")), 2000);
timesre.add(new Day(new SimpleDateFormat("yyyy-MM-dd").parse("2010-01-02")), 6000);
dataTimeSet.addSeries(timesre);
JFreeChart chartTime = ChartFactory.createTimeSeriesChart("折线图",
"生产日期", "工作点数", dataTimeSet, true, true, false);
chartTime.setBackgroundPaint(Color.white);
XYPlot plot = (XYPlot) chartTime.getXYPlot();
plot.setBackgroundPaint(Color.white);//设置网格背景色
plot.setDomainGridlinePaint(Color.LIGHT_GRAY);//设置网格竖线(Domain轴)颜色
plot.setRangeGridlinePaint(Color.LIGHT_GRAY);//设置网格横线颜色
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));//设置曲线图与xy轴的距离
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
}
//设置Y轴
NumberAxis numAxis = (NumberAxis) plot.getRangeAxis();
NumberFormat numFormater = NumberFormat.getNumberInstance();
//设置显示值的小数点个数
numFormater.setMinimumFractionDigits(2);
numAxis.setNumberFormatOverride(numFormater);
//设置Y周范围
//numAxis.setLowerBound(200);
//numAxis.setUpperBound(7000);
//设置X轴(日期轴)
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("MM-dd"));
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
String fileName = ServletUtilities.saveChartAsPNG(chartTime, 600,
350, info, session);//生成图片
//String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
String graphURL = request.getContextPath()
+ "/servlet/DisplayChart?filename=" + fileName;
%>
同样页面中引用图片地址
<table>
<tr align="center">
<img src="<%=graphURL%>" width=500 height=300 border=0
usemap="#map0">
</tr>
</table>
折线图结果: