1 41 42 package org.jfree.chart.plot.junit; 43 44 import java.awt.Font ; 45 import java.io.ByteArrayInputStream ; 46 import java.io.ByteArrayOutputStream ; 47 import java.io.ObjectInput ; 48 import java.io.ObjectInputStream ; 49 import java.io.ObjectOutput ; 50 import java.io.ObjectOutputStream ; 51 import java.util.List ; 52 53 import junit.framework.Test; 54 import junit.framework.TestCase; 55 import junit.framework.TestSuite; 56 57 import org.jfree.chart.annotations.XYTextAnnotation; 58 import org.jfree.chart.axis.AxisLocation; 59 import org.jfree.chart.axis.NumberAxis; 60 import org.jfree.chart.plot.CombinedRangeXYPlot; 61 import org.jfree.chart.plot.PlotOrientation; 62 import org.jfree.chart.plot.XYPlot; 63 import org.jfree.chart.renderer.xy.StandardXYItemRenderer; 64 import org.jfree.chart.renderer.xy.XYItemRenderer; 65 import org.jfree.data.xy.XYDataset; 66 import org.jfree.data.xy.XYSeries; 67 import org.jfree.data.xy.XYSeriesCollection; 68 69 72 public class CombinedRangeXYPlotTests extends TestCase { 73 74 79 public static Test suite() { 80 return new TestSuite(CombinedRangeXYPlotTests.class); 81 } 82 83 88 public CombinedRangeXYPlotTests(String name) { 89 super(name); 90 } 91 92 95 public void testEquals() { 96 CombinedRangeXYPlot plot1 = createPlot(); 97 CombinedRangeXYPlot plot2 = createPlot(); 98 assertTrue(plot1.equals(plot2)); 99 assertTrue(plot2.equals(plot1)); 100 } 101 102 105 public void testRemoveSubplot() { 106 CombinedRangeXYPlot plot = new CombinedRangeXYPlot(); 107 XYPlot plot1 = new XYPlot(); 108 XYPlot plot2 = new XYPlot(); 109 plot.add(plot1); 110 plot.add(plot2); 111 plot.remove(plot2); 113 List plots = plot.getSubplots(); 114 assertTrue(plots.get(0) == plot1); 115 } 116 117 120 public void testCloning() { 121 CombinedRangeXYPlot plot1 = createPlot(); 122 CombinedRangeXYPlot plot2 = null; 123 try { 124 plot2 = (CombinedRangeXYPlot) plot1.clone(); 125 } 126 catch (CloneNotSupportedException e) { 127 System.err.println("Failed to clone."); 128 } 129 assertTrue(plot1 != plot2); 130 assertTrue(plot1.getClass() == plot2.getClass()); 131 assertTrue(plot1.equals(plot2)); 132 } 133 134 137 public void testSerialization() { 138 139 CombinedRangeXYPlot plot1 = createPlot(); 140 CombinedRangeXYPlot plot2 = null; 141 142 try { 143 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 144 ObjectOutput out = new ObjectOutputStream (buffer); 145 out.writeObject(plot1); 146 out.close(); 147 148 ObjectInput in = new ObjectInputStream ( 149 new ByteArrayInputStream (buffer.toByteArray()) 150 ); 151 plot2 = (CombinedRangeXYPlot) in.readObject(); 152 in.close(); 153 } 154 catch (Exception e) { 155 e.printStackTrace(); 156 } 157 assertEquals(plot1, plot2); 158 159 } 160 161 166 private XYDataset createDataset1() { 167 168 XYSeries series1 = new XYSeries("Series 1"); 170 series1.add(10.0, 12353.3); 171 series1.add(20.0, 13734.4); 172 series1.add(30.0, 14525.3); 173 series1.add(40.0, 13984.3); 174 series1.add(50.0, 12999.4); 175 series1.add(60.0, 14274.3); 176 series1.add(70.0, 15943.5); 177 series1.add(80.0, 14845.3); 178 series1.add(90.0, 14645.4); 179 series1.add(100.0, 16234.6); 180 series1.add(110.0, 17232.3); 181 series1.add(120.0, 14232.2); 182 series1.add(130.0, 13102.2); 183 series1.add(140.0, 14230.2); 184 series1.add(150.0, 11235.2); 185 186 XYSeries series2 = new XYSeries("Series 2"); 187 series2.add(10.0, 15000.3); 188 series2.add(20.0, 11000.4); 189 series2.add(30.0, 17000.3); 190 series2.add(40.0, 15000.3); 191 series2.add(50.0, 14000.4); 192 series2.add(60.0, 12000.3); 193 series2.add(70.0, 11000.5); 194 series2.add(80.0, 12000.3); 195 series2.add(90.0, 13000.4); 196 series2.add(100.0, 12000.6); 197 series2.add(110.0, 13000.3); 198 series2.add(120.0, 17000.2); 199 series2.add(130.0, 18000.2); 200 series2.add(140.0, 16000.2); 201 series2.add(150.0, 17000.2); 202 203 XYSeriesCollection collection = new XYSeriesCollection(); 204 collection.addSeries(series1); 205 collection.addSeries(series2); 206 return collection; 207 208 } 209 210 215 private XYDataset createDataset2() { 216 217 XYSeries series2 = new XYSeries("Series 3"); 219 220 series2.add(10.0, 16853.2); 221 series2.add(20.0, 19642.3); 222 series2.add(30.0, 18253.5); 223 series2.add(40.0, 15352.3); 224 series2.add(50.0, 13532.0); 225 series2.add(100.0, 12635.3); 226 series2.add(110.0, 13998.2); 227 series2.add(120.0, 11943.2); 228 series2.add(130.0, 16943.9); 229 series2.add(140.0, 17843.2); 230 series2.add(150.0, 16495.3); 231 series2.add(160.0, 17943.6); 232 series2.add(170.0, 18500.7); 233 series2.add(180.0, 19595.9); 234 235 return new XYSeriesCollection(series2); 236 237 } 238 239 244 private CombinedRangeXYPlot createPlot() { 245 XYDataset data1 = createDataset1(); 247 XYItemRenderer renderer1 = new StandardXYItemRenderer(); 248 NumberAxis rangeAxis1 = new NumberAxis("Range 1"); 249 XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1); 250 subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); 251 252 XYTextAnnotation annotation 253 = new XYTextAnnotation("Hello!", 50.0, 10000.0); 254 annotation.setFont(new Font ("SansSerif", Font.PLAIN, 9)); 255 annotation.setRotationAngle(Math.PI / 4.0); 256 subplot1.addAnnotation(annotation); 257 258 XYDataset data2 = createDataset2(); 260 XYItemRenderer renderer2 = new StandardXYItemRenderer(); 261 NumberAxis rangeAxis2 = new NumberAxis("Range 2"); 262 rangeAxis2.setAutoRangeIncludesZero(false); 263 XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2); 264 subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT); 265 266 CombinedRangeXYPlot plot 268 = new CombinedRangeXYPlot(new NumberAxis("Range")); 269 plot.setGap(10.0); 270 271 plot.add(subplot1, 1); 273 plot.add(subplot2, 1); 274 plot.setOrientation(PlotOrientation.VERTICAL); 275 return plot; 276 } 277 278 } 279 | Popular Tags |