...
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
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<result-types> <result-type name="chart" class="myapp.webwork.extensions.ChartResult"/> </result-types> |
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<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 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
Code Block |
---|
public JFreeChart getChart() { return chart; } |
...
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.
...
But hey, the above works for now. Any suggestions are welcome.