1 37 38 package org.jfree.chart.demo; 39 40 import java.text.SimpleDateFormat ; 41 42 import org.jfree.chart.ChartFactory; 43 import org.jfree.chart.ChartPanel; 44 import org.jfree.chart.JFreeChart; 45 import org.jfree.chart.StandardLegend; 46 import org.jfree.chart.axis.DateAxis; 47 import org.jfree.chart.plot.XYPlot; 48 import org.jfree.chart.renderer.StandardXYItemRenderer; 49 import org.jfree.chart.renderer.XYItemRenderer; 50 import org.jfree.data.MovingAverage; 51 import org.jfree.data.XYDataset; 52 import org.jfree.data.time.Month; 53 import org.jfree.data.time.TimeSeries; 54 import org.jfree.data.time.TimeSeriesCollection; 55 import org.jfree.ui.ApplicationFrame; 56 import org.jfree.ui.RefineryUtilities; 57 58 63 public class MovingAverageDemo extends ApplicationFrame { 64 65 70 public MovingAverageDemo(String title) { 71 72 super(title); 73 74 String chartTitle = "Legal & General Unit Trust Prices"; 76 XYDataset dataset = createDataset(); 77 78 JFreeChart chart = ChartFactory.createTimeSeriesChart( 79 chartTitle, 80 "Date", 81 "Price Per Unit", 82 dataset, 83 true, 84 true, 85 false 86 ); 87 88 StandardLegend legend = (StandardLegend) chart.getLegend(); 89 legend.setDisplaySeriesShapes(true); 90 XYPlot plot = chart.getXYPlot(); 91 XYItemRenderer renderer = plot.getRenderer(); 92 if (renderer instanceof StandardXYItemRenderer) { 93 StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer; 94 rr.setPlotShapes(true); 95 rr.setShapesFilled(true); 96 } 97 DateAxis axis = (DateAxis) plot.getDomainAxis(); 98 axis.setDateFormatOverride(new SimpleDateFormat ("MMM-yyyy")); 99 ChartPanel chartPanel = new ChartPanel(chart); 100 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 101 setContentPane(chartPanel); 102 103 } 104 105 110 public XYDataset createDataset() { 111 112 TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class); 113 s1.add(new Month(2, 2001), 181.8); 114 s1.add(new Month(3, 2001), 167.3); 115 s1.add(new Month(4, 2001), 153.8); 116 s1.add(new Month(5, 2001), 167.6); 117 s1.add(new Month(6, 2001), 158.8); 118 s1.add(new Month(7, 2001), 148.3); 119 s1.add(new Month(8, 2001), 153.9); 120 s1.add(new Month(9, 2001), 142.7); 121 s1.add(new Month(10, 2001), 123.2); 122 s1.add(new Month(11, 2001), 131.8); 123 s1.add(new Month(12, 2001), 139.6); 124 s1.add(new Month(1, 2002), 142.9); 125 s1.add(new Month(2, 2002), 138.7); 126 s1.add(new Month(3, 2002), 137.3); 127 s1.add(new Month(4, 2002), 143.9); 128 s1.add(new Month(5, 2002), 139.8); 129 s1.add(new Month(6, 2002), 137.0); 130 s1.add(new Month(7, 2002), 132.8); 131 132 143 TimeSeries s2 = MovingAverage.createMovingAverage(s1, "Six Month Moving Average", 6, 0); 144 145 TimeSeriesCollection dataset = new TimeSeriesCollection(); 146 dataset.addSeries(s1); 147 dataset.addSeries(s2); 148 149 return dataset; 150 151 } 152 153 158 public static void main(String [] args) { 159 160 MovingAverageDemo demo = new MovingAverageDemo("Moving Average Demo 1"); 161 demo.pack(); 162 RefineryUtilities.centerFrameOnScreen(demo); 163 demo.setVisible(true); 164 165 } 166 167 } 168 | Popular Tags |