Charts in Seam

From the Seam Forums:

Using JFreeChart with Seam can be quite useful:

package example.chartprocesser;

@Name("chartprocesser")
public class ChartProcesserBean {

    @In
    EntityManager em; // Entity Manager to access the database

    byte[] chart; // chart image (.png) as a byte array

    @Factory(value="chart")
    public void createChart() {

        DefaultCategoryDataset ds = this.getData;

        JFreeChart chart = ChartFactory.createLineChart(
                "TITLE",
                "Category Label",
                "Axis Lable",
                ds,
                PlotOrientation.HORIZONTAL,
                false,
                false,
                false
                );

        try{
     	    this.chart = ChartUtilities.encodeAsPNG(chart.createBufferedImage(400, 400));
        } catch (IOException e){
	    e.printStackTrace();
        }

    }

    private DefaultCategoryDataset getData(){
        //get the data and put into DefaultCategoryDataset
        //Then return it.
    }
}

In your JSF page:

<s:graphicImage value="#{chartprocesser.chart}" />

5 comments

  1. Hi Elengado,

    When I copy your source code and deploy application it display error that couldn’t find library of jfreechart but when I remove @Name(“chartprocesser”) then it didn’t display error.

    Can you help me resolve it because I have copied jfreechart and jcommon into java classpath

  2. so, what’s the point of this article then? Just to show that you was able to render a jfreechart on Seam?!

    Congratulations dude!!!!

    ps: lehoa, do you have those two libraries on both EAR project and the WEB project?

  3. Yup, that’s the point. This is a quick reference to how I would do this again without having to sift through irrelevant documentation.

Comments are closed.