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 MultipleAxisDemo2 extends ApplicationFrame { 65 66 71 public MultipleAxisDemo2(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 116 NumberAxis yAxis2 = new NumberAxis("Range Axis 2"); 118 plot.setSecondaryRangeAxis(0, yAxis2); 119 plot.setSecondaryRangeAxisLocation(0, AxisLocation.BOTTOM_OR_RIGHT); 120 121 XYDataset dataset2 = createDataset("Series 2", 1000.0, new Minute(), 170); 122 plot.setSecondaryDataset(0, dataset2); 123 plot.mapSecondaryDatasetToDomainAxis(0, new Integer (0)); 124 plot.mapSecondaryDatasetToRangeAxis(0, new Integer (0)); 125 126 return chart; 127 128 } 129 130 141 151 private XYDataset createDataset(String name, double base, RegularTimePeriod start, int count) { 152 153 TimeSeries series = new TimeSeries(name, start.getClass()); 154 RegularTimePeriod period = start; 155 double value = base; 156 for (int i = 0; i < count; i++) { 157 series.add(period, value); 158 period = period.next(); 159 value = value * (1 + (Math.random() - 0.495) / 10.0); 160 } 161 162 TimeSeriesCollection dataset = new TimeSeriesCollection(); 163 dataset.addSeries(series); 164 165 return dataset; 166 167 } 168 169 174 public static void main(String [] args) { 175 176 MultipleAxisDemo2 demo = new MultipleAxisDemo2("Multiple Axis Demo 2"); 177 demo.pack(); 178 RefineryUtilities.centerFrameOnScreen(demo); 179 demo.setVisible(true); 180 181 } 182 183 } 184 | Popular Tags |