How to -- org.jfree.chart.demo.PieChartDemo4

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
phani_vuppala
Posts: 4
Joined: Fri Apr 09, 2004 8:28 am

How to -- org.jfree.chart.demo.PieChartDemo4

Post by phani_vuppala » Fri Apr 09, 2004 2:35 pm

Hi :

I am using JFreeChart 0.9.17.

I want to use org.jfree.chart.demo.PieChartDemo4 in my web application. I am able to display the graph but not able to rotate it.

Please help me to rotate the graph...

How can org.jfree.chart.demo.PieChartDemo4 be shown in my jsp/servlet?

follwoing is my code written in a class file...

public static String generatePieChart(Date hitDate, HttpSession session, PrintWriter pw) {
String filename = null;
try {



PieDataset dataset = createDataset(14);

JFreeChart chart = ChartFactory.createPieChart(
"Pie Chart Demo 4", // chart title
dataset, // dataset
false, // include legend
true,
false
);



chart.setBackgroundPaint(new Color(222, 222, 255));
PiePlot plot = (PiePlot) chart.getPlot();
plot.setBackgroundPaint(Color.white);
plot.setCircular(true);
plot.setLabelGenerator(new StandardPieItemLabelGenerator(
"{0} = {2}", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()
));
plot.setNoDataMessage("No data available");


Rotator rotator = new Rotator(plot);
rotator.start();


// Write the chart image to the temporary directory
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);

// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info);
pw.flush();







} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_500x300.png";
}
return filename;
}


private static PieDataset createDataset(int sections) {
DefaultPieDataset result = new DefaultPieDataset();
for (int i = 0; i < sections; i++) {
double value = 100.0 * Math.random();
result.setValue("Section " + i, value);
}
return result;
}





static class Rotator extends Timer implements ActionListener {

private PiePlot plot;

private int angle = 270;

Rotator(PiePlot plot) {
super(100, null);
this.plot = plot;
addActionListener(this);
}

public void actionPerformed(ActionEvent event) {
this.plot.setStartAngle(angle);
this.angle = this.angle + 1;
if (this.angle == 360) {
this.angle = 0;
}
}

}

garv
Posts: 127
Joined: Wed Mar 31, 2004 3:44 pm
Location: Amsterdam, The Netherlands

Re: How to -- org.jfree.chart.demo.PieChartDemo4

Post by garv » Sun Apr 11, 2004 12:14 pm

phani_vuppala wrote:How can org.jfree.chart.demo.PieChartDemo4 be shown in my jsp/servlet?
Take a look at this topic.

phani_vuppala
Posts: 4
Joined: Fri Apr 09, 2004 8:28 am

Problem - is with rotation

Post by phani_vuppala » Mon Apr 12, 2004 5:51 am

Hi Garv :

Thank you for the reply..

I am able to display the image in my web applcation. But the problem is i am not able to rotate the image. i.e. org.jfree.chart.demo.PieChartDemo4

Please take a look at the org.jfree.chart.demo.PieChartDemo4 and let me know what to do to keep the piechart rotating in my web page.

An example will be greatly appreciated....

Thanking you,

Best Regards,
V. Phani

garv
Posts: 127
Joined: Wed Mar 31, 2004 3:44 pm
Location: Amsterdam, The Netherlands

Post by garv » Tue Apr 13, 2004 9:19 am

AWT's Timer/ActionListener model doesn't extend to PNG images I'm afraid.

If you want to rotate the chart on your web page, you'll have to either:

(a) use a Java Applet to display the chart using ChartPanel, see here. This is the easiest for you to implement, but I can't really recommend it because your users will have to download Sun's java plugin and you're bound to get loads of questions about that.

or

(b) use JavaScript's setInterval() to periodically download a new, rotated image from the server (which I can't recommend either because of bandwidth use and probably several other inconveniences)

or

(c) periodically generate six or eight or whatever different rotated images at once, save them as files in a designated directory on the webserver and have the browser download those (or write/find some code to merge them into a single animated gif file and serve that). Clearly, this is only practical if you update the charts at most -say- every half hour or so.

(IMHO: ) All things considered, the best advice I can give you is to simply forget about displaying a rotating chart on a web page. It's impractical to do on a web application, and it really doesn't help make the chart any easier to read.

If you must do it, the above are your options as far as I can see them. Option (a) is the easiest for you to implement, but you're likely to end up with a great deal of users that can't see the chart at all. Option (c) is the best one in the context of a web application.

Locked