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