1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.Font ; 41 42 import org.jfree.chart.ChartPanel; 43 import org.jfree.chart.JFreeChart; 44 import org.jfree.chart.annotations.XYTextAnnotation; 45 import org.jfree.chart.axis.DateAxis; 46 import org.jfree.chart.axis.DateTickMarkPosition; 47 import org.jfree.chart.axis.NumberAxis; 48 import org.jfree.chart.axis.ValueAxis; 49 import org.jfree.chart.labels.TimeSeriesToolTipGenerator; 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.chart.renderer.XYBarRenderer; 54 import org.jfree.chart.renderer.XYItemRenderer; 55 import org.jfree.data.IntervalXYDataset; 56 import org.jfree.data.XYDataset; 57 import org.jfree.data.time.Day; 58 import org.jfree.data.time.TimePeriodAnchor; 59 import org.jfree.data.time.TimeSeries; 60 import org.jfree.data.time.TimeSeriesCollection; 61 import org.jfree.date.SerialDate; 62 import org.jfree.ui.ApplicationFrame; 63 import org.jfree.ui.RefineryUtilities; 64 65 70 public class OverlaidXYPlotDemo2 extends ApplicationFrame { 71 72 77 public OverlaidXYPlotDemo2(String title) { 78 79 super(title); 80 JFreeChart chart = createOverlaidChart(); 81 ChartPanel panel = new ChartPanel(chart, true, true, true, true, true); 82 panel.setPreferredSize(new java.awt.Dimension (500, 270)); 83 setContentPane(panel); 84 85 } 86 87 98 103 private JFreeChart createOverlaidChart() { 104 105 DateAxis domainAxis = new DateAxis("Date"); 106 domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE); 107 ValueAxis rangeAxis = new NumberAxis("Value"); 108 109 IntervalXYDataset data1 = createDataset1(); 111 XYItemRenderer renderer1 = new XYBarRenderer(0.20); 112 renderer1.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00")); 113 XYPlot plot = new XYPlot(data1, domainAxis, rangeAxis, renderer1); 114 double x = new Day(9, SerialDate.MARCH, 2002).getMiddleMillisecond(); 115 XYTextAnnotation annotation = new XYTextAnnotation("Hello!", x, 10000.0); 116 annotation.setFont(new Font ("SansSerif", Font.PLAIN, 9)); 117 plot.addAnnotation(annotation); 118 119 ValueAxis rangeAxis2 = new NumberAxis("Value 2"); 120 plot.setSecondaryRangeAxis(0, rangeAxis2); 121 122 XYDataset data2A = createDataset2A(); 124 XYItemRenderer renderer2A = new StandardXYItemRenderer(); 125 renderer2A.setToolTipGenerator(new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00")); 126 plot.setSecondaryDataset(0, data2A); 127 plot.setSecondaryRenderer(0, renderer2A); 128 129 XYDataset data2B = createDataset2B(); 130 plot.setSecondaryDataset(1, data2B); 131 plot.setSecondaryRenderer(1, new StandardXYItemRenderer()); 132 plot.mapSecondaryDatasetToRangeAxis(1, new Integer (0)); 133 134 plot.setOrientation(PlotOrientation.VERTICAL); 135 136 return new JFreeChart("Overlaid Plot Example", JFreeChart.DEFAULT_TITLE_FONT, plot, true); 138 139 } 140 141 146 private IntervalXYDataset createDataset1() { 147 148 TimeSeries series1 = new TimeSeries("Series 1", Day.class); 150 series1.add(new Day(1, SerialDate.MARCH, 2002), 12353.3); 151 series1.add(new Day(2, SerialDate.MARCH, 2002), 13734.4); 152 series1.add(new Day(3, SerialDate.MARCH, 2002), 14525.3); 153 series1.add(new Day(4, SerialDate.MARCH, 2002), 13984.3); 154 series1.add(new Day(5, SerialDate.MARCH, 2002), 12999.4); 155 series1.add(new Day(6, SerialDate.MARCH, 2002), 14274.3); 156 series1.add(new Day(7, SerialDate.MARCH, 2002), 15943.5); 157 series1.add(new Day(8, SerialDate.MARCH, 2002), 14845.3); 158 series1.add(new Day(9, SerialDate.MARCH, 2002), 14645.4); 159 series1.add(new Day(10, SerialDate.MARCH, 2002), 16234.6); 160 series1.add(new Day(11, SerialDate.MARCH, 2002), 17232.3); 161 series1.add(new Day(12, SerialDate.MARCH, 2002), 14232.2); 162 series1.add(new Day(13, SerialDate.MARCH, 2002), 13102.2); 163 series1.add(new Day(14, SerialDate.MARCH, 2002), 14230.2); 164 series1.add(new Day(15, SerialDate.MARCH, 2002), 11235.2); 165 166 TimeSeriesCollection result = new TimeSeriesCollection(series1); 167 return result; 168 169 } 170 171 176 private XYDataset createDataset2A() { 177 178 TimeSeries series2 = new TimeSeries("Series 2", Day.class); 180 181 series2.add(new Day(3, SerialDate.MARCH, 2002), 16853.2); 182 series2.add(new Day(4, SerialDate.MARCH, 2002), 19642.3); 183 series2.add(new Day(5, SerialDate.MARCH, 2002), 18253.5); 184 series2.add(new Day(6, SerialDate.MARCH, 2002), 15352.3); 185 series2.add(new Day(7, SerialDate.MARCH, 2002), 13532.0); 186 series2.add(new Day(8, SerialDate.MARCH, 2002), 12635.3); 187 series2.add(new Day(9, SerialDate.MARCH, 2002), 13998.2); 188 series2.add(new Day(10, SerialDate.MARCH, 2002), 11943.2); 189 series2.add(new Day(11, SerialDate.MARCH, 2002), 16943.9); 190 series2.add(new Day(12, SerialDate.MARCH, 2002), 17843.2); 191 series2.add(new Day(13, SerialDate.MARCH, 2002), 16495.3); 192 series2.add(new Day(14, SerialDate.MARCH, 2002), 17943.6); 193 series2.add(new Day(15, SerialDate.MARCH, 2002), 18500.7); 194 series2.add(new Day(16, SerialDate.MARCH, 2002), 19595.9); 195 196 TimeSeriesCollection result = new TimeSeriesCollection(series2); 197 result.setXPosition(TimePeriodAnchor.MIDDLE); 198 return result; 199 200 } 201 202 207 private XYDataset createDataset2B() { 208 209 TimeSeries series2 = new TimeSeries("Series 2B", Day.class); 211 212 series2.add(new Day(3, SerialDate.MARCH, 2002), 43.9); 213 series2.add(new Day(4, SerialDate.MARCH, 2002), 72.6); 214 series2.add(new Day(5, SerialDate.MARCH, 2002), 89.4); 215 series2.add(new Day(6, SerialDate.MARCH, 2002), 23.8); 216 series2.add(new Day(7, SerialDate.MARCH, 2002), 45.0); 217 series2.add(new Day(8, SerialDate.MARCH, 2002), 65.8); 218 series2.add(new Day(9, SerialDate.MARCH, 2002), 92.1); 219 series2.add(new Day(10, SerialDate.MARCH, 2002), 84.7); 220 series2.add(new Day(11, SerialDate.MARCH, 2002), 77.2); 221 series2.add(new Day(12, SerialDate.MARCH, 2002), 65.1); 222 series2.add(new Day(13, SerialDate.MARCH, 2002), 78.5); 223 series2.add(new Day(14, SerialDate.MARCH, 2002), 75.3); 224 series2.add(new Day(15, SerialDate.MARCH, 2002), 69.9); 225 series2.add(new Day(20, SerialDate.MARCH, 2002), 56.6); 226 227 TimeSeriesCollection result = new TimeSeriesCollection(series2); 228 result.setXPosition(TimePeriodAnchor.MIDDLE); 229 return result; 230 231 } 232 233 238 public static void main(String [] args) { 239 240 OverlaidXYPlotDemo2 demo = new OverlaidXYPlotDemo2("Overlaid XYPlot Demo"); 241 demo.pack(); 242 RefineryUtilities.centerFrameOnScreen(demo); 243 demo.setVisible(true); 244 245 } 246 247 } 248 | Popular Tags |