KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > charts > RingChart


1 package dinamica.charts;
2
3 import dinamica.*;
4 import org.jfree.chart.*;
5 import org.jfree.data.general.*;
6 import org.jfree.util.*;
7 import org.jfree.chart.plot.Plot;
8 import org.jfree.chart.plot.RingPlot;
9 import org.jfree.chart.labels.*;
10
11 /**
12  * Chart plugin for Ring Chart (similar to a Pie chart)
13  * Last update: 23/july/2005
14  * @author Martin Cordova (dinamica@martincordova.com)
15  */

16 public class RingChart extends AbstractChartPlugin
17 {
18
19     /* (non-Javadoc)
20      * @see dinamica.AbstractChartPlugin#getChart(dinamica.Recordset, dinamica.Recordset)
21      */

22     public JFreeChart getChart(Recordset chartInfo, Recordset data)
23         throws Throwable JavaDoc
24     {
25
26         /* create a chart dataset using the data contained in the Recordset "data" */
27         DefaultPieDataset chartdata = new DefaultPieDataset();
28         data.top();
29         while (data.next())
30         {
31             String JavaDoc colx = (String JavaDoc)chartInfo.getValue("column-x");
32             String JavaDoc coly = (String JavaDoc)chartInfo.getValue("column-y");
33             Double JavaDoc value = new Double JavaDoc(String.valueOf(data.getValue(coly)));
34             
35             /* get label x */
36             String JavaDoc label = String.valueOf(data.getValue(colx));
37             
38             /* get value y */
39             if (value==null)
40                 value = new Double JavaDoc(0);
41             
42             /* feed chart dataset with values */
43             chartdata.setValue(label, value.doubleValue());
44         
45         }
46         
47         /* get chart params */
48         String JavaDoc title = (String JavaDoc)chartInfo.getValue("title");
49
50         /* create a chart */
51         JFreeChart chart = ChartFactory.createRingChart(title, chartdata, true, false, false);
52         
53         /* set pie decoration */
54         configurePlot( chart.getPlot() );
55
56         /* return chart */
57         return chart;
58
59     }
60
61     public void configurePlot(Plot p)
62     {
63         /* set pie decoration */
64         RingPlot plot = (RingPlot) p;
65         plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} = {2}"));
66         plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
67         plot.setStartAngle(290D);
68         plot.setDirection(Rotation.CLOCKWISE);
69         plot.setForegroundAlpha(0.6F);
70      }
71
72 }
73
Popular Tags