1 39 40 package org.jfree.chart.junit; 41 42 import java.awt.Graphics2D ; 43 import java.awt.geom.Rectangle2D ; 44 import java.awt.image.BufferedImage ; 45 46 import junit.framework.Test; 47 import junit.framework.TestCase; 48 import junit.framework.TestSuite; 49 50 import org.jfree.chart.ChartFactory; 51 import org.jfree.chart.JFreeChart; 52 import org.jfree.chart.axis.ValueAxis; 53 import org.jfree.chart.event.ChartChangeEvent; 54 import org.jfree.chart.event.ChartChangeListener; 55 import org.jfree.chart.plot.PlotOrientation; 56 import org.jfree.data.CategoryDataset; 57 import org.jfree.data.DatasetUtilities; 58 import org.jfree.data.Range; 59 60 65 public class StackedHorizontalBarChartTests extends TestCase { 66 67 68 private JFreeChart chart; 69 70 75 public static Test suite() { 76 return new TestSuite(StackedHorizontalBarChartTests.class); 77 } 78 79 84 public StackedHorizontalBarChartTests(String name) { 85 super(name); 86 } 87 88 91 protected void setUp() { 92 93 this.chart = createChart(); 94 95 } 96 97 101 public void testDrawWithNullInfo() { 102 103 boolean success = false; 104 105 try { 106 BufferedImage image = new BufferedImage (200 , 100, BufferedImage.TYPE_INT_RGB); 107 Graphics2D g2 = image.createGraphics(); 108 chart.draw(g2, new Rectangle2D.Double (0, 0, 200, 100), null); 109 g2.dispose(); 110 success = true; 111 } 112 catch (Exception e) { 113 success = false; 114 } 115 116 assertTrue(success); 117 118 } 119 120 123 public void testReplaceDataset() { 124 125 Number [][] data = new Integer [][] 127 {{new Integer (-30), new Integer (-20)}, 128 {new Integer (-10), new Integer (10)}, 129 {new Integer (20), new Integer (30)}}; 130 131 CategoryDataset newData = DatasetUtilities.createCategoryDataset("S", "C", data); 132 133 LocalListener l = new LocalListener(); 134 chart.addChangeListener(l); 135 chart.getCategoryPlot().setDataset(newData); 136 assertEquals(true, l.flag); 137 ValueAxis axis = chart.getCategoryPlot().getRangeAxis(); 138 Range range = axis.getRange(); 139 assertTrue("Expecting the lower bound of the range to be around -30: " 140 + range.getLowerBound(), range.getLowerBound() <= -30); 141 assertTrue("Expecting the upper bound of the range to be around 30: " 142 + range.getUpperBound(), range.getUpperBound() >= 30); 143 144 } 145 146 151 private static JFreeChart createChart() { 152 153 Number [][] data = new Integer [][] 155 {{new Integer (-3), new Integer (-2)}, 156 {new Integer (-1), new Integer (1)}, 157 {new Integer (2), new Integer (3)}}; 158 159 CategoryDataset dataset = DatasetUtilities.createCategoryDataset("S", "C", data); 160 161 return ChartFactory.createStackedBarChart( 163 "Stacked Horizontal Bar Chart", "Domain", "Range", 165 dataset, PlotOrientation.HORIZONTAL, 167 true, true, 169 false 170 ); 171 172 } 173 174 179 static class LocalListener implements ChartChangeListener { 180 181 182 private boolean flag = false; 183 184 189 public void chartChanged(ChartChangeEvent event) { 190 flag = true; 191 } 192 193 } 194 195 } 196 | Popular Tags |