CombinedRangeXYPlotTest.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. * CombinedRangeXYPlotTest.java
  29. * ----------------------------
  30. * (C) Copyright 2003-2013, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 21-Aug-2003 : Version 1 (DG);
  38. * 03-Jan-2008 : Added testNotification (DG);
  39. *
  40. */
  41. package org.jfree.chart.plot;
  42. import static org.junit.Assert.assertEquals;
  43. import static org.junit.Assert.assertTrue;
  44. import java.awt.Font;
  45. import java.awt.Graphics2D;
  46. import java.awt.geom.Rectangle2D;
  47. import java.awt.image.BufferedImage;
  48. import java.util.List;
  49. import org.jfree.chart.JFreeChart;
  50. import org.jfree.chart.TestUtilities;
  51. import org.jfree.chart.annotations.XYTextAnnotation;
  52. import org.jfree.chart.axis.AxisLocation;
  53. import org.jfree.chart.axis.NumberAxis;
  54. import org.jfree.chart.event.ChartChangeEvent;
  55. import org.jfree.chart.event.ChartChangeListener;
  56. import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
  57. import org.jfree.chart.renderer.xy.XYItemRenderer;
  58. import org.jfree.data.xy.XYDataset;
  59. import org.jfree.data.xy.XYSeries;
  60. import org.jfree.data.xy.XYSeriesCollection;
  61. import org.junit.Test;
  62. /**
  63. * Tests for the {@link CombinedRangeXYPlot} class.
  64. */
  65. public class CombinedRangeXYPlotTest implements ChartChangeListener {
  66. /** A list of the events received. */
  67. private List events = new java.util.ArrayList();
  68. /**
  69. * Receives a chart change event.
  70. *
  71. * @param event the event.
  72. */
  73. @Override
  74. public void chartChanged(ChartChangeEvent event) {
  75. this.events.add(event);
  76. }
  77. /**
  78. * Test the equals method.
  79. */
  80. @Test
  81. public void testEquals() {
  82. CombinedRangeXYPlot plot1 = createPlot();
  83. CombinedRangeXYPlot plot2 = createPlot();
  84. assertTrue(plot1.equals(plot2));
  85. assertTrue(plot2.equals(plot1));
  86. }
  87. /**
  88. * This is a test to replicate the bug report 987080.
  89. */
  90. @Test
  91. public void testRemoveSubplot() {
  92. CombinedRangeXYPlot plot = new CombinedRangeXYPlot();
  93. XYPlot plot1 = new XYPlot();
  94. XYPlot plot2 = new XYPlot();
  95. plot.add(plot1);
  96. plot.add(plot2);
  97. // remove plot2, but plot1 is removed instead
  98. plot.remove(plot2);
  99. List plots = plot.getSubplots();
  100. assertTrue(plots.get(0) == plot1);
  101. }
  102. /**
  103. * Confirm that cloning works.
  104. */
  105. @Test
  106. public void testCloning() throws CloneNotSupportedException {
  107. CombinedRangeXYPlot plot1 = createPlot();
  108. CombinedRangeXYPlot plot2 = (CombinedRangeXYPlot) plot1.clone();
  109. assertTrue(plot1 != plot2);
  110. assertTrue(plot1.getClass() == plot2.getClass());
  111. assertTrue(plot1.equals(plot2));
  112. }
  113. /**
  114. * Serialize an instance, restore it, and check for equality.
  115. */
  116. @Test
  117. public void testSerialization() {
  118. CombinedRangeXYPlot plot1 = createPlot();
  119. CombinedRangeXYPlot plot2 = (CombinedRangeXYPlot)
  120. TestUtilities.serialised(plot1);
  121. assertEquals(plot1, plot2);
  122. }
  123. /**
  124. * Check that only one chart change event is generated by a change to a
  125. * subplot.
  126. */
  127. @Test
  128. public void testNotification() {
  129. CombinedRangeXYPlot plot = createPlot();
  130. JFreeChart chart = new JFreeChart(plot);
  131. chart.addChangeListener(this);
  132. XYPlot subplot1 = (XYPlot) plot.getSubplots().get(0);
  133. NumberAxis xAxis = (NumberAxis) subplot1.getDomainAxis();
  134. xAxis.setAutoRangeIncludesZero(!xAxis.getAutoRangeIncludesZero());
  135. assertEquals(1, this.events.size());
  136. // a redraw should NOT trigger another change event
  137. BufferedImage image = new BufferedImage(200, 100,
  138. BufferedImage.TYPE_INT_RGB);
  139. Graphics2D g2 = image.createGraphics();
  140. this.events.clear();
  141. chart.draw(g2, new Rectangle2D.Double(0.0, 0.0, 200.0, 100.0));
  142. assertTrue(this.events.isEmpty());
  143. }
  144. /**
  145. * Creates a sample dataset.
  146. *
  147. * @return Series 1.
  148. */
  149. private XYDataset createDataset1() {
  150. XYSeries series1 = new XYSeries("Series 1");
  151. series1.add(10.0, 12353.3);
  152. series1.add(20.0, 13734.4);
  153. series1.add(30.0, 14525.3);
  154. series1.add(40.0, 13984.3);
  155. series1.add(50.0, 12999.4);
  156. series1.add(60.0, 14274.3);
  157. series1.add(70.0, 15943.5);
  158. series1.add(80.0, 14845.3);
  159. series1.add(90.0, 14645.4);
  160. series1.add(100.0, 16234.6);
  161. series1.add(110.0, 17232.3);
  162. series1.add(120.0, 14232.2);
  163. series1.add(130.0, 13102.2);
  164. series1.add(140.0, 14230.2);
  165. series1.add(150.0, 11235.2);
  166. XYSeries series2 = new XYSeries("Series 2");
  167. series2.add(10.0, 15000.3);
  168. series2.add(20.0, 11000.4);
  169. series2.add(30.0, 17000.3);
  170. series2.add(40.0, 15000.3);
  171. series2.add(50.0, 14000.4);
  172. series2.add(60.0, 12000.3);
  173. series2.add(70.0, 11000.5);
  174. series2.add(80.0, 12000.3);
  175. series2.add(90.0, 13000.4);
  176. series2.add(100.0, 12000.6);
  177. series2.add(110.0, 13000.3);
  178. series2.add(120.0, 17000.2);
  179. series2.add(130.0, 18000.2);
  180. series2.add(140.0, 16000.2);
  181. series2.add(150.0, 17000.2);
  182. XYSeriesCollection collection = new XYSeriesCollection();
  183. collection.addSeries(series1);
  184. collection.addSeries(series2);
  185. return collection;
  186. }
  187. /**
  188. * Creates a sample dataset.
  189. *
  190. * @return Series 2.
  191. */
  192. private XYDataset createDataset2() {
  193. // create dataset 2...
  194. XYSeries series2 = new XYSeries("Series 3");
  195. series2.add(10.0, 16853.2);
  196. series2.add(20.0, 19642.3);
  197. series2.add(30.0, 18253.5);
  198. series2.add(40.0, 15352.3);
  199. series2.add(50.0, 13532.0);
  200. series2.add(100.0, 12635.3);
  201. series2.add(110.0, 13998.2);
  202. series2.add(120.0, 11943.2);
  203. series2.add(130.0, 16943.9);
  204. series2.add(140.0, 17843.2);
  205. series2.add(150.0, 16495.3);
  206. series2.add(160.0, 17943.6);
  207. series2.add(170.0, 18500.7);
  208. series2.add(180.0, 19595.9);
  209. return new XYSeriesCollection(series2);
  210. }
  211. /**
  212. * Creates a sample plot.
  213. *
  214. * @return A sample plot.
  215. */
  216. private CombinedRangeXYPlot createPlot() {
  217. // create subplot 1...
  218. XYDataset data1 = createDataset1();
  219. XYItemRenderer renderer1 = new StandardXYItemRenderer();
  220. NumberAxis xAxis1 = new NumberAxis("X1");
  221. XYPlot subplot1 = new XYPlot(data1, xAxis1, null, renderer1);
  222. subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
  223. XYTextAnnotation annotation
  224. = new XYTextAnnotation("Hello!", 50.0, 10000.0);
  225. annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
  226. annotation.setRotationAngle(Math.PI / 4.0);
  227. subplot1.addAnnotation(annotation);
  228. // create subplot 2...
  229. XYDataset data2 = createDataset2();
  230. XYItemRenderer renderer2 = new StandardXYItemRenderer();
  231. NumberAxis xAxis2 = new NumberAxis("X2");
  232. xAxis2.setAutoRangeIncludesZero(false);
  233. XYPlot subplot2 = new XYPlot(data2, xAxis2, null, renderer2);
  234. subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
  235. // parent plot...
  236. CombinedRangeXYPlot plot = new CombinedRangeXYPlot(new NumberAxis(
  237. "Range"));
  238. plot.setGap(10.0);
  239. // add the subplots...
  240. plot.add(subplot1, 1);
  241. plot.add(subplot2, 1);
  242. plot.setOrientation(PlotOrientation.VERTICAL);
  243. return plot;
  244. }
  245. }