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:


inject ComponentResources, and TypeCoercer

    private ComponentResources _resources;
    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{
        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);
        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}"/>

Tapestry5HowToCreatePieChartsInAPage (last edited 2009-09-20 23:20:57 by localhost)