1 23 24 package org.infoglue.deliver.util.charts; 25 26 import java.awt.Color ; 27 import java.text.SimpleDateFormat ; 28 import java.util.Calendar ; 29 import java.util.Iterator ; 30 import java.util.List ; 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 59 public class TimeSeriesDiagram implements XMLDataDiagram 60 { 61 62 private JFreeChart chart; 63 private String xmlData; 64 private String dateFormat; 65 private String header; 66 private String axisYHeader; 67 private String axisXHeader; 68 private String 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 82 { 83 XYDataset dataset = createDataset(); 84 85 JFreeChart chart = createChart(dataset); 86 this.chart = chart; 87 } 88 89 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 (this.dateFormat)); 127 axis.setTickUnit(unit); 128 axis.setTickMarkPosition(DateTickMarkPosition.START); 129 130 axis.setDateFormatOverride(new SimpleDateFormat (this.dateFormat)); 131 } 132 else 133 { 134 axis.setDateFormatOverride(new SimpleDateFormat (this.dateFormat)); 135 } 136 140 return chart; 141 142 } 143 144 149 private XYDataset createDataset() throws Exception 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 series = document.selectNodes("//Series"); 168 169 Iterator seriesIterator = series.iterator(); 170 while(seriesIterator.hasNext()) 171 { 172 Element serieElement = (Element)seriesIterator.next(); 173 String 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 items = serieElement.selectNodes("Item"); 182 Iterator 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 year = yearElement.getText(); 190 String time = timeElement.getText(); 191 String value = valueElement.getText(); 192 193 if(this.timeGranulariry.equalsIgnoreCase("Month")) 194 s1.add(new Month(new Integer (time).intValue(), new Integer (year).intValue()), new Float (value)); 195 else if(this.timeGranulariry.equalsIgnoreCase("Week")) 196 s1.add(new Week(new Integer (time).intValue(), new Integer (year).intValue()), new Float (value)); 197 } 198 199 200 201 timeSeriesDataset.addSeries(s1); 202 204 timeSeriesDataset.setDomainIsPointsInTime(true); 205 206 } 207 208 return timeSeriesDataset; 209 210 } 211 212 215 216 public void setDiagramData(String xmlData) 217 { 218 this.xmlData = xmlData; 219 } 220 221 222 225 246 249 250 public void writeDebug(Document document) throws Exception 251 { 252 OutputFormat format = OutputFormat.createPrettyPrint(); 253 XMLWriter writer = new XMLWriter( System.out, format ); 254 writer.write( document ); 255 } 256 } 257 258 | Popular Tags |