An example of dynamic charts inside a page(this can also be used for other dynamic images).
(This is similar to Tapestry5HowToCreateASimpleGraphComponent)
add this to your pom.xml under dependencies:
<dependency> <groupId>jfree</groupId> <artifactId>jfreechart</artifactId> <version>1.0.5</version> </dependency>
inject ComponentResources, and TypeCoercer
@Inject private ComponentResources _resources; @Inject private TypeCoercer _coercer;
add the method that generates an ActionLink URL for chart event:
public Link getChart3(){ return _resources.createActionLink("chart", false, new Object[]{"600","400","aa","39","bb","12","cc","12","dd","4"}); }
add action listener for that chart event:
public StreamResponse onChart(final int width, final int height, Object ...rest){ DefaultKeyedValues values = new DefaultKeyedValues(); for (int i = 3; i < rest.length; i+=2){ values.addValue(rest[i-1].toString(), _coercer.coerce(rest[i], Number.class)); } PieDataset pieDataset = new DefaultPieDataset(values); PiePlot3D plot = new PiePlot3D(pieDataset); plot.setForegroundAlpha(0.5f); plot.setDepthFactor(0.1); plot.setCircular(true); final JFreeChart chart = new JFreeChart(plot); return new StreamResponse(){ public String getContentType(){ return "image/jpeg"; } public InputStream getStream() throws IOException { BufferedImage image = chart.createBufferedImage(width, height); ByteArrayOutputStream byteArray = new ByteArrayOutputStream() ; ChartUtilities.writeBufferedImageAsJPEG(byteArray, image) ; return new ByteArrayInputStream(byteArray.toByteArray()); } public void prepareResponse(Response response){} }; }
add following code to your page template
<img src="${chart3}"/>