Based on the Tapestry5HowToCreateASimpleGraphComponent this is how you can create a component that can be passed a JFreeChart object to allow you to create any kind of chart

To use add this to your page:

<t:chart width="200" height="150" chart="lineChart"/>

add this to your page class:

public JFreeChart getLineChart() {
                XYSeries series = new XYSeries("Average Size");
                series.add(20.0, 10.0);
                series.add(40.0, 20.0);
                series.add(70.0, 50.0);
                XYDataset xyDataset = new XYSeriesCollection(series);
                final JFreeChart chart =  ChartFactory.createXYLineChart
        ("Sample XY Chart",  // Title
                "Height",           // X-Axis label
                "Weight",           // Y-Axis label
                xyDataset,          // Dataset
                true,                // Show legend
                true,                            // Tooltips
                true                             // Urls

        return chart;

add this to your pom.xml under "dependencies":


(put this component class into yourApp.components package)

package com.liftyourgame.application.components;

import java.awt.image.BufferedImage;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.Link;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.dom.Element;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

public class Chart {
    private ComponentResources _resources;
    private TypeCoercer _coercer;

    /**list(array) of paired values(label & value): [String,Number,String,Number,...]*/
    private JFreeChart _chart;
    private JFreeChart _context;
    private int _width;

    private int _height;

    void beginRender(MarkupWriter writer)
        Object[] params = { new Integer(_width),
                            new Integer(_height) };
        //generate action link
        Link link = _resources.createEventLink("chart", params);
        Element img = writer.element("img", "src", link);
    void afterRender(MarkupWriter writer)

    public StreamResponse onChart(final int width, final int height/*, Object*/){
        return new StreamResponse(){
            public String getContentType(){
                return "image/jpeg";
            public InputStream getStream() throws IOException {
                BufferedImage image  = _context.createBufferedImage(width, height);
                ByteArrayOutputStream byteArray = new ByteArrayOutputStream() ;
                ChartUtilities.writeBufferedImageAsJPEG(byteArray, image) ;
                return new ByteArrayInputStream(byteArray.toByteArray());
            public void prepareResponse(Response response){}

Tapestry5HowToCreateGenericGraphComponent (last edited 2010-04-17 00:37:47 by Cs-32-214)