您的位置:首页 > 编程语言 > Java开发

struts2使用插件建立图表

2015-10-30 18:22 459 查看
问题!struts2使用插件建立图表

定义:为什么要使用插件,因为在web开发中有时候会用到,建立图表来显示数据,类给用户直观的数据结果,这个时候就需要使用jar来实现了,别人写好的jar包。我们只需要拿来用就可以了。自己去编写的话会很复杂的。

案例:

struts.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>
<constant name="struts.action.extension" value="action,,do"></constant>
<constant name="struts.devMode" value="true"></constant>

<package name="chart" extends="jfreechart-default">
<action name="chart" class="cn.itcast.web.jfreechar.GetCharAction">
<result type="chart" name="success">
<param name="height">400</param>
<param name="width">600</param>
</result>
</action>
</package>
</struts>


jfreechart-default是我们需要继承的包,这个需要查看相关文件才知道。



结果返回类型:type必须是chart,高宽自定。

动作类;

package cn.itcast.web.jfreechar;

import java.io.Serializable;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import com.opensymphony.xwork2.ActionSupport;

public class GetCharAction extends ActionSupport implements Serializable {

private JFreeChart chart;
public JFreeChart getChart() {
return chart;
}
public String execute() {
ValueAxis xAxis = new NumberAxis("年度");
ValueAxis yAxis = new NumberAxis("产值");
XYSeries xySeries = new XYSeries("红豆");
xySeries.add(0,300);
xySeries.add(1,200);
xySeries.add(2,400);
xySeries.add(3,500);
xySeries.add(4,600);
xySeries.add(5,500);
xySeries.add(6,800);
xySeries.add(7,1000);
xySeries.add(8,1100);
XYSeriesCollection xyDataset = new XYSeriesCollection(xySeries);
XYPlot xyPlot = new XYPlot(xyDataset,xAxis,yAxis,new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES_AND_LINES));
chart = new JFreeChart(xyPlot);
return SUCCESS;
}
}


这个大家看一下就知道。里面的代码都是固定结构。这个返回结果是以图片形式在网页打开的。

效果:



案例下载:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: