XYStepChartTest.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. * XYStepChartTest.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.StandardXYToolTipGenerator;
  51. import org.jfree.chart.labels.XYToolTipGenerator;
  52. import org.jfree.chart.plot.PlotOrientation;
  53. import org.jfree.chart.plot.XYPlot;
  54. import org.jfree.chart.renderer.xy.XYItemRenderer;
  55. import org.jfree.data.Range;
  56. import org.jfree.data.xy.XYDataset;
  57. import org.jfree.data.xy.XYSeries;
  58. import org.jfree.data.xy.XYSeriesCollection;
  59. import org.junit.Before;
  60. import org.junit.Test;
  61. /**
  62. * Some tests for an XY step plot.
  63. */
  64. public class XYStepChartTest {
  65. /** A chart. */
  66. private JFreeChart chart;
  67. /**
  68. * Common test setup.
  69. */
  70. @Before
  71. public void setUp() {
  72. this.chart = createChart();
  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. try {
  81. BufferedImage image = new BufferedImage(200 , 100,
  82. BufferedImage.TYPE_INT_RGB);
  83. Graphics2D g2 = image.createGraphics();
  84. this.chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null,
  85. null);
  86. g2.dispose();
  87. }
  88. catch (Exception e) {
  89. fail("No exception should be triggered.");
  90. }
  91. }
  92. /**
  93. * Replaces the dataset and checks that it has changed as expected.
  94. */
  95. @Test
  96. public void testReplaceDataset() {
  97. // create a dataset...
  98. XYSeries series1 = new XYSeries("Series 1");
  99. series1.add(10.0, 10.0);
  100. series1.add(20.0, 20.0);
  101. series1.add(30.0, 30.0);
  102. XYDataset dataset = new XYSeriesCollection(series1);
  103. LocalListener l = new LocalListener();
  104. this.chart.addChangeListener(l);
  105. XYPlot plot = (XYPlot) this.chart.getPlot();
  106. plot.setDataset(dataset);
  107. assertEquals(true, l.flag);
  108. ValueAxis axis = plot.getRangeAxis();
  109. Range range = axis.getRange();
  110. assertTrue("Expecting the lower bound of the range to be around 10: "
  111. + range.getLowerBound(), range.getLowerBound() <= 10);
  112. assertTrue("Expecting the upper bound of the range to be around 30: "
  113. + range.getUpperBound(), range.getUpperBound() >= 30);
  114. }
  115. /**
  116. * Check that setting a tool tip generator for a series does override the
  117. * default generator.
  118. */
  119. @Test
  120. public void testSetSeriesToolTipGenerator() {
  121. XYPlot plot = (XYPlot) this.chart.getPlot();
  122. XYItemRenderer renderer = plot.getRenderer();
  123. StandardXYToolTipGenerator tt = new StandardXYToolTipGenerator();
  124. renderer.setSeriesToolTipGenerator(0, tt);
  125. XYToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
  126. assertTrue(tt2 == tt);
  127. }
  128. /**
  129. * Create a horizontal bar chart with sample data in the range -3 to +3.
  130. *
  131. * @return The chart.
  132. */
  133. private static JFreeChart createChart() {
  134. // create a dataset...
  135. XYSeries series1 = new XYSeries("Series 1");
  136. series1.add(1.0, 1.0);
  137. series1.add(2.0, 2.0);
  138. series1.add(3.0, 3.0);
  139. XYDataset dataset = new XYSeriesCollection(series1);
  140. // create the chart...
  141. return ChartFactory.createXYStepChart(
  142. "Step Chart", // chart title
  143. "Domain",
  144. "Range",
  145. dataset, // data
  146. PlotOrientation.VERTICAL,
  147. true, // include legend
  148. true, // tooltips
  149. true // urls
  150. );
  151. }
  152. /**
  153. * A chart change listener.
  154. *
  155. */
  156. static class LocalListener implements ChartChangeListener {
  157. /** A flag. */
  158. private boolean flag = false;
  159. /**
  160. * Event handler.
  161. *
  162. * @param event the event.
  163. */
  164. @Override
  165. public void chartChanged(ChartChangeEvent event) {
  166. this.flag = true;
  167. }
  168. }
  169. }