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