i want to generate dynamic chart images save them to a List object and display the images in the xhtml file.
I have created a small application to illustrate the problem.
Controller:
Code: Select all
public class DynamicPie {
public DynamicPie(){
}
public DefaultPieDataset createDataset(){
DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
defaultpiedataset.setValue("Java", new Double(43.200000000000003D));
defaultpiedataset.setValue("Visual Basic", new Double(10D));
defaultpiedataset.setValue("C/C++", new Double(17.5D));
defaultpiedataset.setValue("PHP", new Double(32.5D));
defaultpiedataset.setValue("Perl", null);
return defaultpiedataset;
}
public JFreeChart createPieCharts(DefaultPieDataset defaultpiedataset){
JFreeChart jfreechart = ChartFactory.createPieChart3D("Pie Chart 3D demo ", defaultpiedataset, true, true, false);
PiePlot3D pieplot3d = (PiePlot3D)jfreechart.getPlot();
pieplot3d.setStartAngle(290D);
pieplot3d.setDirection(Rotation.CLOCKWISE);
pieplot3d.setForegroundAlpha(0.5F);
pieplot3d.setNoDataMessage("No data to display");
return jfreechart;
}
public StreamedContent generateImages(JFreeChart chart){
StreamedContent pic = null;
File chartFile = new File("dynmaicPie");
try {
ChartUtilities.saveChartAsPNG(chartFile, chart, 680, 600);
} catch (IOException e) {
e.printStackTrace();
}
try {
pic = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return pic;
}
public List<StreamedContent> generateImageList(JFreeChart chart){
System.out.println("list Gen start");
StreamedContent pic = null;
List<StreamedContent> picList = new ArrayList<StreamedContent>();
for(int i=0; i<3; i++){
File chartFile = new File("dynmaicTest"+i);
try {
ChartUtilities.saveChartAsPNG(chartFile, chart, 680, 600);
} catch (IOException e) {
e.printStackTrace();
}
try {
pic = new DefaultStreamedContent(new FileInputStream(chartFile), "image/png");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
picList.add(pic);
}
System.out.println("list gen end");
return picList;
}
}
Code: Select all
@ManagedBean(name="dynamicPieBean")
@RequestScoped
public class DynamicPieBean implements Serializable{
private static final long serialVersionUID = 42L;
private StreamedContent image;
private List<StreamedContent> imageList;
public void generatePieChart(){
System.out.println("simple chart");
DynamicPie pie = new DynamicPie();
DefaultPieDataset dataset = pie.createDataset();
JFreeChart chart = pie.createPieCharts(dataset);
StreamedContent pic = pie.generateImages(chart);
this.image = pic;
}
public void generatePieList(){
System.out.println("test");
DynamicPie pie = new DynamicPie();
DefaultPieDataset dataset = pie.createDataset();
JFreeChart chart = pie.createPieCharts(dataset);
List<StreamedContent> picList = pie.generateImageList(chart);
this.imageList = picList;
}
public StreamedContent getImage() {
this.generatePieChart();
return image;
}
public void setImage(StreamedContent image) {
this.image = image;
}
public List<StreamedContent> getImageList() {
this.generatePieList();
return imageList;
}
public void setImageList(List<StreamedContent> imageList) {
this.imageList = imageList;
}
}
Code: Select all
<html xmlns="http://www.w3c.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>index</title>
</h:head>
<h:body>
<h1>single Pie image</h1>
<br />
<p:graphicImage value="#{dynamicPieBean.image}"/>
<br/>
<h1>Pie image list</h1>
<c:forEach var="image" items="#{dynamicPieBean.imageList}">
ListObjects: <p:graphicImage value="#{image}"/> <br />
</c:forEach>
</h:body>
</html>
-commons-fileupload-1.2.2.jar
-commons-io.-2.0.jar
-jcommon-1.0.16.jar
-jsf-api.jar
-jsf-impl.jar
-primefaces-2.1.jar
-slf4j-api-1.6.1.jar
As you can see on the image the single Streamed Content is diplayed. But the images in the list object aren't displayed.
Uploaded with ImageShack.us
Has anyone experienced a similar problem or is there a posibility to display List<StreamedContent> in the frontend?
Thanks.