saveChartAsPNG() problem

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Pompilio
Posts: 2
Joined: Wed Apr 15, 2015 11:44 pm
antibot: No, of course not.

saveChartAsPNG() problem

Post by Pompilio » Thu Apr 16, 2015 12:07 am

I'm tired of looking for an answer but really can't found it, or at least the one that make my JSP code works properly.
Here's the thing, I want to create a chart and save it as PNG but doesn't work. This is the code:

Code: Select all

<%@ page import="org.jfree.chart.ChartFactory"%>
<%@ page import="org.jfree.chart.ChartUtilities"%>
<%@ page import="org.jfree.chart.JFreeChart"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset"%>
<%@ page import="java.io.File"%>
<%// Create a simple pie chart
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("Ubuntu", new Integer(75));
pieDataset.setValue("Xubuntu", new Integer(10));
pieDataset.setValue("Kubuntu", new Integer(10));
pieDataset.setValue("Others", new Integer(5));
JFreeChart chart = ChartFactory.createPieChart("Sistemas Operativos", pieDataset, true, true, false);
try {
 ChartUtilities.saveChartAsJPEG(new File("PieChart.jpg"), chart, 500, 300);
} catch (Exception e) {
 System.out.println("Error.");
}%>
<%
out.println("Hello");
out.println("Hello");
out.println("<script type=\"text/javascript\">alert(\"Hello\");</script>");
%>
As you can see, I have the chart code in the beginning, and at the end I have some out.prints just for testing. The next image show the result of the code above:
Image

And the prompt asking for "save file" never appears :cry: Neither the image automatically save it in the tmp directory or the one where the file is located (let's say /home/darwin/webapps/ROOT/test/).

Any ideas or help plz? I'm using CentOS 6... don't know what else do you need to know. Thank you!

Pompilio
Posts: 2
Joined: Wed Apr 15, 2015 11:44 pm
antibot: No, of course not.

Re: saveChartAsPNG() problem

Post by Pompilio » Thu Apr 16, 2015 10:59 pm

Ok, I'll answer myself and just in case anyone need this in the future.

Dunno why, but I had to write the full direction and not only the file name (I thought doesn't needed in order to save it in the directory where jsp file is located) and the successful result is the next one:

Code: Select all

ChartUtilities.saveChartAsPNG(new File("/home/darwin/webapps/ROOT/test/test.png"), chart, 500, 300);
Cya.

Locked