1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.Color ; 41 42 import org.jfree.chart.ChartFactory; 43 import org.jfree.chart.ChartPanel; 44 import org.jfree.chart.JFreeChart; 45 import org.jfree.chart.Spacer; 46 import org.jfree.chart.TextTitle; 47 import org.jfree.chart.axis.AxisLocation; 48 import org.jfree.chart.axis.NumberAxis; 49 import org.jfree.chart.plot.PlotOrientation; 50 import org.jfree.chart.plot.XYPlot; 51 import org.jfree.chart.renderer.StandardXYItemRenderer; 52 import org.jfree.data.XYDataset; 53 import org.jfree.data.time.Minute; 54 import org.jfree.data.time.RegularTimePeriod; 55 import org.jfree.data.time.TimeSeries; 56 import org.jfree.data.time.TimeSeriesCollection; 57 import org.jfree.ui.ApplicationFrame; 58 import org.jfree.ui.RefineryUtilities; 59 60 65 public class MultipleAxisDemo1 extends ApplicationFrame { 66 67 72 public MultipleAxisDemo1(String title) { 73 74 super(title); 75 JFreeChart chart = createChart(); 76 ChartPanel chartPanel = new ChartPanel(chart); 77 chartPanel.setPreferredSize(new java.awt.Dimension (600, 270)); 78 chartPanel.setHorizontalZoom(true); 79 chartPanel.setVerticalZoom(true); 80 setContentPane(chartPanel); 81 82 } 83 84 89 private JFreeChart createChart() { 90 91 XYDataset dataset1 = createDataset("Series 1", 100.0, new Minute(), 200); 92 93 JFreeChart chart = ChartFactory.createTimeSeriesChart( 94 "Multiple Axis Demo 1", 95 "Time of Day", 96 "Primary Range Axis", 97 dataset1, 98 true, 99 true, 100 false 101 ); 102 103 chart.setBackgroundPaint(Color.white); 104 chart.addSubtitle(new TextTitle("Four datasets and four range axes.")); 105 XYPlot plot = chart.getXYPlot(); 106 plot.setOrientation(PlotOrientation.VERTICAL); 107 plot.setBackgroundPaint(Color.lightGray); 108 plot.setDomainGridlinePaint(Color.white); 109 plot.setRangeGridlinePaint(Color.white); 110 111 plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); 112 113 StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer(); 114 renderer.setPaint(Color.black); 115 116 NumberAxis axis2 = new NumberAxis("Range Axis 2"); 118 axis2.setAutoRangeIncludesZero(false); 119 axis2.setLabelPaint(Color.red); 120 axis2.setTickLabelPaint(Color.red); 121 plot.setSecondaryRangeAxis(0, axis2); 122 plot.setSecondaryRangeAxisLocation(0, AxisLocation.BOTTOM_OR_LEFT); 123 124 XYDataset dataset2 = createDataset("Series 2", 1000.0, new Minute(), 170); 125 plot.setSecondaryDataset(0, dataset2); 126 plot.mapSecondaryDatasetToRangeAxis(0, new Integer (0)); 127 plot.setSecondaryRenderer(0, new StandardXYItemRenderer()); 128 plot.getSecondaryRenderer(0).setSeriesPaint(0, Color.red); 129 130 NumberAxis axis3 = new NumberAxis("Range Axis 3"); 132 axis3.setLabelPaint(Color.blue); 133 axis3.setTickLabelPaint(Color.blue); 134 plot.setSecondaryRangeAxis(1, axis3); 135 136 XYDataset dataset3 = createDataset("Series 3", 10000.0, new Minute(), 170); 137 plot.setSecondaryDataset(1, dataset3); 138 plot.mapSecondaryDatasetToRangeAxis(1, new Integer (1)); 139 140 plot.setSecondaryRenderer(1, new StandardXYItemRenderer()); 141 plot.getSecondaryRenderer(1).setSeriesPaint(0, Color.blue); 142 143 NumberAxis axis4 = new NumberAxis("Range Axis 4"); 145 axis4.setLabelPaint(Color.green); 146 axis4.setTickLabelPaint(Color.green); 147 plot.setSecondaryRangeAxis(2, axis4); 148 149 XYDataset dataset4 = createDataset("Series 4", 25.0, new Minute(), 200); 150 plot.setSecondaryDataset(2, dataset4); 151 plot.mapSecondaryDatasetToRangeAxis(2, new Integer (2)); 152 153 plot.setSecondaryRenderer(2, new StandardXYItemRenderer()); 154 plot.getSecondaryRenderer(2).setSeriesPaint(0, Color.green); 155 156 return chart; 157 } 158 159 170 180 private XYDataset createDataset(String name, double base, RegularTimePeriod start, int count) { 181 182 TimeSeries series = new TimeSeries(name, start.getClass()); 183 RegularTimePeriod period = start; 184 double value = base; 185 for (int i = 0; i < count; i++) { 186 series.add(period, value); 187 period = period.next(); 188 value = value * (1 + (Math.random() - 0.495) / 10.0); 189 } 190 191 TimeSeriesCollection dataset = new TimeSeriesCollection(); 192 dataset.addSeries(series); 193 194 return dataset; 195 196 } 197 198 203 public static void main(String [] args) { 204 205 MultipleAxisDemo1 demo = new MultipleAxisDemo1("Multiple Axis Demo 1"); 206 demo.pack(); 207 RefineryUtilities.centerFrameOnScreen(demo); 208 demo.setVisible(true); 209 210 } 211 212 } 213 | Popular Tags |