1 43 44 package org.jfree.chart.demo; 45 46 import java.awt.Color ; 47 import java.text.SimpleDateFormat ; 48 49 import javax.swing.JPanel ; 50 51 import org.jfree.chart.ChartFactory; 52 import org.jfree.chart.ChartPanel; 53 import org.jfree.chart.JFreeChart; 54 import org.jfree.chart.axis.DateAxis; 55 import org.jfree.chart.plot.XYPlot; 56 import org.jfree.chart.renderer.xy.XYItemRenderer; 57 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 58 import org.jfree.data.time.Month; 59 import org.jfree.data.time.TimeSeries; 60 import org.jfree.data.time.TimeSeriesCollection; 61 import org.jfree.data.xy.XYDataset; 62 import org.jfree.ui.ApplicationFrame; 63 import org.jfree.ui.RectangleInsets; 64 import org.jfree.ui.RefineryUtilities; 65 66 71 public class TimeSeriesChartDemo1 extends ApplicationFrame { 72 73 79 public TimeSeriesChartDemo1(String title) { 80 super(title); 81 ChartPanel chartPanel = (ChartPanel) createDemoPanel(); 82 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 83 chartPanel.setMouseZoomable(true, false); 84 setContentPane(chartPanel); 85 } 86 87 94 private static JFreeChart createChart(XYDataset dataset) { 95 96 JFreeChart chart = ChartFactory.createTimeSeriesChart( 97 "Legal & General Unit Trust Prices", "Date", "Price Per Unit", dataset, true, true, false ); 105 106 chart.setBackgroundPaint(Color.white); 107 108 XYPlot plot = (XYPlot) chart.getPlot(); 109 plot.setBackgroundPaint(Color.lightGray); 110 plot.setDomainGridlinePaint(Color.white); 111 plot.setRangeGridlinePaint(Color.white); 112 plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); 113 plot.setDomainCrosshairVisible(true); 114 plot.setRangeCrosshairVisible(true); 115 116 XYItemRenderer r = plot.getRenderer(); 117 if (r instanceof XYLineAndShapeRenderer) { 118 XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r; 119 renderer.setBaseShapesVisible(true); 120 renderer.setBaseShapesFilled(true); 121 } 122 123 DateAxis axis = (DateAxis) plot.getDomainAxis(); 124 axis.setDateFormatOverride(new SimpleDateFormat ("MMM-yyyy")); 125 126 return chart; 127 128 } 129 130 135 private static XYDataset createDataset() { 136 137 TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class); 138 s1.add(new Month(2, 2001), 181.8); 139 s1.add(new Month(3, 2001), 167.3); 140 s1.add(new Month(4, 2001), 153.8); 141 s1.add(new Month(5, 2001), 167.6); 142 s1.add(new Month(6, 2001), 158.8); 143 s1.add(new Month(7, 2001), 148.3); 144 s1.add(new Month(8, 2001), 153.9); 145 s1.add(new Month(9, 2001), 142.7); 146 s1.add(new Month(10, 2001), 123.2); 147 s1.add(new Month(11, 2001), 131.8); 148 s1.add(new Month(12, 2001), 139.6); 149 s1.add(new Month(1, 2002), 142.9); 150 s1.add(new Month(2, 2002), 138.7); 151 s1.add(new Month(3, 2002), 137.3); 152 s1.add(new Month(4, 2002), 143.9); 153 s1.add(new Month(5, 2002), 139.8); 154 s1.add(new Month(6, 2002), 137.0); 155 s1.add(new Month(7, 2002), 132.8); 156 157 TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class); 158 s2.add(new Month(2, 2001), 129.6); 159 s2.add(new Month(3, 2001), 123.2); 160 s2.add(new Month(4, 2001), 117.2); 161 s2.add(new Month(5, 2001), 124.1); 162 s2.add(new Month(6, 2001), 122.6); 163 s2.add(new Month(7, 2001), 119.2); 164 s2.add(new Month(8, 2001), 116.5); 165 s2.add(new Month(9, 2001), 112.7); 166 s2.add(new Month(10, 2001), 101.5); 167 s2.add(new Month(11, 2001), 106.1); 168 s2.add(new Month(12, 2001), 110.3); 169 s2.add(new Month(1, 2002), 111.7); 170 s2.add(new Month(2, 2002), 111.0); 171 s2.add(new Month(3, 2002), 109.6); 172 s2.add(new Month(4, 2002), 113.2); 173 s2.add(new Month(5, 2002), 111.6); 174 s2.add(new Month(6, 2002), 108.8); 175 s2.add(new Month(7, 2002), 101.6); 176 177 185 TimeSeriesCollection dataset = new TimeSeriesCollection(); 186 dataset.addSeries(s1); 187 dataset.addSeries(s2); 188 189 return dataset; 190 191 } 192 193 198 public static JPanel createDemoPanel() { 199 JFreeChart chart = createChart(createDataset()); 200 return new ChartPanel(chart); 201 } 202 203 208 public static void main(String [] args) { 209 210 TimeSeriesChartDemo1 demo = new TimeSeriesChartDemo1( 211 "Time Series Chart Demo 1" ); 212 demo.pack(); 213 RefineryUtilities.centerFrameOnScreen(demo); 214 demo.setVisible(true); 215 216 } 217 218 } 219 | Popular Tags |