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.axis.AxisLocation; 47 import org.jfree.chart.axis.NumberAxis; 48 import org.jfree.chart.plot.PlotOrientation; 49 import org.jfree.chart.plot.XYPlot; 50 import org.jfree.chart.renderer.StandardXYItemRenderer; 51 import org.jfree.data.XYDataset; 52 import org.jfree.data.time.Minute; 53 import org.jfree.data.time.RegularTimePeriod; 54 import org.jfree.data.time.TimeSeries; 55 import org.jfree.data.time.TimeSeriesCollection; 56 import org.jfree.ui.ApplicationFrame; 57 import org.jfree.ui.RefineryUtilities; 58 59 64 public class MultipleAxisDemo3 extends ApplicationFrame { 65 66 71 public MultipleAxisDemo3(String title) { 72 73 super(title); 74 JFreeChart chart = createChart(); 75 ChartPanel chartPanel = new ChartPanel(chart); 76 chartPanel.setPreferredSize(new java.awt.Dimension (600, 270)); 77 setContentPane(chartPanel); 78 79 } 80 81 86 private JFreeChart createChart() { 87 88 XYDataset dataset1 = createDataset("Series 1", 100.0, new Minute(), 200); 89 90 JFreeChart chart = ChartFactory.createTimeSeriesChart( 91 "Multiple Axis Demo 2", 92 "Time of Day", 93 "Primary Range Axis", 94 dataset1, 95 true, 96 true, 97 false 98 ); 99 100 chart.setBackgroundPaint(Color.white); 101 XYPlot plot = chart.getXYPlot(); 102 plot.setOrientation(PlotOrientation.VERTICAL); 103 plot.setBackgroundPaint(Color.lightGray); 104 plot.setDomainGridlinePaint(Color.white); 105 plot.setRangeGridlinePaint(Color.white); 106 plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); 107 108 StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer(); 109 renderer.setPaint(Color.black); 110 111 NumberAxis xAxis2 = new NumberAxis("Domain Axis 2"); 113 xAxis2.setAutoRangeIncludesZero(false); 114 plot.setSecondaryDomainAxis(0, xAxis2); 115 plot.setSecondaryDomainAxisLocation(0, AxisLocation.BOTTOM_OR_LEFT); 116 117 NumberAxis xAxis3 = new NumberAxis("Domain Axis 3"); 119 xAxis2.setAutoRangeIncludesZero(false); 120 plot.setSecondaryDomainAxis(1, xAxis3); 121 plot.setSecondaryDomainAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT); 122 123 NumberAxis yAxis2 = new NumberAxis("Range Axis 2"); 125 plot.setSecondaryRangeAxis(0, yAxis2); 126 plot.setSecondaryRangeAxisLocation(0, AxisLocation.BOTTOM_OR_RIGHT); 127 128 XYDataset dataset2 = createDataset("Series 2", 1000.0, new Minute(), 170); 129 plot.setSecondaryDataset(0, dataset2); 130 plot.mapSecondaryDatasetToDomainAxis(0, new Integer (0)); 131 plot.mapSecondaryDatasetToRangeAxis(0, new Integer (0)); 132 133 return chart; 134 135 } 136 137 148 158 private XYDataset createDataset(String name, double base, RegularTimePeriod start, int count) { 159 160 TimeSeries series = new TimeSeries(name, start.getClass()); 161 RegularTimePeriod period = start; 162 double value = base; 163 for (int i = 0; i < count; i++) { 164 series.add(period, value); 165 period = period.next(); 166 value = value * (1 + (Math.random() - 0.495) / 10.0); 167 } 168 169 TimeSeriesCollection dataset = new TimeSeriesCollection(); 170 dataset.addSeries(series); 171 172 return dataset; 173 174 } 175 176 181 public static void main(String [] args) { 182 183 MultipleAxisDemo3 demo = new MultipleAxisDemo3("Multiple Axis Demo 3"); 184 demo.pack(); 185 RefineryUtilities.centerFrameOnScreen(demo); 186 demo.setVisible(true); 187 188 } 189 190 } 191 | Popular Tags |