You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 14 Next »

I am rendering a chart to the output stream. Instead of streaming it directly to the response.out, I create a ChartResult, and let webwork do the chaining for me.

I generate the chart in one class, and I render it out in another class, effectively decoupling the view from the Actions. You can easily render it out to a file or some view other than a web response.out if you wish.

Configuration

struts.xml
<result-types>
   <result-type name="chart" class="myapp.webwork.extensions.ChartResult"/>
</result-types>
struts.xml
<action name="viewModerationChart" class="myapp.webwork.actions.ViewModerationChartAction">
  <result name="success" type="chart">
    <param name="width">400</param>
    <param name="height">300</param> </result>
</action>

Source Codes

My result class searches for a "chart" in the ValueStack and renders it out...

public class ChartResult implements Result {

	private int width;
	private int height;

	public void execute(ActionInvocation invocation) throws Exception {
		JFreeChart chart =
			(JFreeChart) invocation.getStack().findValue("chart");
		HttpServletResponse response = ServletActionContext.getResponse();
		OutputStream os = response.getOutputStream();
		ChartUtilities.writeChartAsPNG(os, chart, width, height);
		os.flush();
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void setWidth(int width) {
		this.width = width;
	}

}

My action class creates the JFreeChart to render...

public class ViewModerationChartAction extends ActionSupport {

	private JFreeChart chart;

	public String execute() throws Exception {
		// chart creation logic...
		XYSeries dataSeries = new XYSeries(new Integer(1)); //pass a key for this serie
		for (int i = 0; i <= 100; i++) {
			dataSeries.add(i, RandomUtils.nextInt());
		}
		XYSeriesCollection xyDataset = new XYSeriesCollection(dataSeries);

		ValueAxis xAxis = new NumberAxis("Raw Marks");
		ValueAxis yAxis = new NumberAxis("Moderated Marks");

		// set my chart variable
		chart =
			new JFreeChart(
				"Moderation Function",
				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;
	}

}

Explaination

public JFreeChart getChart() {
	return chart;
}

makes the chart available on the ValueStack, which the result gets via

JFreeChart chart = (JFreeChart) invocation.getStack().findValue("chart");

From what I can deduce, the webwork pulls in the height and width variables from the xwork.xml definitions for that particular action...

<param name="width">400</param>
<param name="height">300</param>

Suggestions for the Next developer...

Currently the "chart" property is hardcoded. There should be a better way of transferring data from the Action to the Result, via some externally defined variable or something.

As mentioned by John Patterson (mailing list), the Action is still dependant on a JFreeChart Chart class. This can be improved. The seperation between Action and View can be made cleaner. A chart-agonistic List or Array can be used as the data, and the configuration of the chart details (font, axis, etc...) be done via the result properties in the xwork.xml.

But hey, the above works for now. Any suggestions are welcome.

Creating charts via CeWolf directly in Velocity templates

See CeWolf charts using Velocity templates.

  • No labels