1 38 39 package org.jfree.chart.demo; 40 41 import java.text.SimpleDateFormat ; 42 43 import org.jfree.chart.ChartFactory; 44 import org.jfree.chart.ChartPanel; 45 import org.jfree.chart.JFreeChart; 46 import org.jfree.chart.axis.DateAxis; 47 import org.jfree.chart.axis.DateTickUnit; 48 import org.jfree.chart.plot.XYPlot; 49 import org.jfree.chart.renderer.StandardXYItemRenderer; 50 import org.jfree.data.XYDataset; 51 import org.jfree.data.time.Month; 52 import org.jfree.data.time.TimeSeries; 53 import org.jfree.data.time.TimeSeriesCollection; 54 import org.jfree.ui.ApplicationFrame; 55 import org.jfree.ui.RefineryUtilities; 56 57 64 public class TimeSeriesDemo3 extends ApplicationFrame { 65 66 71 public TimeSeriesDemo3(String title) { 72 73 super(title); 74 75 TimeSeries series1 = new TimeSeries("Series 1", Month.class); 76 series1.add(new Month(1, 2002), 500.2); 77 series1.add(new Month(2, 2002), 694.1); 78 series1.add(new Month(3, 2002), 734.4); 79 series1.add(new Month(4, 2002), 453.2); 80 series1.add(new Month(5, 2002), 500.2); 81 series1.add(new Month(6, 2002), 345.6); 82 series1.add(new Month(7, 2002), 500.2); 83 series1.add(new Month(8, 2002), 694.1); 84 series1.add(new Month(9, 2002), 734.4); 85 series1.add(new Month(10, 2002), 453.2); 86 series1.add(new Month(11, 2002), 500.2); 87 series1.add(new Month(12, 2002), 345.6); 88 89 TimeSeries series2 = new TimeSeries("Series 2", Month.class); 90 series2.add(new Month(1, 2002), 234.1); 91 series2.add(new Month(2, 2002), 623.7); 92 series2.add(new Month(3, 2002), 642.5); 93 series2.add(new Month(4, 2002), 651.4); 94 series2.add(new Month(5, 2002), 643.5); 95 series2.add(new Month(6, 2002), 785.6); 96 series2.add(new Month(7, 2002), 234.1); 97 series2.add(new Month(8, 2002), 623.7); 98 series2.add(new Month(9, 2002), 642.5); 99 series2.add(new Month(10, 2002), 651.4); 100 series2.add(new Month(11, 2002), 643.5); 101 series2.add(new Month(12, 2002), 785.6); 102 103 TimeSeriesCollection dataset = new TimeSeriesCollection(); 104 dataset.addSeries(series1); 105 dataset.addSeries(series2); 106 107 JFreeChart chart = createChart(dataset); 108 109 ChartPanel chartPanel = new ChartPanel(chart); 110 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 111 setContentPane(chartPanel); 112 113 } 114 115 126 133 private JFreeChart createChart(XYDataset dataset) { 134 JFreeChart chart = ChartFactory.createTimeSeriesChart( 135 "Time Series Demo 3", 136 "Time", 137 "Value", 138 dataset, 139 true, 140 true, 141 false 142 ); 143 XYPlot plot = chart.getXYPlot(); 144 DateAxis axis = (DateAxis) plot.getDomainAxis(); 145 axis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1, 146 new SimpleDateFormat ("MMM-yyyy"))); 147 axis.setVerticalTickLabels(true); 148 149 StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer(); 150 renderer.setPlotShapes(true); 151 renderer.setSeriesShapesFilled(0, Boolean.TRUE); 152 renderer.setSeriesShapesFilled(1, Boolean.FALSE); 153 154 return chart; 155 } 156 157 162 public static void main(String [] args) { 163 164 TimeSeriesDemo3 demo = new TimeSeriesDemo3("Time Series Demo 3"); 165 demo.pack(); 166 RefineryUtilities.centerFrameOnScreen(demo); 167 demo.setVisible(true); 168 169 } 170 171 } 172 | Popular Tags |