1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.Color ; 41 import java.awt.GridLayout ; 42 43 import javax.swing.JPanel ; 44 45 import org.jfree.chart.ChartFactory; 46 import org.jfree.chart.ChartPanel; 47 import org.jfree.chart.JFreeChart; 48 import org.jfree.chart.Spacer; 49 import org.jfree.chart.axis.DateAxis; 50 import org.jfree.chart.plot.PlotOrientation; 51 import org.jfree.chart.plot.XYPlot; 52 import org.jfree.chart.renderer.StandardXYItemRenderer; 53 import org.jfree.data.XYDataset; 54 import org.jfree.data.time.Day; 55 import org.jfree.data.time.RegularTimePeriod; 56 import org.jfree.data.time.TimeSeries; 57 import org.jfree.data.time.TimeSeriesCollection; 58 import org.jfree.date.SerialDate; 59 import org.jfree.ui.ApplicationFrame; 60 import org.jfree.ui.RefineryUtilities; 61 62 67 public class TimeSeriesDemo11 extends ApplicationFrame { 68 69 74 public TimeSeriesDemo11(String title) { 75 76 super(title); 77 JPanel panel = new JPanel (new GridLayout (2, 2)); 78 panel.setPreferredSize(new java.awt.Dimension (800, 600)); 79 80 Day today = new Day(); 81 XYDataset dataset = createDataset("Series 1", 100.0, today, 365); 82 83 JFreeChart chart1 = createChart("Chart 1 : 1 Year", dataset); 84 ChartPanel chartPanel1 = new ChartPanel(chart1); 85 panel.add(chartPanel1); 86 87 JFreeChart chart2 = createChart("Chart 2 : 6 Months", dataset); 88 SerialDate t = today.getSerialDate(); 89 SerialDate t6m = SerialDate.addMonths(-6, t); 90 Day sixMonthsAgo = new Day(t6m); 91 DateAxis axis2 = (DateAxis) chart2.getXYPlot().getDomainAxis(); 92 axis2.setRange(sixMonthsAgo.getStart(), today.getEnd()); 93 ChartPanel chartPanel2 = new ChartPanel(chart2); 94 panel.add(chartPanel2); 95 96 JFreeChart chart3 = createChart("Chart 3 : 3 Months", dataset); 97 SerialDate t3m = SerialDate.addMonths(-3, t); 98 Day threeMonthsAgo = new Day(t3m); 99 DateAxis axis3 = (DateAxis) chart3.getXYPlot().getDomainAxis(); 100 axis3.setRange(threeMonthsAgo.getStart(), today.getEnd()); 101 ChartPanel chartPanel3 = new ChartPanel(chart3); 102 panel.add(chartPanel3); 103 104 JFreeChart chart4 = createChart("Chart 4 : 1 Month", dataset); 105 SerialDate t1m = SerialDate.addMonths(-1, t); 106 Day oneMonthsAgo = new Day(t1m); 107 DateAxis axis4 = (DateAxis) chart4.getXYPlot().getDomainAxis(); 108 axis4.setRange(oneMonthsAgo.getStart(), today.getEnd()); 109 ChartPanel chartPanel4 = new ChartPanel(chart4); 110 panel.add(chartPanel4); 111 112 setContentPane(panel); 113 114 } 115 116 127 135 private JFreeChart createChart(String title, XYDataset dataset) { 136 137 138 JFreeChart chart = ChartFactory.createTimeSeriesChart( 139 title, 140 "Date", 141 "Price", 142 dataset, 143 true, 144 true, 145 false 146 ); 147 148 chart.setBackgroundPaint(Color.white); 149 XYPlot plot = chart.getXYPlot(); 150 plot.setOrientation(PlotOrientation.VERTICAL); 151 plot.setBackgroundPaint(Color.lightGray); 152 plot.setDomainGridlinePaint(Color.white); 153 plot.setRangeGridlinePaint(Color.white); 154 plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); 155 156 StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer(); 157 renderer.setPaint(Color.blue); 158 159 return chart; 160 161 } 162 163 173 private XYDataset createDataset(String name, double base, RegularTimePeriod start, int count) { 174 175 TimeSeries series = new TimeSeries(name, start.getClass()); 176 RegularTimePeriod period = start; 177 double value = base; 178 for (int i = 0; i < count; i++) { 179 series.add(period, value); 180 period = period.previous(); 181 value = value * (1 + (Math.random() - 0.495) / 10.0); 182 } 183 184 TimeSeriesCollection dataset = new TimeSeriesCollection(); 185 dataset.addSeries(series); 186 187 return dataset; 188 189 } 190 191 196 public static void main(String [] args) { 197 198 TimeSeriesDemo11 demo = new TimeSeriesDemo11("Time Series Demo 11"); 199 demo.pack(); 200 RefineryUtilities.centerFrameOnScreen(demo); 201 demo.setVisible(true); 202 203 } 204 205 } 206 | Popular Tags |