01: package dinamica.charts;
02:
03: import dinamica.*;
04: import org.jfree.chart.*;
05: import org.jfree.data.general.*;
06: import org.jfree.chart.plot.*;
07: import org.jfree.chart.labels.*;
08:
09: /**
10: * Chart plugin for simple Pie
11: * Last update: 24/july/2005
12: * @author Martin Cordova (dinamica@martincordova.com)
13: */
14: public class PieChart extends AbstractChartPlugin {
15:
16: /* (non-Javadoc)
17: * @see dinamica.AbstractChartPlugin#getChart(dinamica.Recordset, dinamica.Recordset)
18: */
19: public JFreeChart getChart(Recordset chartInfo, Recordset data)
20: throws Throwable {
21:
22: /* create a chart dataset using the data contained in the Recordset "data" */
23: DefaultPieDataset chartdata = new DefaultPieDataset();
24: data.top();
25: while (data.next()) {
26: String colx = (String) chartInfo.getValue("column-x");
27: String coly = (String) chartInfo.getValue("column-y");
28: Double value = new Double(String.valueOf(data
29: .getValue(coly)));
30:
31: /* get label x */
32: String label = String.valueOf(data.getValue(colx));
33:
34: /* get value y */
35: if (value == null)
36: value = new Double(0);
37:
38: /* feed chart */
39: chartdata.setValue(label, value.doubleValue());
40:
41: }
42:
43: /* get chart params */
44: String title = (String) chartInfo.getValue("title");
45:
46: /* create a chart */
47: JFreeChart chart = ChartFactory.createPieChart(title,
48: chartdata, true, false, false);
49:
50: /* set pie decoration */
51: configurePlot(chart.getPlot());
52:
53: /* return chart */
54: return chart;
55:
56: }
57:
58: /**
59: * Configure chart decorations
60: */
61: public void configurePlot(Plot p) {
62:
63: PiePlot plot = (PiePlot) p;
64: StandardPieSectionLabelGenerator splg = new StandardPieSectionLabelGenerator(
65: "{2}");
66: splg.getPercentFormat().setMinimumFractionDigits(2);
67: plot.setLabelGenerator(splg);
68: plot.setForegroundAlpha(0.7F);
69:
70: }
71:
72: }
|