1 46 47 package org.jfree.chart.junit; 48 49 import java.awt.Graphics2D ; 50 import java.awt.geom.Rectangle2D ; 51 import java.awt.image.BufferedImage ; 52 53 import junit.framework.Test; 54 import junit.framework.TestCase; 55 import junit.framework.TestSuite; 56 57 import org.jfree.chart.ChartFactory; 58 import org.jfree.chart.JFreeChart; 59 import org.jfree.chart.axis.ValueAxis; 60 import org.jfree.chart.event.ChartChangeEvent; 61 import org.jfree.chart.event.ChartChangeListener; 62 import org.jfree.chart.labels.CategoryToolTipGenerator; 63 import org.jfree.chart.labels.StandardCategoryToolTipGenerator; 64 import org.jfree.chart.plot.CategoryPlot; 65 import org.jfree.chart.plot.PlotOrientation; 66 import org.jfree.chart.renderer.category.CategoryItemRenderer; 67 import org.jfree.chart.urls.CategoryURLGenerator; 68 import org.jfree.chart.urls.StandardCategoryURLGenerator; 69 import org.jfree.data.Range; 70 import org.jfree.data.category.CategoryDataset; 71 import org.jfree.data.general.DatasetUtilities; 72 73 76 public class StackedBarChartTests extends TestCase { 77 78 79 private JFreeChart chart; 80 81 86 public static Test suite() { 87 return new TestSuite(StackedBarChartTests.class); 88 } 89 90 95 public StackedBarChartTests(String name) { 96 super(name); 97 } 98 99 102 protected void setUp() { 103 this.chart = createChart(); 104 } 105 106 110 public void testDrawWithNullInfo() { 111 112 boolean success = false; 113 114 try { 115 BufferedImage image = new BufferedImage ( 116 200 , 100, BufferedImage.TYPE_INT_RGB 117 ); 118 Graphics2D g2 = image.createGraphics(); 119 this.chart.draw( 120 g2, new Rectangle2D.Double (0, 0, 200, 100), null, null 121 ); 122 g2.dispose(); 123 success = true; 124 } 125 catch (Exception e) { 126 success = false; 127 } 128 129 assertTrue(success); 130 131 } 132 133 136 public void testReplaceDataset() { 137 138 Number [][] data = new Integer [][] 140 {{new Integer (-30), new Integer (-20)}, 141 {new Integer (-10), new Integer (10)}, 142 {new Integer (20), new Integer (30)}}; 143 144 CategoryDataset newData 145 = DatasetUtilities.createCategoryDataset("S", "C", data); 146 147 LocalListener l = new LocalListener(); 148 this.chart.addChangeListener(l); 149 this.chart.getCategoryPlot().setDataset(newData); 150 assertEquals(true, l.flag); 151 ValueAxis axis = this.chart.getCategoryPlot().getRangeAxis(); 152 Range range = axis.getRange(); 153 assertTrue("Expecting the lower bound of the range to be around -30: " 154 + range.getLowerBound(), range.getLowerBound() <= -30); 155 assertTrue("Expecting the upper bound of the range to be around 30: " 156 + range.getUpperBound(), range.getUpperBound() >= 30); 157 158 } 159 160 164 public void testSetSeriesToolTipGenerator() { 165 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 166 CategoryItemRenderer renderer = plot.getRenderer(); 167 StandardCategoryToolTipGenerator tt 168 = new StandardCategoryToolTipGenerator(); 169 renderer.setSeriesToolTipGenerator(0, tt); 170 CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0); 171 assertTrue(tt2 == tt); 172 } 173 174 178 public void testSetSeriesURLGenerator() { 179 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 180 CategoryItemRenderer renderer = plot.getRenderer(); 181 StandardCategoryURLGenerator url1 182 = new StandardCategoryURLGenerator(); 183 renderer.setSeriesItemURLGenerator(0, url1); 184 CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0); 185 assertTrue(url2 == url1); 186 } 187 188 193 private static JFreeChart createChart() { 194 195 Number [][] data = new Integer [][] 197 {{new Integer (-3), new Integer (-2)}, 198 {new Integer (-1), new Integer (1)}, 199 {new Integer (2), new Integer (3)}}; 200 201 CategoryDataset dataset 202 = DatasetUtilities.createCategoryDataset("S", "C", data); 203 204 return ChartFactory.createStackedBarChart( 206 "Stacked Bar Chart", "Domain", "Range", 208 dataset, PlotOrientation.HORIZONTAL, 210 true, true, 212 true 213 ); 214 215 } 216 217 220 static class LocalListener implements ChartChangeListener { 221 222 223 private boolean flag = false; 224 225 230 public void chartChanged(ChartChangeEvent event) { 231 this.flag = true; 232 } 233 234 } 235 236 } 237 | Popular Tags |