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.CategoryToolTipGenerator; 59 import org.jfree.chart.labels.StandardCategoryToolTipGenerator; 60 import org.jfree.chart.plot.CategoryPlot; 61 import org.jfree.chart.plot.PlotOrientation; 62 import org.jfree.chart.renderer.category.CategoryItemRenderer; 63 import org.jfree.chart.urls.CategoryURLGenerator; 64 import org.jfree.chart.urls.StandardCategoryURLGenerator; 65 import org.jfree.data.Range; 66 import org.jfree.data.category.CategoryDataset; 67 import org.jfree.data.general.DatasetUtilities; 68 69 72 public class StackedAreaChartTests extends TestCase { 73 74 75 private JFreeChart chart; 76 77 82 public static Test suite() { 83 return new TestSuite(StackedAreaChartTests.class); 84 } 85 86 91 public StackedAreaChartTests(String name) { 92 super(name); 93 } 94 95 98 protected void setUp() { 99 this.chart = createChart(); 100 } 101 102 106 public void testDrawWithNullInfo() { 107 108 boolean success = false; 109 try { 110 BufferedImage image = new BufferedImage ( 111 200 , 100, BufferedImage.TYPE_INT_RGB 112 ); 113 Graphics2D g2 = image.createGraphics(); 114 this.chart.draw( 115 g2, new Rectangle2D.Double (0, 0, 200, 100), null, null 116 ); 117 g2.dispose(); 118 success = true; 119 } 120 catch (Exception e) { 121 success = false; 122 } 123 assertTrue(success); 124 125 } 126 127 130 public void testReplaceDataset() { 131 132 Number [][] data = new Integer [][] 134 {{new Integer (-30), new Integer (-20)}, 135 {new Integer (-10), new Integer (10)}, 136 {new Integer (20), new Integer (30)}}; 137 138 CategoryDataset newData 139 = DatasetUtilities.createCategoryDataset("S", "C", data); 140 141 LocalListener l = new LocalListener(); 142 this.chart.addChangeListener(l); 143 this.chart.getCategoryPlot().setDataset(newData); 144 assertEquals(true, l.flag); 145 ValueAxis axis = this.chart.getCategoryPlot().getRangeAxis(); 146 Range range = axis.getRange(); 147 assertTrue("Expecting the lower bound of the range to be around -30: " 148 + range.getLowerBound(), range.getLowerBound() <= -30); 149 assertTrue("Expecting the upper bound of the range to be around 30: " 150 + range.getUpperBound(), range.getUpperBound() >= 30); 151 152 } 153 154 158 public void testSetSeriesToolTipGenerator() { 159 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 160 CategoryItemRenderer renderer = plot.getRenderer(); 161 StandardCategoryToolTipGenerator tt 162 = new StandardCategoryToolTipGenerator(); 163 renderer.setSeriesToolTipGenerator(0, tt); 164 CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0); 165 assertTrue(tt2 == tt); 166 } 167 168 172 public void testSetSeriesURLGenerator() { 173 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 174 CategoryItemRenderer renderer = plot.getRenderer(); 175 StandardCategoryURLGenerator url1 176 = new StandardCategoryURLGenerator(); 177 renderer.setSeriesItemURLGenerator(0, url1); 178 CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0); 179 assertTrue(url2 == url1); 180 } 181 182 187 private static JFreeChart createChart() { 188 189 Number [][] data = new Integer [][] 191 {{new Integer (-3), new Integer (-2)}, 192 {new Integer (-1), new Integer (1)}, 193 {new Integer (2), new Integer (3)}}; 194 195 CategoryDataset dataset 196 = DatasetUtilities.createCategoryDataset("S", "C", data); 197 198 return ChartFactory.createStackedAreaChart( 200 "Stacked Area Chart", "Domain", "Range", 202 dataset, PlotOrientation.HORIZONTAL, 204 true, true, 206 true 207 ); 208 209 } 210 211 214 static class LocalListener implements ChartChangeListener { 215 216 217 private boolean flag = false; 218 219 224 public void chartChanged(ChartChangeEvent event) { 225 this.flag = true; 226 } 227 228 } 229 230 } 231 | Popular Tags |