1 /* =========================================================== 2 * JFreeChart : a free chart library for the Java(tm) platform 3 * =========================================================== 4 * 5 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors. 6 * 7 * Project Info: http://www.jfree.org/jfreechart/index.html 8 * 9 * This library is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU Lesser General Public License as published by 11 * the Free Software Foundation; either version 2.1 of the License, or 12 * (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17 * License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22 * USA. 23 * 24 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 25 * Other names may be trademarks of their respective owners.] 26 * 27 * ------------------- 28 * BarChart3DTest.java 29 * ------------------- 30 * (C) Copyright 2002-2013, by Object Refinery Limited. 31 * 32 * Original Author: David Gilbert (for Object Refinery Limited); 33 * Contributor(s): -; 34 * 35 * Changes: 36 * -------- 37 * 11-Jun-2002 : Version 1 (DG); 38 * 25-Jun-2002 : Removed redundant code (DG); 39 * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG); 40 * 14-Jul-2003 : Renamed BarChart3DTests.java (DG); 41 * 42 */ 43 44 package org.jfree.chart; 45 46 import static org.junit.Assert.assertEquals; 47 import static org.junit.Assert.assertSame; 48 import static org.junit.Assert.assertTrue; 49 import static org.junit.Assert.fail; 50 51 import java.awt.Graphics2D; 52 import java.awt.geom.Rectangle2D; 53 import java.awt.image.BufferedImage; 54 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 import org.junit.Before; 69 import org.junit.Test; 70 71 /** 72 * Tests for a 3D bar chart. 73 */ 74 public class BarChart3DTest { 75 76 /** The chart. */ 77 private JFreeChart chart; 78 79 /** 80 * Common test setup. 81 */ 82 @Before setUp()83 public void setUp() { 84 this.chart = createBarChart3D(); 85 } 86 87 /** 88 * Draws the chart with a null info object to make sure that no exceptions 89 * are thrown (a problem that was occurring at one point). 90 */ 91 @Test testDrawWithNullInfo()92 public void testDrawWithNullInfo() { 93 try { 94 BufferedImage image = new BufferedImage(200 , 100, 95 BufferedImage.TYPE_INT_RGB); 96 Graphics2D g2 = image.createGraphics(); 97 this.chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, 98 null); 99 g2.dispose(); 100 } 101 catch (Exception e) { 102 fail("There should be no exception."); 103 } 104 } 105 106 /** 107 * Replaces the dataset and checks that the data range is as expected. 108 */ 109 @Test testReplaceDataset()110 public void testReplaceDataset() { 111 112 // create a dataset... 113 Number[][] data = new Integer[][] 114 {{new Integer(-30), new Integer(-20)}, 115 {new Integer(-10), new Integer(10)}, 116 {new Integer(20), new Integer(30)}}; 117 118 CategoryDataset newData = DatasetUtilities.createCategoryDataset("S", 119 "C", data); 120 121 LocalListener l = new LocalListener(); 122 this.chart.addChangeListener(l); 123 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 124 plot.setDataset(newData); 125 assertEquals(true, l.flag); 126 ValueAxis axis = plot.getRangeAxis(); 127 Range range = axis.getRange(); 128 assertTrue("Expecting the lower bound of the range to be around -30: " 129 + range.getLowerBound(), range.getLowerBound() <= -30); 130 assertTrue("Expecting the upper bound of the range to be around 30: " 131 + range.getUpperBound(), range.getUpperBound() >= 30); 132 133 } 134 135 /** 136 * Check that setting a tool tip generator for a series does override the 137 * default generator. 138 */ 139 @Test testSetSeriesToolTipGenerator()140 public void testSetSeriesToolTipGenerator() { 141 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 142 CategoryItemRenderer renderer = plot.getRenderer(); 143 StandardCategoryToolTipGenerator tt 144 = new StandardCategoryToolTipGenerator(); 145 renderer.setSeriesToolTipGenerator(0, tt); 146 CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0); 147 assertSame(tt2, tt); 148 } 149 150 /** 151 * Check that setting a URL generator for a series does override the 152 * default generator. 153 */ 154 @Test testSetSeriesURLGenerator()155 public void testSetSeriesURLGenerator() { 156 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 157 CategoryItemRenderer renderer = plot.getRenderer(); 158 StandardCategoryURLGenerator url1 159 = new StandardCategoryURLGenerator(); 160 renderer.setSeriesItemURLGenerator(0, url1); 161 CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0); 162 assertSame(url2, url1); 163 } 164 165 /** 166 * Create a bar chart with sample data in the range -3 to +3. 167 * 168 * @return The chart. 169 */ createBarChart3D()170 private static JFreeChart createBarChart3D() { 171 Number[][] data = new Integer[][] 172 {{new Integer(-3), new Integer(-2)}, 173 {new Integer(-1), new Integer(1)}, 174 {new Integer(2), new Integer(3)}}; 175 176 CategoryDataset dataset = DatasetUtilities.createCategoryDataset("S", 177 "C", data); 178 return ChartFactory.createBarChart3D("Bar Chart 3D", "Domain", "Range", 179 dataset, PlotOrientation.HORIZONTAL, true, true, true); 180 181 } 182 183 /** 184 * A chart change listener. 185 * 186 */ 187 static class LocalListener implements ChartChangeListener { 188 189 /** A flag. */ 190 private boolean flag; 191 192 /** 193 * Event handler. 194 * 195 * @param event the event. 196 */ 197 @Override chartChanged(ChartChangeEvent event)198 public void chartChanged(ChartChangeEvent event) { 199 this.flag = true; 200 } 201 202 } 203 204 } 205