Using JFreeChart with Seam

Posted by Jerzy Seweryn on
Graphic statistics in Seam with JFreeChart.
[code lang="java"]
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.
}
}
[/code]
In your JSF page:
[code lang="xml"]
<s:graphicImage value="#{chartprocesser.chart}" />
[/code]