KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > charts > LineChart


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

16 public class LineChart 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 = chartInfo.getString("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         /* 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.doubleValue(), 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.createLineChart(
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        
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         LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)plot.getRenderer();
135         lineandshaperenderer.setShapesVisible(true);
136         lineandshaperenderer.setShapesFilled(true);
137         
138     }
139     
140 }
141
Popular Tags