1 42 43 package org.jfree.chart.demo; 44 45 import java.awt.Font ; 46 47 import org.jfree.chart.ChartPanel; 48 import org.jfree.chart.JFreeChart; 49 import org.jfree.chart.annotations.XYTextAnnotation; 50 import org.jfree.chart.axis.DateAxis; 51 import org.jfree.chart.axis.DateTickMarkPosition; 52 import org.jfree.chart.axis.NumberAxis; 53 import org.jfree.chart.axis.ValueAxis; 54 import org.jfree.chart.labels.TimeSeriesToolTipGenerator; 55 import org.jfree.chart.plot.XYPlot; 56 import org.jfree.chart.renderer.StandardXYItemRenderer; 57 import org.jfree.chart.renderer.XYBarRenderer; 58 import org.jfree.chart.renderer.XYItemRenderer; 59 import org.jfree.data.IntervalXYDataset; 60 import org.jfree.data.XYDataset; 61 import org.jfree.data.time.Day; 62 import org.jfree.data.time.TimeSeries; 63 import org.jfree.data.time.TimeSeriesCollection; 64 import org.jfree.date.SerialDate; 65 import org.jfree.ui.ApplicationFrame; 66 import org.jfree.ui.RefineryUtilities; 67 68 73 public class OverlaidXYPlotDemo extends ApplicationFrame { 74 75 80 public OverlaidXYPlotDemo(String title) { 81 82 super(title); 83 JFreeChart chart = createOverlaidChart(); 84 ChartPanel panel = new ChartPanel(chart, true, true, true, true, true); 85 panel.setPreferredSize(new java.awt.Dimension (500, 270)); 86 setContentPane(panel); 87 88 } 89 90 95 private JFreeChart createOverlaidChart() { 96 97 IntervalXYDataset data1 = createDataset1(); 99 XYItemRenderer renderer1 = new XYBarRenderer(0.20); 100 renderer1.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00")); 101 DateAxis domainAxis = new DateAxis("Date"); 102 domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE); 103 ValueAxis rangeAxis = new NumberAxis("Value"); 104 XYPlot plot = new XYPlot(data1, domainAxis, rangeAxis, renderer1); 105 double x = new Day(9, SerialDate.MARCH, 2002).getMiddleMillisecond(); 106 XYTextAnnotation annotation = new XYTextAnnotation("Hello!", x, 10000.0); 107 annotation.setFont(new Font ("SansSerif", Font.PLAIN, 9)); 108 plot.addAnnotation(annotation); 109 110 XYDataset data2 = createDataset2(); 112 XYItemRenderer renderer2 = new StandardXYItemRenderer(); 113 renderer2.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00")); 114 plot.setSecondaryDataset(0, data2); 115 plot.setSecondaryRenderer(0, renderer2); 116 117 return new JFreeChart("Overlaid Plot Example", JFreeChart.DEFAULT_TITLE_FONT, plot, true); 119 120 } 121 122 127 private IntervalXYDataset createDataset1() { 128 129 TimeSeries series1 = new TimeSeries("Series 1", Day.class); 131 series1.add(new Day(1, SerialDate.MARCH, 2002), 12353.3); 132 series1.add(new Day(2, SerialDate.MARCH, 2002), 13734.4); 133 series1.add(new Day(3, SerialDate.MARCH, 2002), 14525.3); 134 series1.add(new Day(4, SerialDate.MARCH, 2002), 13984.3); 135 series1.add(new Day(5, SerialDate.MARCH, 2002), 12999.4); 136 series1.add(new Day(6, SerialDate.MARCH, 2002), 14274.3); 137 series1.add(new Day(7, SerialDate.MARCH, 2002), 15943.5); 138 series1.add(new Day(8, SerialDate.MARCH, 2002), 14845.3); 139 series1.add(new Day(9, SerialDate.MARCH, 2002), 14645.4); 140 series1.add(new Day(10, SerialDate.MARCH, 2002), 16234.6); 141 series1.add(new Day(11, SerialDate.MARCH, 2002), 17232.3); 142 series1.add(new Day(12, SerialDate.MARCH, 2002), 14232.2); 143 series1.add(new Day(13, SerialDate.MARCH, 2002), 13102.2); 144 series1.add(new Day(14, SerialDate.MARCH, 2002), 14230.2); 145 series1.add(new Day(15, SerialDate.MARCH, 2002), 11235.2); 146 147 return new TimeSeriesCollection(series1); 148 149 } 150 151 162 167 private XYDataset createDataset2() { 168 169 TimeSeries series2 = new TimeSeries("Series 2", Day.class); 171 172 series2.add(new Day(3, SerialDate.MARCH, 2002), 16853.2); 173 series2.add(new Day(4, SerialDate.MARCH, 2002), 19642.3); 174 series2.add(new Day(5, SerialDate.MARCH, 2002), 18253.5); 175 series2.add(new Day(6, SerialDate.MARCH, 2002), 15352.3); 176 series2.add(new Day(7, SerialDate.MARCH, 2002), 13532.0); 177 series2.add(new Day(8, SerialDate.MARCH, 2002), 12635.3); 178 series2.add(new Day(9, SerialDate.MARCH, 2002), 13998.2); 179 series2.add(new Day(10, SerialDate.MARCH, 2002), 11943.2); 180 series2.add(new Day(11, SerialDate.MARCH, 2002), 16943.9); 181 series2.add(new Day(12, SerialDate.MARCH, 2002), 17843.2); 182 series2.add(new Day(13, SerialDate.MARCH, 2002), 16495.3); 183 series2.add(new Day(14, SerialDate.MARCH, 2002), 17943.6); 184 series2.add(new Day(15, SerialDate.MARCH, 2002), 18500.7); 185 series2.add(new Day(16, SerialDate.MARCH, 2002), 19595.9); 186 187 TimeSeriesCollection tsc = new TimeSeriesCollection(series2); 188 return tsc; 189 190 } 191 192 197 public static void main(String [] args) { 198 199 OverlaidXYPlotDemo demo = new OverlaidXYPlotDemo("Overlaid XYPlot Demo"); 200 demo.pack(); 201 RefineryUtilities.centerFrameOnScreen(demo); 202 demo.setVisible(true); 203 204 } 205 206 } 207 | Popular Tags |