1 42 43 package org.jfree.chart.junit; 44 45 import java.awt.Graphics2D ; 46 import java.awt.geom.Rectangle2D ; 47 import java.awt.image.BufferedImage ; 48 49 import junit.framework.Test; 50 import junit.framework.TestCase; 51 import junit.framework.TestSuite; 52 53 import org.jfree.chart.ChartFactory; 54 import org.jfree.chart.JFreeChart; 55 import org.jfree.chart.axis.ValueAxis; 56 import org.jfree.chart.event.ChartChangeEvent; 57 import org.jfree.chart.event.ChartChangeListener; 58 import org.jfree.chart.labels.StandardXYToolTipGenerator; 59 import org.jfree.chart.labels.XYToolTipGenerator; 60 import org.jfree.chart.plot.PlotOrientation; 61 import org.jfree.chart.plot.XYPlot; 62 import org.jfree.chart.renderer.xy.XYItemRenderer; 63 import org.jfree.data.Range; 64 import org.jfree.data.xy.XYDataset; 65 import org.jfree.data.xy.XYSeries; 66 import org.jfree.data.xy.XYSeriesCollection; 67 68 72 public class ScatterPlotTests extends TestCase { 73 74 75 private JFreeChart chart; 76 77 82 public static Test suite() { 83 return new TestSuite(ScatterPlotTests.class); 84 } 85 86 91 public ScatterPlotTests(String name) { 92 super(name); 93 } 94 95 98 protected void setUp() { 99 100 this.chart = createChart(); 101 102 } 103 104 108 public void testDrawWithNullInfo() { 109 110 boolean success = false; 111 112 try { 113 BufferedImage image = new BufferedImage ( 114 200 , 100, BufferedImage.TYPE_INT_RGB 115 ); 116 Graphics2D g2 = image.createGraphics(); 117 this.chart.draw( 118 g2, new Rectangle2D.Double (0, 0, 200, 100), null, null 119 ); 120 g2.dispose(); 121 success = true; 122 } 123 catch (Exception e) { 124 success = false; 125 e.printStackTrace(); 126 } 127 128 assertTrue(success); 129 130 } 131 132 135 public void testReplaceDataset() { 136 137 XYSeries series1 = new XYSeries("Series 1"); 139 series1.add(10.0, 10.0); 140 series1.add(20.0, 20.0); 141 series1.add(30.0, 30.0); 142 XYDataset dataset = new XYSeriesCollection(series1); 143 144 LocalListener l = new LocalListener(); 145 this.chart.addChangeListener(l); 146 this.chart.getXYPlot().setDataset(dataset); 147 assertEquals(true, l.flag); 148 ValueAxis axis = this.chart.getXYPlot().getRangeAxis(); 149 Range range = axis.getRange(); 150 assertTrue("Expecting the lower bound of the range to be around 10: " 151 + range.getLowerBound(), range.getLowerBound() <= 10); 152 assertTrue("Expecting the upper bound of the range to be around 30: " 153 + range.getUpperBound(), range.getUpperBound() >= 30); 154 155 } 156 157 161 public void testSetSeriesToolTipGenerator() { 162 XYPlot plot = (XYPlot) this.chart.getPlot(); 163 XYItemRenderer renderer = plot.getRenderer(); 164 StandardXYToolTipGenerator tt = new StandardXYToolTipGenerator(); 165 renderer.setSeriesToolTipGenerator(0, tt); 166 XYToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0); 167 assertTrue(tt2 == tt); 168 } 169 170 175 private static JFreeChart createChart() { 176 177 XYSeries series1 = new XYSeries("Series 1"); 179 series1.add(1.0, 1.0); 180 series1.add(2.0, 2.0); 181 series1.add(3.0, 3.0); 182 XYDataset dataset = new XYSeriesCollection(series1); 183 184 return ChartFactory.createScatterPlot( 186 "Scatter Plot", "Domain", 188 "Range", 189 dataset, PlotOrientation.VERTICAL, 191 true, true, false ); 195 196 } 197 198 202 static class LocalListener implements ChartChangeListener { 203 204 205 private boolean flag = false; 206 207 212 public void chartChanged(ChartChangeEvent event) { 213 this.flag = true; 214 } 215 216 } 217 218 } 219 | Popular Tags |