As the stream service is capable of streaming any kind of data, this stream asset comes in handy
when the stream is a image or some other resource that is to be treated as an asset.
An example is when a chart is to be rendered on a page, the resource source can stream the chart data
and the asset can be configured to call that specific resource source.
Tapestry Bayeux comes with a abstract
JFreeChartResource
(an implementation of the StreamResource)
that makes it easy to stream a JFreeChart.
A SimpleJFreeChartResource
implementation is also available. That implementation is sufficient in many cases.
The example below uses that implementation.
All you have to do is concentrate on creating your chart and bayeux will handle all the dirty work
for you.
Presuming you have defined bayeux library in you application under the library name "bayeux":
The following illustrates how you can implement you own resource source capable of streaming a JFreeChart.
package com.nordija.tapestry.bayeux.stream.jfreechart; public class JFreeChartResourceSource implements StreamResourceSource { public StreamResource getStreamResource(IRequestCycle cycle, Object[] parameters) throws IOException { //use the parameters to construct the chart you like DefaultKeyedValuesDataset dataset = new DefaultKeyedValuesDataset(); dataset.setValue("one", 50); dataset.setValue("two", 20); dataset.setValue("three", 30); JFreeChart chart = ChartFactory.createPieChart("My Piechart", dataset, false, false, false); return new SimpleJFreeChartResource(chart, 100, 200, ImageFormat.JPEG); } }
<extension name="bayeux.stream.myjfreechart" class="com.nordija.tapestry.bayeux.stream.jfreechart.JFreeChartResourceSource"/>
First we create a method that creates the StreamAsset. The method could be defined in the page class for which the image is to be rendered:
public StreamAsset getMyStreamAsset() { return new StreamAsset(getStreamService(), "myjfreechart", new Object[]{}, this); }
<inject property="streamService" object="service:com.nordija.tapestry.bayeux.StreamService"/>
<img jwcid="@Image" image="ognl:myStreamAsset" src="#" alt="stream asset" />