KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > charts > LineChart3D


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 Line charts 3D with multiseries support
12  * Last update: 24/july/2005
13  * @author Martin Cordova (dinamica@martincordova.com)
14  */

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

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