接上一篇随笔 JFreeChart 生成 柱状图 ,环境及其配置不变,只是将生成柱状图的核心类 CylinderAction 替换成 CurveAction,让 CurveAction来负责折线图的生成,看下代码:
package com.fancy.action;
import java.awt.Color;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
/**
* -----------------------------------------
* @描述 折线图
* @作者 fancy
* @邮箱 fancydeepin@yeah.net
* @日期 2012-8-7 <p>
* -----------------------------------------
*/
public class CurveAction extends BaseAction{
private static final long serialVersionUID = 1L;
private String fileName;
public String execute() throws Exception{
TimeSeries series2010 = new TimeSeries("2010年度", Month.class);
TimeSeries series2011 = new TimeSeries("2011年度", Month.class);
TimeSeriesCollection dataset = new TimeSeriesCollection();
series2010.add(new Month(1, 2011), 36);
series2010.add(new Month(2, 2011), 84);
series2010.add(new Month(3, 2011), 202);
series2010.add(new Month(4, 2011), 183);
series2010.add(new Month(5, 2011), 172);
series2010.add(new Month(6, 2011), 107);
series2010.add(new Month(7, 2011), 200);
series2010.add(new Month(8, 2011), 87);
series2010.add(new Month(9, 2011), 250);
series2010.add(new Month(10, 2011), 152);
series2010.add(new Month(11, 2011), 235);
series2010.add(new Month(12, 2011), 266);
series2011.add(new Month(1, 2011), 156);
series2011.add(new Month(2, 2011), 216);
series2011.add(new Month(3, 2011), 118);
series2011.add(new Month(4, 2011), 250);
series2011.add(new Month(5, 2011), 257);
series2011.add(new Month(6, 2011), 253);
series2011.add(new Month(7, 2011), 355);
series2011.add(new Month(8, 2011), 300);
series2011.add(new Month(9, 2011), 103);
series2011.add(new Month(10, 2011), 250);
series2011.add(new Month(11, 2011), 348);
series2011.add(new Month(12, 2011), 325);
dataset.addSeries(series2010);
dataset.addSeries(series2011);
JFreeChart chart = ChartFactory.createTimeSeriesChart("A产品销售量", "", "", dataset, true, true, false);
DateAxis dateaxis = (DateAxis)chart.getXYPlot().getDomainAxis();
dateaxis.setDateFormatOverride(new SimpleDateFormat("MM月"));
chart.setBackgroundPaint(Color.WHITE);
fileName = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, getHttpSession());
return "curve";
}
public String getFileName() {
return fileName;
}
}
将 cylinder.jsp 拷贝一份名字改成 curve.jsp,其余不变,访问:http://localhost:8080/jfreechart-demo/curve.html 的结果如图示:
在上图中,可以看出年度A产品的大概走势,但并没有具体的数据值,为使折线上的数据点的数值能够被显示出来,下面在 CurveAction 类中添加代码:
package com.fancy.action;
import java.awt.Color;
import java.awt.Font;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.TextAnchor;
/**
* -----------------------------------------
* @描述 折线图
* @作者 fancy
* @邮箱 fancydeepin@yeah.net
* @日期 2012-8-7 <p>
* -----------------------------------------
*/
public class CurveAction extends BaseAction{
private static final long serialVersionUID = 1L;
private String fileName;
public String execute() throws Exception{
TimeSeries series2010 = new TimeSeries("2010年度", Month.class);
TimeSeries series2011 = new TimeSeries("2011年度", Month.class);
TimeSeriesCollection dataset = new TimeSeriesCollection();
series2010.add(new Month(1, 2011), 36);
series2010.add(new Month(2, 2011), 84);
series2010.add(new Month(3, 2011), 202);
series2010.add(new Month(4, 2011), 183);
series2010.add(new Month(5, 2011), 172);
series2010.add(new Month(6, 2011), 107);
series2010.add(new Month(7, 2011), 200);
series2010.add(new Month(8, 2011), 87);
series2010.add(new Month(9, 2011), 250);
series2010.add(new Month(10, 2011), 152);
series2010.add(new Month(11, 2011), 235);
series2010.add(new Month(12, 2011), 266);
series2011.add(new Month(1, 2011), 156);
series2011.add(new Month(2, 2011), 216);
series2011.add(new Month(3, 2011), 118);
series2011.add(new Month(4, 2011), 250);
series2011.add(new Month(5, 2011), 257);
series2011.add(new Month(6, 2011), 253);
series2011.add(new Month(7, 2011), 355);
series2011.add(new Month(8, 2011), 300);
series2011.add(new Month(9, 2011), 103);
series2011.add(new Month(10, 2011), 250);
series2011.add(new Month(11, 2011), 348);
series2011.add(new Month(12, 2011), 325);
dataset.addSeries(series2010);
dataset.addSeries(series2011);
JFreeChart chart = ChartFactory.createTimeSeriesChart("A产品销售量", "", "", dataset, true, true, false);
DateAxis dateaxis = (DateAxis)chart.getXYPlot().getDomainAxis();
dateaxis.setDateFormatOverride(new SimpleDateFormat("MM月"));
chart.setBackgroundPaint(Color.WHITE);
XYPlot plot = (XYPlot)chart.getPlot();
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
//设置曲线是否显示数据点
xylineandshaperenderer.setBaseShapesVisible(true);
//设置曲线显示各数据点的值
XYItemRenderer xyitem = plot.getRenderer();
xyitem.setBaseItemLabelsVisible(true);
xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 14));
plot.setRenderer(xyitem);
ValueAxis rangeAxis = plot.getRangeAxis();
//距离上边距边框距离
rangeAxis.setUpperMargin(0.3);
//距离下边框边距距离
rangeAxis.setLowerMargin(0.5);
ValueAxis domainAxis = plot.getDomainAxis();
// 左边距 边框距离
domainAxis.setLowerMargin(0.05);
// 右边距 边框距离,防止最后边的一个数据靠近了坐标轴。
domainAxis.setUpperMargin(0.1);
fileName = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, getHttpSession());
return "curve";
}
public String getFileName() {
return fileName;
}
}
OK,再次访问的结果如图示:
posted on 2012-08-07 12:00
fancydeepin 阅读(2670)
评论(1) 编辑 收藏