1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.Font ; 41 42 import org.jfree.chart.ChartPanel; 43 import org.jfree.chart.JFreeChart; 44 import org.jfree.chart.annotations.XYTextAnnotation; 45 import org.jfree.chart.axis.AxisLocation; 46 import org.jfree.chart.axis.NumberAxis; 47 import org.jfree.chart.plot.CombinedDomainXYPlot; 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.chart.renderer.XYItemRenderer; 52 import org.jfree.data.XYDataset; 53 import org.jfree.data.XYSeries; 54 import org.jfree.data.XYSeriesCollection; 55 import org.jfree.ui.ApplicationFrame; 56 import org.jfree.ui.RefineryUtilities; 57 58 63 public class CombinedXYPlotDemo4 extends ApplicationFrame { 64 65 70 public CombinedXYPlotDemo4(String title) { 71 72 super(title); 73 JFreeChart chart = createCombinedChart(); 74 ChartPanel panel = new ChartPanel(chart, true, true, true, false, true); 75 panel.setPreferredSize(new java.awt.Dimension (500, 270)); 76 setContentPane(panel); 77 78 } 79 80 85 private JFreeChart createCombinedChart() { 86 87 XYDataset data1 = createDataset1(); 89 XYItemRenderer renderer1 = new StandardXYItemRenderer(); 90 NumberAxis rangeAxis1 = new NumberAxis("Range 1"); 91 XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1); 92 subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); 93 94 subplot1.setSecondaryDataset(0, createDataset2()); 96 NumberAxis axis2 = new NumberAxis("Range Axis 2"); 97 axis2.setAutoRangeIncludesZero(false); 98 subplot1.setSecondaryRangeAxis(0, axis2); 99 subplot1.setSecondaryRangeAxisLocation(0, AxisLocation.BOTTOM_OR_RIGHT); 100 subplot1.setSecondaryRenderer(0, new StandardXYItemRenderer()); 101 subplot1.mapSecondaryDatasetToRangeAxis(0, new Integer (0)); 102 103 XYTextAnnotation annotation = new XYTextAnnotation("Hello!", 50.0, 10000.0); 104 annotation.setFont(new Font ("SansSerif", Font.PLAIN, 9)); 105 annotation.setRotationAngle(Math.PI / 4.0); 106 subplot1.addAnnotation(annotation); 107 108 XYDataset data2 = createDataset2(); 110 XYItemRenderer renderer2 = new StandardXYItemRenderer(); 111 NumberAxis rangeAxis2 = new NumberAxis("Range 2"); 112 rangeAxis2.setAutoRangeIncludesZero(false); 113 XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2); 114 subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT); 115 116 CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new NumberAxis("Domain")); 118 plot.setGap(10.0); 119 120 plot.add(subplot1, 1); 122 plot.add(subplot2, 1); 123 plot.setOrientation(PlotOrientation.VERTICAL); 124 125 return new JFreeChart("CombinedDomainXYPlot Demo", 127 JFreeChart.DEFAULT_TITLE_FONT, plot, true); 128 129 } 130 131 136 private XYDataset createDataset1() { 137 138 XYSeries series1 = new XYSeries("Series 1a"); 140 series1.add(10.0, 12353.3); 141 series1.add(20.0, 13734.4); 142 series1.add(30.0, 14525.3); 143 series1.add(40.0, 13984.3); 144 series1.add(50.0, 12999.4); 145 series1.add(60.0, 14274.3); 146 series1.add(70.0, 15943.5); 147 series1.add(80.0, 14845.3); 148 series1.add(90.0, 14645.4); 149 series1.add(100.0, 16234.6); 150 series1.add(110.0, 17232.3); 151 series1.add(120.0, 14232.2); 152 series1.add(130.0, 13102.2); 153 series1.add(140.0, 14230.2); 154 series1.add(150.0, 11235.2); 155 156 XYSeries series1b = new XYSeries("Series 1b"); 157 series1b.add(10.0, 15000.3); 158 series1b.add(20.0, 11000.4); 159 series1b.add(30.0, 17000.3); 160 series1b.add(40.0, 15000.3); 161 series1b.add(50.0, 14000.4); 162 series1b.add(60.0, 12000.3); 163 series1b.add(70.0, 11000.5); 164 series1b.add(80.0, 12000.3); 165 series1b.add(90.0, 13000.4); 166 series1b.add(100.0, 12000.6); 167 series1b.add(110.0, 13000.3); 168 series1b.add(120.0, 17000.2); 169 series1b.add(130.0, 18000.2); 170 series1b.add(140.0, 16000.2); 171 series1b.add(150.0, 17000.2); 172 173 XYSeriesCollection collection = new XYSeriesCollection(); 174 collection.addSeries(series1); 175 collection.addSeries(series1b); 176 return collection; 177 178 } 179 180 185 private XYDataset createDataset2() { 186 187 XYSeries series2 = new XYSeries("Series 2"); 189 190 series2.add(10.0, 16853.2); 191 series2.add(20.0, 19642.3); 192 series2.add(30.0, 18253.5); 193 series2.add(40.0, 15352.3); 194 series2.add(50.0, 13532.0); 195 series2.add(100.0, 12635.3); 196 series2.add(110.0, 13998.2); 197 series2.add(120.0, 11943.2); 198 series2.add(130.0, 16943.9); 199 series2.add(140.0, 17843.2); 200 series2.add(150.0, 16495.3); 201 series2.add(160.0, 17943.6); 202 series2.add(170.0, 18500.7); 203 series2.add(180.0, 19595.9); 204 205 return new XYSeriesCollection(series2); 206 207 } 208 209 220 225 public static void main(String [] args) { 226 227 CombinedXYPlotDemo4 demo = new CombinedXYPlotDemo4("CombinedDomainXYPlot Demo"); 228 demo.pack(); 229 RefineryUtilities.centerFrameOnScreen(demo); 230 demo.setVisible(true); 231 232 } 233 234 } 235 | Popular Tags |