1 38 39 package org.jfree.chart.demo; 40 41 import java.awt.Color ; 42 import java.text.SimpleDateFormat ; 43 44 import org.jfree.chart.ChartFactory; 45 import org.jfree.chart.ChartPanel; 46 import org.jfree.chart.JFreeChart; 47 import org.jfree.chart.Spacer; 48 import org.jfree.chart.StandardLegend; 49 import org.jfree.chart.axis.DateAxis; 50 import org.jfree.chart.plot.XYPlot; 51 import org.jfree.chart.renderer.StandardXYItemRenderer; 52 import org.jfree.chart.renderer.XYItemRenderer; 53 import org.jfree.data.XYDataset; 54 import org.jfree.data.time.Month; 55 import org.jfree.data.time.TimeSeries; 56 import org.jfree.data.time.TimeSeriesCollection; 57 import org.jfree.ui.ApplicationFrame; 58 import org.jfree.ui.RefineryUtilities; 59 60 66 public class TimeSeriesDemo extends ApplicationFrame { 67 68 74 public TimeSeriesDemo(String title) { 75 76 super(title); 77 78 XYDataset dataset = createDataset(); 79 80 JFreeChart chart = createChart(dataset); 81 82 ChartPanel chartPanel = new ChartPanel(chart); 83 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 84 chartPanel.setMouseZoomable(true, false); 85 setContentPane(chartPanel); 86 87 } 88 89 96 private JFreeChart createChart(XYDataset dataset) { 97 98 JFreeChart chart = ChartFactory.createTimeSeriesChart( 99 "Legal & General Unit Trust Prices", 100 "Date", "Price Per Unit", 101 dataset, 102 true, 103 true, 104 false 105 ); 106 107 chart.setBackgroundPaint(Color.white); 108 109 StandardLegend sl = (StandardLegend) chart.getLegend(); 110 sl.setDisplaySeriesShapes(true); 111 112 XYPlot plot = chart.getXYPlot(); 113 plot.setBackgroundPaint(Color.lightGray); 114 plot.setDomainGridlinePaint(Color.white); 115 plot.setRangeGridlinePaint(Color.white); 116 plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); 117 plot.setDomainCrosshairVisible(true); 118 plot.setRangeCrosshairVisible(true); 119 120 XYItemRenderer renderer = plot.getRenderer(); 121 if (renderer instanceof StandardXYItemRenderer) { 122 StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer; 123 rr.setPlotShapes(true); 124 rr.setShapesFilled(true); 125 } 126 127 DateAxis axis = (DateAxis) plot.getDomainAxis(); 128 axis.setDateFormatOverride(new SimpleDateFormat ("MMM-yyyy")); 129 130 return chart; 131 132 } 133 134 145 150 private XYDataset createDataset() { 151 152 TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class); 153 s1.add(new Month(2, 2001), 181.8); 154 s1.add(new Month(3, 2001), 167.3); 155 s1.add(new Month(4, 2001), 153.8); 156 s1.add(new Month(5, 2001), 167.6); 157 s1.add(new Month(6, 2001), 158.8); 158 s1.add(new Month(7, 2001), 148.3); 159 s1.add(new Month(8, 2001), 153.9); 160 s1.add(new Month(9, 2001), 142.7); 161 s1.add(new Month(10, 2001), 123.2); 162 s1.add(new Month(11, 2001), 131.8); 163 s1.add(new Month(12, 2001), 139.6); 164 s1.add(new Month(1, 2002), 142.9); 165 s1.add(new Month(2, 2002), 138.7); 166 s1.add(new Month(3, 2002), 137.3); 167 s1.add(new Month(4, 2002), 143.9); 168 s1.add(new Month(5, 2002), 139.8); 169 s1.add(new Month(6, 2002), 137.0); 170 s1.add(new Month(7, 2002), 132.8); 171 172 TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class); 173 s2.add(new Month(2, 2001), 129.6); 174 s2.add(new Month(3, 2001), 123.2); 175 s2.add(new Month(4, 2001), 117.2); 176 s2.add(new Month(5, 2001), 124.1); 177 s2.add(new Month(6, 2001), 122.6); 178 s2.add(new Month(7, 2001), 119.2); 179 s2.add(new Month(8, 2001), 116.5); 180 s2.add(new Month(9, 2001), 112.7); 181 s2.add(new Month(10, 2001), 101.5); 182 s2.add(new Month(11, 2001), 106.1); 183 s2.add(new Month(12, 2001), 110.3); 184 s2.add(new Month(1, 2002), 111.7); 185 s2.add(new Month(2, 2002), 111.0); 186 s2.add(new Month(3, 2002), 109.6); 187 s2.add(new Month(4, 2002), 113.2); 188 s2.add(new Month(5, 2002), 111.6); 189 s2.add(new Month(6, 2002), 108.8); 190 s2.add(new Month(7, 2002), 101.6); 191 192 TimeSeriesCollection dataset = new TimeSeriesCollection(); 193 dataset.addSeries(s1); 194 dataset.addSeries(s2); 195 196 dataset.setDomainIsPointsInTime(true); 197 198 return dataset; 199 200 } 201 202 207 public static void main(String [] args) { 208 209 TimeSeriesDemo demo = new TimeSeriesDemo("Time Series Demo 1"); 210 demo.pack(); 211 RefineryUtilities.centerFrameOnScreen(demo); 212 demo.setVisible(true); 213 214 } 215 216 } 217 | Popular Tags |