BarChart3DTest.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. package org.jfree.chart;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertSame;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import java.awt.Graphics2D;
  49. import java.awt.geom.Rectangle2D;
  50. import java.awt.image.BufferedImage;
  51. import org.jfree.chart.axis.ValueAxis;
  52. import org.jfree.chart.event.ChartChangeEvent;
  53. import org.jfree.chart.event.ChartChangeListener;
  54. import org.jfree.chart.labels.CategoryToolTipGenerator;
  55. import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
  56. import org.jfree.chart.plot.CategoryPlot;
  57. import org.jfree.chart.plot.PlotOrientation;
  58. import org.jfree.chart.renderer.category.CategoryItemRenderer;
  59. import org.jfree.chart.urls.CategoryURLGenerator;
  60. import org.jfree.chart.urls.StandardCategoryURLGenerator;
  61. import org.jfree.data.Range;
  62. import org.jfree.data.category.CategoryDataset;
  63. import org.jfree.data.general.DatasetUtilities;
  64. import org.junit.Before;
  65. import org.junit.Test;
  66. /**
  67. * Tests for a 3D bar chart.
  68. */
  69. public class BarChart3DTest {
  70. /** The chart. */
  71. private JFreeChart chart;
  72. /**
  73. * Common test setup.
  74. */
  75. @Before
  76. public void setUp() {
  77. this.chart = createBarChart3D();
  78. }
  79. /**
  80. * Draws the chart with a null info object to make sure that no exceptions
  81. * are thrown (a problem that was occurring at one point).
  82. */
  83. @Test
  84. public void testDrawWithNullInfo() {
  85. try {
  86. BufferedImage image = new BufferedImage(200 , 100,
  87. BufferedImage.TYPE_INT_RGB);
  88. Graphics2D g2 = image.createGraphics();
  89. this.chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null,
  90. null);
  91. g2.dispose();
  92. }
  93. catch (Exception e) {
  94. fail("There should be no exception.");
  95. }
  96. }
  97. /**
  98. * Replaces the dataset and checks that the data range is as expected.
  99. */
  100. @Test
  101. public void testReplaceDataset() {
  102. // create a dataset...
  103. Number[][] data = new Integer[][]
  104. {{new Integer(-30), new Integer(-20)},
  105. {new Integer(-10), new Integer(10)},
  106. {new Integer(20), new Integer(30)}};
  107. CategoryDataset newData = DatasetUtilities.createCategoryDataset("S",
  108. "C", data);
  109. LocalListener l = new LocalListener();
  110. this.chart.addChangeListener(l);
  111. CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
  112. plot.setDataset(newData);
  113. assertEquals(true, l.flag);
  114. ValueAxis axis = plot.getRangeAxis();
  115. Range range = axis.getRange();
  116. assertTrue("Expecting the lower bound of the range to be around -30: "
  117. + range.getLowerBound(), range.getLowerBound() <= -30);
  118. assertTrue("Expecting the upper bound of the range to be around 30: "
  119. + range.getUpperBound(), range.getUpperBound() >= 30);
  120. }
  121. /**
  122. * Check that setting a tool tip generator for a series does override the
  123. * default generator.
  124. */
  125. @Test
  126. public void testSetSeriesToolTipGenerator() {
  127. CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
  128. CategoryItemRenderer renderer = plot.getRenderer();
  129. StandardCategoryToolTipGenerator tt
  130. = new StandardCategoryToolTipGenerator();
  131. renderer.setSeriesToolTipGenerator(0, tt);
  132. CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
  133. assertSame(tt2, tt);
  134. }
  135. /**
  136. * Check that setting a URL generator for a series does override the
  137. * default generator.
  138. */
  139. @Test
  140. public void testSetSeriesURLGenerator() {
  141. CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
  142. CategoryItemRenderer renderer = plot.getRenderer();
  143. StandardCategoryURLGenerator url1
  144. = new StandardCategoryURLGenerator();
  145. renderer.setSeriesItemURLGenerator(0, url1);
  146. CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
  147. assertSame(url2, url1);
  148. }
  149. /**
  150. * Create a bar chart with sample data in the range -3 to +3.
  151. *
  152. * @return The chart.
  153. */
  154. private static JFreeChart createBarChart3D() {
  155. Number[][] data = new Integer[][]
  156. {{new Integer(-3), new Integer(-2)},
  157. {new Integer(-1), new Integer(1)},
  158. {new Integer(2), new Integer(3)}};
  159. CategoryDataset dataset = DatasetUtilities.createCategoryDataset("S",
  160. "C", data);
  161. return ChartFactory.createBarChart3D("Bar Chart 3D", "Domain", "Range",
  162. dataset, PlotOrientation.HORIZONTAL, true, true, true);
  163. }
  164. /**
  165. * A chart change listener.
  166. *
  167. */
  168. static class LocalListener implements ChartChangeListener {
  169. /** A flag. */
  170. private boolean flag;
  171. /**
  172. * Event handler.
  173. *
  174. * @param event the event.
  175. */
  176. @Override
  177. public void chartChanged(ChartChangeEvent event) {
  178. this.flag = true;
  179. }
  180. }
  181. }