BarChartTest.java 6.7 KB

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