AreaChartTest.java 6.4 KB

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