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.CombinedDomainXYPlot; 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 CombinedDomainXYPlotTests extends TestCase { 73 74 79 public static Test suite() { 80 return new TestSuite(CombinedDomainXYPlotTests.class); 81 } 82 83 88 public CombinedDomainXYPlotTests(String name) { 89 super(name); 90 } 91 92 95 public void testConstructor1() { 96 CombinedDomainXYPlot plot = new CombinedDomainXYPlot(null); 97 assertEquals(null, plot.getDomainAxis()); 98 } 99 100 103 public void testRemoveSubplot() { 104 CombinedDomainXYPlot plot = new CombinedDomainXYPlot(); 105 XYPlot plot1 = new XYPlot(); 106 XYPlot plot2 = new XYPlot(); 107 plot.add(plot1); 108 plot.add(plot2); 109 plot.remove(plot2); 111 List plots = plot.getSubplots(); 112 assertTrue(plots.get(0) == plot1); 113 } 114 115 118 public void testEquals() { 119 CombinedDomainXYPlot plot1 = createPlot(); 120 CombinedDomainXYPlot plot2 = createPlot(); 121 assertTrue(plot1.equals(plot2)); 122 assertTrue(plot2.equals(plot1)); 123 } 124 125 128 public void testCloning() { 129 CombinedDomainXYPlot plot1 = createPlot(); 130 CombinedDomainXYPlot plot2 = null; 131 try { 132 plot2 = (CombinedDomainXYPlot) plot1.clone(); 133 } 134 catch (CloneNotSupportedException e) { 135 System.err.println("Failed to clone."); 136 } 137 assertTrue(plot1 != plot2); 138 assertTrue(plot1.getClass() == plot2.getClass()); 139 assertTrue(plot1.equals(plot2)); 140 } 141 142 145 public void testSerialization() { 146 147 CombinedDomainXYPlot plot1 = createPlot(); 148 CombinedDomainXYPlot plot2 = null; 149 150 try { 151 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 152 ObjectOutput out = new ObjectOutputStream (buffer); 153 out.writeObject(plot1); 154 out.close(); 155 156 ObjectInput in = new ObjectInputStream ( 157 new ByteArrayInputStream (buffer.toByteArray()) 158 ); 159 plot2 = (CombinedDomainXYPlot) in.readObject(); 160 in.close(); 161 } 162 catch (Exception e) { 163 e.printStackTrace(); 164 } 165 assertEquals(plot1, plot2); 166 167 } 168 169 174 private XYDataset createDataset1() { 175 176 XYSeries series1 = new XYSeries("Series 1"); 178 series1.add(10.0, 12353.3); 179 series1.add(20.0, 13734.4); 180 series1.add(30.0, 14525.3); 181 series1.add(40.0, 13984.3); 182 series1.add(50.0, 12999.4); 183 series1.add(60.0, 14274.3); 184 series1.add(70.0, 15943.5); 185 series1.add(80.0, 14845.3); 186 series1.add(90.0, 14645.4); 187 series1.add(100.0, 16234.6); 188 series1.add(110.0, 17232.3); 189 series1.add(120.0, 14232.2); 190 series1.add(130.0, 13102.2); 191 series1.add(140.0, 14230.2); 192 series1.add(150.0, 11235.2); 193 194 XYSeries series2 = new XYSeries("Series 2"); 195 series2.add(10.0, 15000.3); 196 series2.add(20.0, 11000.4); 197 series2.add(30.0, 17000.3); 198 series2.add(40.0, 15000.3); 199 series2.add(50.0, 14000.4); 200 series2.add(60.0, 12000.3); 201 series2.add(70.0, 11000.5); 202 series2.add(80.0, 12000.3); 203 series2.add(90.0, 13000.4); 204 series2.add(100.0, 12000.6); 205 series2.add(110.0, 13000.3); 206 series2.add(120.0, 17000.2); 207 series2.add(130.0, 18000.2); 208 series2.add(140.0, 16000.2); 209 series2.add(150.0, 17000.2); 210 211 XYSeriesCollection collection = new XYSeriesCollection(); 212 collection.addSeries(series1); 213 collection.addSeries(series2); 214 return collection; 215 216 } 217 218 223 private XYDataset createDataset2() { 224 225 XYSeries series2 = new XYSeries("Series 3"); 227 228 series2.add(10.0, 16853.2); 229 series2.add(20.0, 19642.3); 230 series2.add(30.0, 18253.5); 231 series2.add(40.0, 15352.3); 232 series2.add(50.0, 13532.0); 233 series2.add(100.0, 12635.3); 234 series2.add(110.0, 13998.2); 235 series2.add(120.0, 11943.2); 236 series2.add(130.0, 16943.9); 237 series2.add(140.0, 17843.2); 238 series2.add(150.0, 16495.3); 239 series2.add(160.0, 17943.6); 240 series2.add(170.0, 18500.7); 241 series2.add(180.0, 19595.9); 242 243 return new XYSeriesCollection(series2); 244 245 } 246 247 252 private CombinedDomainXYPlot createPlot() { 253 XYDataset data1 = createDataset1(); 255 XYItemRenderer renderer1 = new StandardXYItemRenderer(); 256 NumberAxis rangeAxis1 = new NumberAxis("Range 1"); 257 XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1); 258 subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); 259 260 XYTextAnnotation annotation 261 = new XYTextAnnotation("Hello!", 50.0, 10000.0); 262 annotation.setFont(new Font ("SansSerif", Font.PLAIN, 9)); 263 annotation.setRotationAngle(Math.PI / 4.0); 264 subplot1.addAnnotation(annotation); 265 266 XYDataset data2 = createDataset2(); 268 XYItemRenderer renderer2 = new StandardXYItemRenderer(); 269 NumberAxis rangeAxis2 = new NumberAxis("Range 2"); 270 rangeAxis2.setAutoRangeIncludesZero(false); 271 XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2); 272 subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT); 273 274 CombinedDomainXYPlot plot 276 = new CombinedDomainXYPlot(new NumberAxis("Domain")); 277 plot.setGap(10.0); 278 279 plot.add(subplot1, 1); 281 plot.add(subplot2, 1); 282 plot.setOrientation(PlotOrientation.VERTICAL); 283 return plot; 284 } 285 } 286 | Popular Tags |