我正在尝试根据用户输入生成图表。我在Struts 2框架中使用JFree Charts。在图表生成Action类中,我无法实现ModelDriven
概念;我也无法从HttpServletRequest
对象中检索参数值。
如果我通过实施ModelDriven
或ServletRequestAware
调用图表生成操作类,它可以正常工作,但它会在下一页显示图表。我需要根据用户输入生成图表。
我没有成功搜索有关JFree和Struts 2的信息;任何有用的教程链接也将不胜感激。
这是我的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>
<package name="jfree" extends="jfreechart-default">
<action name="chartAction" class="com.kogent.action.ChartAction">
<result name="success" type="chart">
<param name="width">500</param>
<param name="height">300</param>
</result>
</action>
</package>
</struts>
这是我的Action类
package com.kogent.action;
import java.util.Random;
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;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class ChartAction extends ActionSupport implements ModelDriven<FormBean>,Preparable{
private JFreeChart chart;
private FormBean bean;
@Override
public FormBean getModel() {
// TODO Auto-generated method stub
return bean;
}
@Override
public void prepare() throws Exception {
// TODO Auto-generated method stub
bean=new FormBean();
}
public String execute() throws Exception {
// chart creation logic...
System.out.print(bean.getCategory()+" "+bean.getChartType());
//if remove this above line my action runs fine but i require this vales from the user
XYSeries dataSeries = new XYSeries(new Integer(1));
for (int i = 0; i <= 100; i++) {
dataSeries.add(i, new Random().nextInt(50));
}
XYSeriesCollection xyDataSet = new XYSeriesCollection(dataSeries);
ValueAxis xAxis = new NumberAxis("Marks");
ValueAxis yAxis = new NumberAxis("Age");
chart =
new JFreeChart("Chart Title", JFreeChart.DEFAULT_TITLE_FONT,
new XYPlot(xyDataSet,xAxis, yAxis,
new StandardXYItemRenderer(StandardXYItemRenderer.LINES)),
false);
chart.setBackgroundPaint(java.awt.Color.white);
return super.SUCCESS;
}
public JFreeChart getChart() {
return chart;
}
}
答案 0 :(得分:1)
根据这个jfree图表,有很多例子, 只是我给这些链接检查出来。
First
:实现任何jfree图表除了servlet request aware
和servlet response aware
之外没有任何其他要求,请求是用于接收用户的请求,响应是用于向用户提供输出。您希望使用'ModelDriven' interface ( it gains the extra ability to transfer the form data into the object automatically)
。
只需使用此链接。
Create chart and Display them dynamically in JSP
Create area chart in JSP page using JFreeChart
Creating Pie Charts with JFreeChart