1 38 39 package org.jfree.chart.demo; 40 41 import java.text.SimpleDateFormat ; 42 import java.util.Calendar ; 43 44 import org.jfree.chart.ChartFactory; 45 import org.jfree.chart.ChartPanel; 46 import org.jfree.chart.JFreeChart; 47 import org.jfree.chart.axis.DateAxis; 48 import org.jfree.chart.axis.DateTickUnit; 49 import org.jfree.chart.axis.NumberAxis; 50 import org.jfree.chart.axis.SegmentedTimeline; 51 import org.jfree.chart.axis.TickUnits; 52 import org.jfree.data.HighLowDataset; 53 import org.jfree.ui.ApplicationFrame; 54 import org.jfree.ui.RefineryUtilities; 55 56 61 public class SegmentedHighLowChartDemo extends ApplicationFrame { 62 63 71 public SegmentedHighLowChartDemo(String title, 72 boolean useSegmentedAxis, 73 int timelineType) { 74 75 super(title); 76 77 System.out.println("\nMaking SegmentedHighLowChartDemo(" + title + ")"); 78 79 Calendar cal = Calendar.getInstance(); 81 cal.set(Calendar.HOUR_OF_DAY, 0); 82 cal.set(Calendar.MINUTE, 0); 83 cal.set(Calendar.SECOND, 0); 84 cal.set(Calendar.MILLISECOND, 0); 85 86 SegmentedTimeline timeline = null; 88 switch (timelineType) { 89 case 1: 90 timeline = SegmentedTimeline.newMondayThroughFridayTimeline(); 91 break; 92 93 case 2: 94 timeline = SegmentedTimeline.newFifteenMinuteTimeline(); 95 96 Calendar cal2 = (Calendar ) cal.clone(); 97 cal2.add(Calendar.YEAR, 1); 98 99 timeline.addBaseTimelineExclusions(cal.getTime().getTime(), 102 cal2.getTime().getTime()); 103 break; 104 105 default: 106 System.out.println("Invalid timelineType."); 107 System.exit(1); 108 } 109 110 111 HighLowDataset dataset = 113 DemoDatasetFactory.createSegmentedHighLowDataset(timeline, cal.getTime()); 114 115 JFreeChart chart; 116 if (useSegmentedAxis) { 117 chart = ChartFactory.createHighLowChart( 118 title, 119 "Time", "Value", 120 dataset, timeline, true); 121 } else { 122 chart = ChartFactory.createHighLowChart( 123 title, 124 "Time", "Value", 125 dataset, true); 126 } 127 128 DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis(); 129 axis.setAutoRange(true); 130 TickUnits units = new TickUnits(); 131 units.add(new DateTickUnit(DateTickUnit.DAY, 1, DateTickUnit.HOUR, 1, 132 new SimpleDateFormat ("d-MMM"))); 133 units.add(new DateTickUnit(DateTickUnit.DAY, 2, DateTickUnit.HOUR, 1, 134 new SimpleDateFormat ("d-MMM"))); 135 units.add(new DateTickUnit(DateTickUnit.DAY, 7, DateTickUnit.DAY, 1, 136 new SimpleDateFormat ("d-MMM"))); 137 units.add(new DateTickUnit(DateTickUnit.DAY, 15, DateTickUnit.DAY, 1, 138 new SimpleDateFormat ("d-MMM"))); 139 units.add(new DateTickUnit(DateTickUnit.DAY, 30, DateTickUnit.DAY, 1, 140 new SimpleDateFormat ("d-MMM"))); 141 axis.setStandardTickUnits(units); 142 143 NumberAxis vaxis = (NumberAxis) chart.getXYPlot().getRangeAxis(); 144 vaxis.setAutoRangeIncludesZero(false); 145 146 ChartPanel chartPanel = new ChartPanel(chart); 147 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 148 149 setContentPane(chartPanel); 150 151 } 152 153 164 169 public static void main(String [] args) { 170 171 ApplicationFrame[][] frame = new ApplicationFrame[2][2]; 172 frame[0][0] = new SegmentedHighLowChartDemo("Segmented Daily High-Low-Open-Close Demo", 173 true, 1); 174 frame[1][0] = new SegmentedHighLowChartDemo("Normal Daily High-Low-Open-Close Demo", 175 false, 1); 176 frame[0][1] = new SegmentedHighLowChartDemo("Segmented Intraday High-Low-Open-Close Demo", 177 true, 2); 178 frame[1][1] = new SegmentedHighLowChartDemo("Normal Intraday High-Low-Open-Close Demo", 179 false, 2); 180 for (int i = 0; i < 2; i++) { 181 for (int j = 0; j < 2; j++) { 182 frame[i][j].pack(); 183 RefineryUtilities.positionFrameOnScreen(frame[i][j], .15 + .70 * j, .25 + .50 * i); 184 frame[i][j].setVisible(true); 185 } 186 } 187 188 } 189 190 } 191 | Popular Tags |