Differences between revisions 2 and 3
Revision 2 as of 2007-07-09 18:07:28
Size: 2179
Editor: DavorHrg
Comment:
Revision 3 as of 2009-09-20 23:20:57
Size: 2179
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
An example of dynamic charts inside a page(this can also be used for other dynamic images).[[BR]]
(This is similar to ["Tapestry5HowToCreateASimpleGraphComponent"])
An example of dynamic charts inside a page(this can also be used for other dynamic images).<<BR>>
(This is similar to [[Tapestry5HowToCreateASimpleGraphComponent]])

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}"/>

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