KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > charts > VerticalBarChart3D


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 import org.jfree.chart.renderer.category.BarRenderer3D;
9
10
11 /**
12  * Chart plugin for Bar3D charts with multiseries support
13  * Last update: 18/11/2003
14  * @author Martin Cordova (dinamica@martincordova.com)
15  */

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

124     public void configurePlot(Plot p)
125     {
126
127         CategoryPlot plot = (CategoryPlot)p;
128        
129         plot.setForegroundAlpha(0.8F);
130         plot.setBackgroundPaint(Color.WHITE);
131         plot.setRangeGridlinePaint(Color.BLACK);
132         plot.setDomainGridlinePaint(Color.BLACK);
133         plot.setDomainGridlinesVisible(true);
134         plot.setRangeGridlinesVisible(true);
135
136         BarRenderer3D barrenderer3d = (BarRenderer3D)plot.getRenderer();
137         barrenderer3d.setDrawBarOutline(false);
138         
139     }
140     
141 }
142
Popular Tags