Child pages
  • JFreeChart Plugin

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

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.

...

Info

JFreeChart is a free 100% Java (LGPL) chart library that makes it easy for developers to display professional quality charts in their applications.

The JFreeChart plugin

Excerpt

allows Actions to easily return generated charts and graphs

.

Instead of streaming a generated chart directly to the HTTP response, this plugin provides a ChartResult, which handles the generation for you. This allows you to 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 HTTP response .out if you wish.

Configuration

...


<result-types>
   <result-type name="chart" class="myapp.webwork.extensions.ChartResult"/>
</result-types>

...

Features

  • Handles rendering charts to the HTTP response
  • Can be used in other non-web contexts

Future Work

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.

Note

You can also create charts via the CeWolf library directly. See CeWolf charts using Velocity templates.

Usage

To use the plugin, have your Struts configuration package extend the jfreechart-default package, which provides the chart result type. Next, use it as a result in an action:

Code Block
langxml
titleChart example in 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...

Code Block

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...

...

In your Action class, provide a getChart() method that returns the chart to be rendered. This method will be called by the chart result.

Example

Code Block
langjava
titleStruts Action that provides a chart
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;
	}

}

...

Gallery
Code Block

public JFreeChart getChart() {
	return chart;
}

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

Code Block

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...

Code Block

<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.

Settings

This plugin doesn't provides any global settings.

Installation

This plugin can be installed by copying the plugin jar into your application's /WEB-INF/lib directory. The JFreeChart library will need to be downloaded separately, as its LGPL license doesn't allow it to be distributed with Struts.