KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > charts > TimeSeriesDiagram


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23  
24 package org.infoglue.deliver.util.charts;
25
26 import java.awt.Color JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.Calendar JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.dom4j.Document;
33 import org.dom4j.Element;
34 import org.dom4j.io.OutputFormat;
35 import org.dom4j.io.XMLWriter;
36 import org.infoglue.cms.util.dom.DOMBuilder;
37 import org.jfree.chart.ChartFactory;
38 import org.jfree.chart.JFreeChart;
39 import org.jfree.chart.StandardLegend;
40 import org.jfree.chart.axis.DateAxis;
41 import org.jfree.chart.axis.DateTickMarkPosition;
42 import org.jfree.chart.axis.DateTickUnit;
43 import org.jfree.chart.plot.XYPlot;
44 import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
45 import org.jfree.chart.renderer.xy.XYItemRenderer;
46 import org.jfree.data.time.Month;
47 import org.jfree.data.time.TimeSeries;
48 import org.jfree.data.time.TimeSeriesCollection;
49 import org.jfree.data.time.Week;
50 import org.jfree.data.xy.XYDataset;
51 import org.jfree.ui.Spacer;
52
53 /**
54  * An example of a time series chart. For the most part, default settings are used, except that
55  * the renderer is modified to show filled shapes (as well as lines) at each data point.
56  *
57  * @author David Gilbert
58  */

