1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.Color ; 41 import java.text.SimpleDateFormat ; 42 43 import org.jfree.chart.ChartFactory; 44 import org.jfree.chart.ChartPanel; 45 import org.jfree.chart.JFreeChart; 46 import org.jfree.chart.StandardLegend; 47 import org.jfree.chart.axis.DateAxis; 48 import org.jfree.chart.axis.NumberAxis; 49 import org.jfree.chart.plot.XYPlot; 50 import org.jfree.chart.renderer.StandardXYItemRenderer; 51 import org.jfree.chart.renderer.XYItemRenderer; 52 import org.jfree.data.XYDataset; 53 import org.jfree.data.time.Month; 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 65 public class DualAxisDemo2 extends ApplicationFrame { 66 67 72 public DualAxisDemo2(String title) { 73 74 super(title); 75 76 String chartTitle = "Dual Axis Demo 2"; 78 XYDataset dataset = createDataset1(); 79 80 JFreeChart chart = ChartFactory.createTimeSeriesChart( 81 chartTitle, 82 "Date", 83 "Price Per Unit", 84 dataset, 85 true, 86 true, 87 false 88 ); 89 90 StandardLegend legend = (StandardLegend) chart.getLegend(); 91 legend.setDisplaySeriesShapes(true); 92 93 XYPlot plot = chart.getXYPlot(); 94 NumberAxis axis2 = new NumberAxis("Secondary"); 95 axis2.setAutoRangeIncludesZero(false); 96 plot.setSecondaryRangeAxis(0, axis2); 97 plot.setSecondaryDataset(0, createDataset2()); 98 plot.mapSecondaryDatasetToRangeAxis(0, new Integer (0)); 99 XYItemRenderer renderer = plot.getRenderer(); 100 if (renderer instanceof StandardXYItemRenderer) { 101 StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer; 102 rr.setPlotShapes(true); 103 rr.setShapesFilled(true); 104 } 105 106 StandardXYItemRenderer renderer2 = new StandardXYItemRenderer(); 107 renderer2.setSeriesPaint(0, Color.black); 108 renderer2.setPlotShapes(true); 109 plot.setSecondaryRenderer(0, renderer2); 110 111 DateAxis axis = (DateAxis) plot.getDomainAxis(); 112 axis.setDateFormatOverride(new SimpleDateFormat ("MMM-yyyy")); 113 114 ChartPanel chartPanel = new ChartPanel(chart); 115 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 116 setContentPane(chartPanel); 117 118 } 119 120 125 private XYDataset createDataset1() { 126 127 TimeSeries s1 = new TimeSeries("Random Data 1", Month.class); 128 s1.add(new Month(2, 2001), 181.8); 129 s1.add(new Month(3, 2001), 167.3); 130 s1.add(new Month(4, 2001), 153.8); 131 s1.add(new Month(5, 2001), 167.6); 132 s1.add(new Month(6, 2001), 158.8); 133 s1.add(new Month(7, 2001), 148.3); 134 s1.add(new Month(8, 2001), 153.9); 135 s1.add(new Month(9, 2001), 142.7); 136 s1.add(new Month(10, 2001), 123.2); 137 s1.add(new Month(11, 2001), 131.8); 138 s1.add(new Month(12, 2001), 139.6); 139 s1.add(new Month(1, 2002), 142.9); 140 s1.add(new Month(2, 2002), 138.7); 141 s1.add(new Month(3, 2002), 137.3); 142 s1.add(new Month(4, 2002), 143.9); 143 s1.add(new Month(5, 2002), 139.8); 144 s1.add(new Month(6, 2002), 137.0); 145 s1.add(new Month(7, 2002), 132.8); 146 147 TimeSeriesCollection dataset = new TimeSeriesCollection(); 148 dataset.addSeries(s1); 149 150 return dataset; 151 152 } 153 154 159 private XYDataset createDataset2() { 160 161 162 TimeSeries s2 = new TimeSeries("Random Data 2", Month.class); 163 s2.add(new Month(2, 2001), 429.6); 164 s2.add(new Month(3, 2001), 323.2); 165 s2.add(new Month(4, 2001), 417.2); 166 s2.add(new Month(5, 2001), 624.1); 167 s2.add(new Month(6, 2001), 422.6); 168 s2.add(new Month(7, 2001), 619.2); 169 s2.add(new Month(8, 2001), 416.5); 170 s2.add(new Month(9, 2001), 512.7); 171 s2.add(new Month(10, 2001), 501.5); 172 s2.add(new Month(11, 2001), 306.1); 173 s2.add(new Month(12, 2001), 410.3); 174 s2.add(new Month(1, 2002), 511.7); 175 s2.add(new Month(2, 2002), 611.0); 176 s2.add(new Month(3, 2002), 709.6); 177 s2.add(new Month(4, 2002), 613.2); 178 s2.add(new Month(5, 2002), 711.6); 179 s2.add(new Month(6, 2002), 708.8); 180 s2.add(new Month(7, 2002), 501.6); 181 182 TimeSeriesCollection dataset = new TimeSeriesCollection(); 183 dataset.addSeries(s2); 184 185 return dataset; 186 187 } 188 189 200 205 public static void main(String [] args) { 206 207 DualAxisDemo2 demo = new DualAxisDemo2("Dual Axis Demo 2"); 208 demo.pack(); 209 RefineryUtilities.centerFrameOnScreen(demo); 210 demo.setVisible(true); 211 212 } 213 214 } 215 | Popular Tags |