LineChartTest.java 6.2 KB

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