1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.BasicStroke ; 41 import java.awt.Color ; 42 import java.awt.Font ; 43 44 import org.jfree.chart.ChartFactory; 45 import org.jfree.chart.ChartPanel; 46 import org.jfree.chart.JFreeChart; 47 import org.jfree.chart.Legend; 48 import org.jfree.chart.Marker; 49 import org.jfree.chart.MarkerLabelPosition; 50 import org.jfree.chart.annotations.XYAnnotation; 51 import org.jfree.chart.annotations.XYDrawableAnnotation; 52 import org.jfree.chart.annotations.XYPointerAnnotation; 53 import org.jfree.chart.axis.DateAxis; 54 import org.jfree.chart.axis.ValueAxis; 55 import org.jfree.chart.labels.TimeSeriesToolTipGenerator; 56 import org.jfree.chart.plot.PlotOrientation; 57 import org.jfree.chart.plot.XYPlot; 58 import org.jfree.data.XYDataset; 59 import org.jfree.data.time.Day; 60 import org.jfree.data.time.Hour; 61 import org.jfree.data.time.Minute; 62 import org.jfree.data.time.TimeSeries; 63 import org.jfree.data.time.TimeSeriesCollection; 64 import org.jfree.ui.ApplicationFrame; 65 import org.jfree.ui.RefineryUtilities; 66 import org.jfree.ui.TextAnchor; 67 68 73 public class MarkerDemo1 extends ApplicationFrame { 74 75 80 public MarkerDemo1(String title) { 81 82 super(title); 83 XYDataset data = createDataset(); 84 JFreeChart chart = createChart(data); 85 ChartPanel chartPanel = new ChartPanel(chart); 86 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 87 chartPanel.setVerticalZoom(true); 88 chartPanel.setHorizontalZoom(true); 89 setContentPane(chartPanel); 90 91 } 92 93 100 private JFreeChart createChart(XYDataset data) { 101 102 JFreeChart chart = ChartFactory.createScatterPlot( 103 "Marker Demo 1", 104 "X", 105 "Y", 106 data, 107 PlotOrientation.VERTICAL, 108 true, 109 true, 110 false 111 ); 112 chart.getLegend().setAnchor(Legend.EAST); 113 114 XYPlot plot = chart.getXYPlot(); 116 117 plot.getRenderer().setToolTipGenerator(new TimeSeriesToolTipGenerator()); 119 120 DateAxis domainAxis = new DateAxis("Time"); 122 domainAxis.setUpperMargin(0.50); 123 plot.setDomainAxis(domainAxis); 124 125 ValueAxis rangeAxis = plot.getRangeAxis(); 126 rangeAxis.setUpperMargin(0.30); 127 rangeAxis.setLowerMargin(0.50); 128 129 Marker start = new Marker(200.0, Color.green); 131 start.setLabel("Bid Start Price"); 132 start.setLabelPosition(MarkerLabelPosition.TOP_RIGHT); 133 plot.addRangeMarker(start); 134 135 Marker target = new Marker(175.0, Color.red); 137 target.setLabel("Target Price"); 138 target.setLabelPosition(MarkerLabelPosition.TOP_RIGHT); 139 plot.addRangeMarker(target); 140 141 Hour hour = new Hour(2, new Day(22, 5, 2003)); 143 double millis = hour.getFirstMillisecond(); 144 Marker originalEnd = new Marker(millis, Color.orange); 145 originalEnd.setLabel("Original Close (02:00)"); 146 plot.addDomainMarker(originalEnd); 147 148 Minute min = new Minute(15, hour); 150 millis = min.getFirstMillisecond(); 151 Marker currentEnd = new Marker(millis, Color.red); 152 currentEnd.setLabel("Close Date (02:15)"); 153 currentEnd.setLabelPosition(MarkerLabelPosition.TOP_RIGHT); 154 plot.addDomainMarker(currentEnd); 155 156 167 Hour h = new Hour(2, new Day(22, 5, 2003)); 169 Minute m = new Minute(10, h); 170 millis = m.getFirstMillisecond(); 171 CircleDrawer cd = new CircleDrawer(Color.red, new BasicStroke (1.0f), null); 172 XYAnnotation bestBid = new XYDrawableAnnotation(millis, 163.0, 11, 11, cd); 173 plot.addAnnotation(bestBid); 174 XYPointerAnnotation pointer = new XYPointerAnnotation("Best Bid", millis, 163.0, 175 3.0 * Math.PI / 4.0); 176 pointer.setBaseRadius(35.0); 177 pointer.setTipRadius(10.0); 178 pointer.setFont(new Font ("SansSerif", Font.PLAIN, 9)); 179 pointer.setPaint(Color.blue); 180 pointer.setTextAnchor(TextAnchor.HALF_ASCENT_RIGHT); 181 plot.addAnnotation(pointer); 182 183 return chart; 184 185 } 186 187 192 private XYDataset createDataset() { 193 194 TimeSeriesCollection result = new TimeSeriesCollection(); 195 result.addSeries(createSupplier1Bids()); 196 result.addSeries(createSupplier2Bids()); 197 return result; 198 199 } 200 201 206 private TimeSeries createSupplier1Bids() { 207 208 Hour hour = new Hour(1, new Day(22, 5, 2003)); 209 210 TimeSeries series1 = new TimeSeries("Supplier 1", Minute.class); 211 series1.add(new Minute(13, hour), 200.0); 212 series1.add(new Minute(14, hour), 195.0); 213 series1.add(new Minute(45, hour), 190.0); 214 series1.add(new Minute(46, hour), 188.0); 215 series1.add(new Minute(47, hour), 185.0); 216 series1.add(new Minute(52, hour), 180.0); 217 218 return series1; 219 220 } 221 222 227 private TimeSeries createSupplier2Bids() { 228 229 Hour hour1 = new Hour(1, new Day(22, 5, 2003)); 230 Hour hour2 = (Hour) hour1.next(); 231 232 TimeSeries series2 = new TimeSeries("Supplier 2", Minute.class); 233 series2.add(new Minute(25, hour1), 185.0); 234 series2.add(new Minute(0, hour2), 175.0); 235 series2.add(new Minute(5, hour2), 170.0); 236 series2.add(new Minute(6, hour2), 168.0); 237 series2.add(new Minute(9, hour2), 165.0); 238 series2.add(new Minute(10, hour2), 163.0); 239 240 return series2; 241 242 } 243 244 249 public static void main(String [] args) { 250 251 MarkerDemo1 demo = new MarkerDemo1("Marker Demo 1"); 252 demo.pack(); 253 RefineryUtilities.centerFrameOnScreen(demo); 254 demo.setVisible(true); 255 256 } 257 258 } 259 | Popular Tags |