WaterfallChartTest.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * WaterfallChartTest.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. * 12-Apr-2005 : Version 1 (DG);
  38. *
  39. */
  40. package org.jfree.chart;
  41. import static org.junit.Assert.assertTrue;
  42. import static org.junit.Assert.fail;
  43. import java.awt.Graphics2D;
  44. import java.awt.geom.Rectangle2D;
  45. import java.awt.image.BufferedImage;
  46. import org.jfree.chart.labels.CategoryToolTipGenerator;
  47. import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
  48. import org.jfree.chart.plot.CategoryPlot;
  49. import org.jfree.chart.plot.PlotOrientation;
  50. import org.jfree.chart.renderer.category.CategoryItemRenderer;
  51. import org.jfree.chart.urls.CategoryURLGenerator;
  52. import org.jfree.chart.urls.StandardCategoryURLGenerator;
  53. import org.jfree.data.category.CategoryDataset;
  54. import org.jfree.data.general.DatasetUtilities;
  55. import org.junit.Before;
  56. import org.junit.Test;
  57. /**
  58. * Some tests for a waterfall chart.
  59. */
  60. public class WaterfallChartTest {
  61. /** A chart. */
  62. private JFreeChart chart;
  63. /**
  64. * Common test setup.
  65. */
  66. @Before
  67. public void setUp() {
  68. this.chart = createWaterfallChart();
  69. }
  70. /**
  71. * Draws the chart with a null info object to make sure that no exceptions
  72. * are thrown (a problem that was occurring at one point).
  73. */
  74. @Test
  75. public void testDrawWithNullInfo() {
  76. try {
  77. BufferedImage image = new BufferedImage(200 , 100,
  78. BufferedImage.TYPE_INT_RGB);
  79. Graphics2D g2 = image.createGraphics();
  80. this.chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null,
  81. null);
  82. g2.dispose();
  83. }
  84. catch (Exception e) {
  85. fail("There should be no exception.");
  86. }
  87. }
  88. /**
  89. * Check that setting a tool tip generator for a series does override the
  90. * default generator.
  91. */
  92. @Test
  93. public void testSetSeriesToolTipGenerator() {
  94. CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
  95. CategoryItemRenderer renderer = plot.getRenderer();
  96. StandardCategoryToolTipGenerator tt
  97. = new StandardCategoryToolTipGenerator();
  98. renderer.setSeriesToolTipGenerator(0, tt);
  99. CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
  100. assertTrue(tt2 == tt);
  101. }
  102. /**
  103. * Check that setting a URL generator for a series does override the
  104. * default generator.
  105. */
  106. @Test
  107. public void testSetSeriesURLGenerator() {
  108. CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
  109. CategoryItemRenderer renderer = plot.getRenderer();
  110. StandardCategoryURLGenerator url1
  111. = new StandardCategoryURLGenerator();
  112. renderer.setSeriesItemURLGenerator(0, url1);
  113. CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
  114. assertTrue(url2 == url1);
  115. }
  116. /**
  117. * Create a bar chart with sample data in the range -3 to +3.
  118. *
  119. * @return The chart.
  120. */
  121. private static JFreeChart createWaterfallChart() {
  122. Number[][] data = new Integer[][]
  123. {{new Integer(-3), new Integer(-2)},
  124. {new Integer(-1), new Integer(1)},
  125. {new Integer(2), new Integer(3)}};
  126. CategoryDataset dataset = DatasetUtilities.createCategoryDataset("S",
  127. "C", data);
  128. return ChartFactory.createWaterfallChart(
  129. "Waterfall Chart",
  130. "Domain", "Range",
  131. dataset,
  132. PlotOrientation.HORIZONTAL,
  133. true, // include legend
  134. true,
  135. true
  136. );
  137. }
  138. }