KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > charts > VerticalBarChart


1 package dinamica.charts;
2
3 import java.awt.Color JavaDoc;
4 import dinamica.*;
5 import org.jfree.chart.*;
6 import org.jfree.data.category.*;
7 import org.jfree.chart.plot.*;
8
9
10 /**
11  * Chart plugin for Bar 2D charts with multiseries support
12  * Last update: 18/11/2003
13  * @author Martin Cordova (dinamica@martincordova.com)
14  */

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

21     public JFreeChart getChart(Recordset chartInfo, Recordset data)
22         throws Throwable JavaDoc
23     {
24
25         /* get date format for x-axis if required */
26         String JavaDoc dateFormat = (String JavaDoc)chartInfo.getValue("dateformat");
27
28         /* create a chart dataset using the data contained in the Recordset "data" */
29         DefaultCategoryDataset chartdata = new DefaultCategoryDataset();
30         
31         /* get series labels - if any */
32         String JavaDoc series[] = null;
33         String JavaDoc seriesLabels = (String JavaDoc)chartInfo.getValue("title-series");
34         if (seriesLabels!=null)
35             series = StringUtil.split(seriesLabels, ";");
36         else
37         {
38             series = new String JavaDoc[1];
39             series[0] = "";
40         }
41             
42
43         /* are there multiple series? */
44         String JavaDoc dataCols[] = null;
45         String JavaDoc coly = (String JavaDoc)chartInfo.getValue("column-y");
46         if (coly.indexOf(";")>0)
47             dataCols = StringUtil.split(coly, ";");
48         else
49         {
50             dataCols = new String JavaDoc[1];
51             dataCols[0] = coly;
52         }
53         
54         
55         /* navigate the recordset and feed the chart dataset */
56         data.top();
57         while (data.next())
58         {
59             
60             /* get label x */
61             String JavaDoc colx = (String JavaDoc)chartInfo.getValue("column-x");
62             RecordsetField f = data.getField(colx);
63             String JavaDoc label = null;
64             if (f.getType()==java.sql.Types.DATE)
65                 label = StringUtil.formatDate((java.util.Date JavaDoc)data.getValue(colx), dateFormat);
66             else
67                 label = String.valueOf(data.getValue(colx));
68
69             /* get value y for each serie */
70             for (int i=0;i<dataCols.length;i++)
71             {
72                 Double JavaDoc value = new Double JavaDoc(String.valueOf(data.getValue(dataCols[i])));
73                 if (value==null)
74                     value = new Double JavaDoc(0);
75
76                 chartdata.addValue(value, series[i], label);
77
78             }
79         
80         }
81         
82         /* get chart params */
83         String JavaDoc title = (String JavaDoc)chartInfo.getValue("title");
84         String JavaDoc titlex = (String JavaDoc)chartInfo.getValue("title-x");
85         String JavaDoc titley = (String JavaDoc)chartInfo.getValue("title-y");
86
87         /* if there is more than 1 series then use legends */
88         boolean useLegend = (dataCols.length>1);
89
90         /* create a chart */
91         JFreeChart chart = ChartFactory.createBarChart(
92             title, // chart title
93
titlex, // domain axis label
94
titley, // range axis label
95
chartdata, // data
96
PlotOrientation.VERTICAL, // orientation
97
useLegend, // include legend
98
false, // tooltips
99
false // urls
100
);
101         
102         /* set chart decoration */
103         configurePlot( chart.getPlot() );
104         
105         //PATCH 2005-07-19 - support for custom default color
106
//for single series charts - line, bar and area only
107
String JavaDoc color = chartInfo.getString("color");
108         if (!useLegend && color!=null)
109         {
110             CategoryPlot p = (CategoryPlot)chart.getPlot();
111             p.getRenderer().setSeriesPaint(0, Color.decode(color));
112         }
113         
114         /* return chart */
115         return chart;
116         
117
118     }
119
120     /**
121      * Configure chart decorations
122      */

123     public void configurePlot(Plot p)
124     {
125
126         CategoryPlot plot = (CategoryPlot)p;
127         plot.setBackgroundPaint(Color.WHITE);
128         plot.setRangeGridlinePaint(Color.BLACK);
129         plot.setDomainGridlinePaint(Color.BLACK);
130         plot.setDomainGridlinesVisible(true);
131         plot.setRangeGridlinesVisible(true);
132         
133     }
134     
135 }
136
Popular Tags