59 public class TimeSeriesDiagram implements XMLDataDiagram
60 {
61
62     private JFreeChart chart;
63     private String JavaDoc xmlData;
64     private String JavaDoc dateFormat;
65     private String JavaDoc header;
66     private String JavaDoc axisYHeader;
67     private String JavaDoc axisXHeader;
68     private String JavaDoc timeGranulariry;
69     
70     public JFreeChart getChart()
71     {
72         return chart;
73     }
74
75     public void setChart(JFreeChart chart)
76     {
77         this.chart = chart;
78     }
79
80
81     public void renderChart() throws Exception JavaDoc
82     {
83         XYDataset dataset = createDataset();
84
85         JFreeChart chart = createChart(dataset);
86         this.chart = chart;
87     }
88     
89     /**
90      * Creates a chart.
91      *
92      * @param dataset a dataset.
93      *
94      * @return A chart.
95      */

96     
97     private JFreeChart createChart(XYDataset dataset)
98     {
99         JFreeChart chart = ChartFactory.createTimeSeriesChart(header, axisXHeader, axisYHeader, dataset, true, true, false);
100         
101         chart.setBackgroundPaint(Color.white);
102
103         StandardLegend sl = (StandardLegend) chart.getLegend();
104         sl.setDisplaySeriesShapes(true);
105
106         XYPlot plot = chart.getXYPlot();
107         plot.setBackgroundPaint(Color.lightGray);
108         plot.setDomainGridlinePaint(Color.white);
109         plot.setRangeGridlinePaint(Color.white);
110         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
111         plot.setDomainCrosshairVisible(true);
112         plot.setRangeCrosshairVisible(true);
113         
114         XYItemRenderer renderer = plot.getRenderer();
115         if (renderer instanceof StandardXYItemRenderer) {
116             StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
117             rr.setPlotShapes(true);
118             rr.setShapesFilled(true);
119         }
120         
121         DateAxis axis = (DateAxis) plot.getDomainAxis();
122         
123         if(this.timeGranulariry.equalsIgnoreCase("Week"))
124         {
125             
126             DateTickUnit unit = new DateTickUnit(DateTickUnit.DAY, 7, Calendar.getInstance().getFirstDayOfWeek(), new SimpleDateFormat JavaDoc(this.dateFormat));
127             axis.setTickUnit(unit);
128             axis.setTickMarkPosition(DateTickMarkPosition.START);
129              
130             axis.setDateFormatOverride(new SimpleDateFormat JavaDoc(this.dateFormat));
131         }
132         else
133         {
134             axis.setDateFormatOverride(new SimpleDateFormat JavaDoc(this.dateFormat));
135         }
136         /*
137         DateAxis axis = (DateAxis) plot.getDomainAxis();
138         axis.setDateFormatOverride(new SimpleDateFormat(this.dateFormat));
139         */

140         return chart;
141
142     }
143     
144     /**
145      * Creates a dataset, consisting of two series of monthly data.
146      *
147      * @return the dataset.
148      */

149     private XYDataset createDataset() throws Exception JavaDoc
150     {
151         TimeSeriesCollection timeSeriesDataset = new TimeSeriesCollection();
152
153         Document document = new DOMBuilder().getDocument(this.xmlData);
154         this.writeDebug(document);
155         
156         Element headerElement = (Element)document.selectSingleNode("//chartHeader");
157         this.header = headerElement.getText();
158         Element axisYHeaderElement = (Element)document.selectSingleNode("//axisYHeader");
159         this.axisYHeader = axisYHeaderElement.getText();
160         Element axisXHeaderElement = (Element)document.selectSingleNode("//axisXHeader");
161         this.axisXHeader = axisXHeaderElement.getText();
162         Element timeGranularityElement = (Element)document.selectSingleNode("//timeGranularity");
163         this.timeGranulariry = timeGranularityElement.getText();
164         Element dateFormatElement = (Element)document.selectSingleNode("//dateFormat");
165         this.dateFormat = dateFormatElement.getText();
166                         
167         List JavaDoc series = document.selectNodes("//Series");
168         
169         Iterator JavaDoc seriesIterator = series.iterator();
170         while(seriesIterator.hasNext())
171         {
172             Element serieElement = (Element)seriesIterator.next();
173             String JavaDoc serieName = serieElement.attributeValue("name");
174             
175             TimeSeries s1 = null;
176             if(this.timeGranulariry.equalsIgnoreCase("Month"))
177                 s1 = new TimeSeries(serieName, Month.class);
178             else if(this.timeGranulariry.equalsIgnoreCase("Week"))
179                 s1 = new TimeSeries(serieName, Week.class);
180         
181             List JavaDoc items = serieElement.selectNodes("Item");
182             Iterator JavaDoc itemsIterator = items.iterator();
183             while(itemsIterator.hasNext())
184             {
185                 Element itemElement = (Element)itemsIterator.next();
186                 Element yearElement = (Element)itemElement.selectSingleNode("yearId");
187                 Element timeElement = (Element)itemElement.selectSingleNode("timeId");
188                 Element valueElement = (Element)itemElement.selectSingleNode("value");
189                 String JavaDoc year = yearElement.getText();
190                 String JavaDoc time = timeElement.getText();
191                 String JavaDoc value = valueElement.getText();
192                 
193                 if(this.timeGranulariry.equalsIgnoreCase("Month"))
194                     s1.add(new Month(new Integer JavaDoc(time).intValue(), new Integer JavaDoc(year).intValue()), new Float JavaDoc(value));
195                 else if(this.timeGranulariry.equalsIgnoreCase("Week"))
196                     s1.add(new Week(new Integer JavaDoc(time).intValue(), new Integer JavaDoc(year).intValue()), new Float JavaDoc(value));
197             }
198             
199
200
201             timeSeriesDataset.addSeries(s1);
202             //timeSeriesDataset.addSeries(s2);
203

204             timeSeriesDataset.setDomainIsPointsInTime(true);
205
206         }
207
208         return timeSeriesDataset;
209
210     }
211
212     /**
213      * This method takes xml as input for the diagram.
214      */

215     
216     public void setDiagramData(String JavaDoc xmlData)
217     {
218         this.xmlData = xmlData;
219     }
220     
221
222     /**
223      * This method creates a new Document from an xml-string.
224      */

225     /*
226     public Document getDocument(String xml) throws Exception
227     {
228         if(xml == null)
229             return null;
230                     
231         Document document = null;
232         
233         try
234         {
235             SAXReader xmlReader = new SAXReader();
236             document = xmlReader.read(new StringReader(xml));
237         }
238         catch(Exception e)
239         {
240             e.printStackTrace();
241         }
242         
243         return document;
244     }
245 */

246     /**
247      * This method writes a document to System.out.
248      */

249
250     public void writeDebug(Document document) throws Exception JavaDoc
251     {
252         OutputFormat format = OutputFormat.createPrettyPrint();
253         XMLWriter writer = new XMLWriter( System.out, format );
254         writer.write( document );
255     }
256 }
257
258
Popular Tags