前提:struts2,spring3.0.5配置完毕
下面配置JFreeChart:
需要导入的jar(可从附件下载):
jcommon-1.0.17.jar
jfreechart-1.0.14.jar
struts2-jfreechart-plugin-2.2.3.jar
struts-config.xml
---------------------
Action
----------------------
JSP
------------------
下面配置JFreeChart:
需要导入的jar(可从附件下载):
jcommon-1.0.17.jar
jfreechart-1.0.14.jar
struts2-jfreechart-plugin-2.2.3.jar
struts-config.xml
---------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="test" extends="jfreechart-default">
<action name="getDyTrend" class="UserJFreeDyTrendAction">
<result name="success" type="chart">
<param name ="width">500</param>
<param name ="height">370</param>
</result>
</action>
</package>
</struts>
Action
----------------------
public class ChartAction extends ActionSupport {
public String execute() {
// 暂时使用null
getChart1(null, null);
return "success";
}
public JFreeChart getChart1(TbChannelInfo ch, List<DyData> dyDataList) {
log.debug(className + " getChart1 start");
// 有序排序
chart = null;
chart = ChartFactory.createTimeSeriesChart("Dynamic Trend Graphical.", "时间", "mv",
getDataSet(), false, false, false);
// 重新设置图标标题,改变字体
chart.setTitle(new TextTitle("Dynamic Trend Graphical.", new Font("黑体", Font.ITALIC, 22)));
// 取得统计图标的第一个图例
//LegendTitle legend = chart.getLegend();
// 修改图例的字体
//legend.setItemFont(new Font("宋体", Font.BOLD, 14));
XYPlot plot = (XYPlot) chart.getPlot();
// 取得横轴
ValueAxis categoryAxis = plot.getDomainAxis();
// 设置横轴显示标签的字体
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 22));
categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 18));
// 取得纵轴
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
// 设置纵轴显示标签的字体
numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 22));
return chart;
}
private XYDataset getDataSet() {
TimeSeries apple = new TimeSeries("测点一", Month.class);
apple.add(new Month(10, 2007), 3900);
apple.add(new Month(11, 2007), 900);
apple.add(new Month(12, 2007), 2500);
apple.add(new Month(1, 2008), 3900);
apple.add(new Month(2, 2008), 2000);
apple.add(new Month(3, 2008), 3300);
TimeSeries orange = new TimeSeries("测点二", Month.class);
orange.add(new Month(10, 2007), 3300);
orange.add(new Month(11, 2007), 2680);
orange.add(new Month(12, 2007), 2000);
orange.add(new Month(1, 2008), 1900);
orange.add(new Month(2, 2008), 2000);
orange.add(new Month(3, 2008), 2300);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(apple);
dataset.addSeries(orange);
return dataset;
}
}
JSP
------------------
<img src="getDyTrend?temp=<%=Math.random()%>" width=420 height=300 border=0>