1 38 39 package org.jfree.chart.demo; 40 41 import java.text.SimpleDateFormat ; 42 43 import org.jfree.chart.ChartPanel; 44 import org.jfree.chart.JFreeChart; 45 import org.jfree.chart.axis.DateAxis; 46 import org.jfree.chart.axis.DateTickUnit; 47 import org.jfree.chart.axis.NumberAxis; 48 import org.jfree.chart.axis.ValueAxis; 49 import org.jfree.chart.plot.XYPlot; 50 import org.jfree.chart.renderer.StandardXYItemRenderer; 51 import org.jfree.chart.renderer.XYBarRenderer; 52 import org.jfree.chart.renderer.XYItemRenderer; 53 import org.jfree.data.XYDataset; 54 import org.jfree.data.time.Day; 55 import org.jfree.data.time.Hour; 56 import org.jfree.data.time.Minute; 57 import org.jfree.data.time.SimpleTimePeriod; 58 import org.jfree.data.time.TimePeriodValues; 59 import org.jfree.data.time.TimePeriodValuesCollection; 60 import org.jfree.ui.ApplicationFrame; 61 import org.jfree.ui.RefineryUtilities; 62 63 68 public class TimePeriodValuesDemo extends ApplicationFrame { 69 70 75 public TimePeriodValuesDemo(String title) { 76 77 super(title); 78 79 XYDataset data1 = createDataset1(); 80 XYItemRenderer renderer1 = new XYBarRenderer(); 81 82 DateAxis domainAxis = new DateAxis("Date"); 83 domainAxis.setVerticalTickLabels(true); 84 domainAxis.setTickUnit(new DateTickUnit(DateTickUnit.HOUR, 1)); 85 domainAxis.setDateFormatOverride(new SimpleDateFormat ("hh:mm")); 86 domainAxis.setLowerMargin(0.01); 87 domainAxis.setUpperMargin(0.01); 88 ValueAxis rangeAxis = new NumberAxis("Value"); 89 90 XYPlot plot = new XYPlot(data1, domainAxis, rangeAxis, renderer1); 91 92 XYDataset data2 = createDataset2(); 93 StandardXYItemRenderer renderer2 94 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES_AND_LINES); 95 renderer2.setShapesFilled(true); 96 97 plot.setSecondaryDataset(0, data2); 98 plot.setSecondaryRenderer(0, renderer2); 99 101 JFreeChart chart = new JFreeChart("Supply and Demand", plot); 102 ChartPanel chartPanel = new ChartPanel(chart); 103 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 104 chartPanel.setMouseZoomable(true, false); 105 setContentPane(chartPanel); 106 107 } 108 109 120 125 public XYDataset createDataset1() { 126 127 TimePeriodValues s1 = new TimePeriodValues("Supply"); 128 TimePeriodValues s2 = new TimePeriodValues("Demand"); 129 Day today = new Day(); 130 for (int i = 0; i < 24; i++) { 131 Minute m0 = new Minute(0, new Hour(i, today)); 132 Minute m1 = new Minute(15, new Hour(i, today)); 133 Minute m2 = new Minute(30, new Hour(i, today)); 134 Minute m3 = new Minute(45, new Hour(i, today)); 135 Minute m4 = new Minute(0, new Hour(i + 1, today)); 136 s1.add(new SimpleTimePeriod(m0.getStart(), m1.getStart()), Math.random()); 137 s2.add(new SimpleTimePeriod(m1.getStart(), m2.getStart()), Math.random()); 138 s1.add(new SimpleTimePeriod(m2.getStart(), m3.getStart()), Math.random()); 139 s2.add(new SimpleTimePeriod(m3.getStart(), m4.getStart()), Math.random()); 140 } 141 142 TimePeriodValuesCollection dataset = new TimePeriodValuesCollection(); 143 dataset.addSeries(s1); 144 dataset.addSeries(s2); 145 146 return dataset; 147 148 } 149 150 155 public XYDataset createDataset2() { 156 157 TimePeriodValues s1 = new TimePeriodValues("WebCOINS"); 158 Day today = new Day(); 159 for (int i = 0; i < 24; i++) { 160 Minute m0 = new Minute(0, new Hour(i, today)); 161 Minute m1 = new Minute(30, new Hour(i, today)); 162 Minute m2 = new Minute(0, new Hour(i + 1, today)); 163 s1.add(new SimpleTimePeriod(m0.getStart(), m1.getStart()), Math.random() * 2.0); 164 s1.add(new SimpleTimePeriod(m1.getStart(), m2.getStart()), Math.random() * 2.0); 165 } 166 167 TimePeriodValuesCollection dataset = new TimePeriodValuesCollection(); 168 dataset.addSeries(s1); 169 170 return dataset; 171 172 } 173 174 179 public static void main(String [] args) { 180 181 TimePeriodValuesDemo demo = new TimePeriodValuesDemo("Time Period Values Demo 1"); 182 demo.pack(); 183 RefineryUtilities.centerFrameOnScreen(demo); 184 demo.setVisible(true); 185 186 } 187 188 } 189 | Popular Tags |