CombinedDomainXYPlotTest.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * CombinedDomainXYPlotTest.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 CombinedDomainXYPlot} class.
  64. */
  65. public class CombinedDomainXYPlotTest 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. * Confirm that the constructor will accept a null axis.
  79. */
  80. @Test
  81. public void testConstructor1() {
  82. CombinedDomainXYPlot plot = new CombinedDomainXYPlot(null);
  83. assertEquals(null, plot.getDomainAxis());
  84. }
  85. /**
  86. * This is a test to replicate the bug report 987080.
  87. */
  88. @Test
  89. public void testRemoveSubplot() {
  90. CombinedDomainXYPlot plot = new CombinedDomainXYPlot();
  91. XYPlot plot1 = new XYPlot();
  92. XYPlot plot2 = new XYPlot();
  93. plot.add(plot1);
  94. plot.add(plot2);
  95. // remove plot2, but plot1 is removed instead
  96. plot.remove(plot2);
  97. List plots = plot.getSubplots();
  98. assertTrue(plots.get(0) == plot1);
  99. }
  100. /**
  101. * Tests the equals() method.
  102. */
  103. @Test
  104. public void testEquals() {
  105. CombinedDomainXYPlot plot1 = createPlot();
  106. CombinedDomainXYPlot plot2 = createPlot();
  107. assertTrue(plot1.equals(plot2));
  108. assertTrue(plot2.equals(plot1));
  109. }
  110. /**
  111. * Confirm that cloning works.
  112. */
  113. @Test
  114. public void testCloning() throws CloneNotSupportedException {
  115. CombinedDomainXYPlot plot1 = createPlot();
  116. CombinedDomainXYPlot plot2 = (CombinedDomainXYPlot) plot1.clone();
  117. assertTrue(plot1 != plot2);
  118. assertTrue(plot1.getClass() == plot2.getClass());
  119. assertTrue(plot1.equals(plot2));
  120. }
  121. /**
  122. * Serialize an instance, restore it, and check for equality.
  123. */
  124. @Test
  125. public void testSerialization() {
  126. CombinedDomainXYPlot plot1 = createPlot();
  127. CombinedDomainXYPlot plot2 = (CombinedDomainXYPlot)
  128. TestUtilities.serialised(plot1);
  129. assertEquals(plot1, plot2);
  130. }
  131. /**
  132. * Check that only one chart change event is generated by a change to a
  133. * subplot.
  134. */
  135. @Test
  136. public void testNotification() {
  137. CombinedDomainXYPlot plot = createPlot();
  138. JFreeChart chart = new JFreeChart(plot);
  139. chart.addChangeListener(this);
  140. XYPlot subplot1 = (XYPlot) plot.getSubplots().get(0);
  141. NumberAxis yAxis = (NumberAxis) subplot1.getRangeAxis();
  142. yAxis.setAutoRangeIncludesZero(!yAxis.getAutoRangeIncludesZero());
  143. assertEquals(1, this.events.size());
  144. // a redraw should NOT trigger another change event
  145. BufferedImage image = new BufferedImage(200, 100,
  146. BufferedImage.TYPE_INT_RGB);
  147. Graphics2D g2 = image.createGraphics();
  148. this.events.clear();
  149. chart.draw(g2, new Rectangle2D.Double(0.0, 0.0, 200.0, 100.0));
  150. assertTrue(this.events.isEmpty());
  151. }
  152. /**
  153. * Creates a sample dataset.
  154. *
  155. * @return Series 1.
  156. */
  157. private XYDataset createDataset1() {
  158. // create dataset 1...
  159. XYSeries series1 = new XYSeries("Series 1");
  160. series1.add(10.0, 12353.3);
  161. series1.add(20.0, 13734.4);
  162. series1.add(30.0, 14525.3);
  163. series1.add(40.0, 13984.3);
  164. series1.add(50.0, 12999.4);
  165. series1.add(60.0, 14274.3);
  166. series1.add(70.0, 15943.5);
  167. series1.add(80.0, 14845.3);
  168. series1.add(90.0, 14645.4);
  169. series1.add(100.0, 16234.6);
  170. series1.add(110.0, 17232.3);
  171. series1.add(120.0, 14232.2);
  172. series1.add(130.0, 13102.2);
  173. series1.add(140.0, 14230.2);
  174. series1.add(150.0, 11235.2);
  175. XYSeries series2 = new XYSeries("Series 2");
  176. series2.add(10.0, 15000.3);
  177. series2.add(20.0, 11000.4);
  178. series2.add(30.0, 17000.3);
  179. series2.add(40.0, 15000.3);
  180. series2.add(50.0, 14000.4);
  181. series2.add(60.0, 12000.3);
  182. series2.add(70.0, 11000.5);
  183. series2.add(80.0, 12000.3);
  184. series2.add(90.0, 13000.4);
  185. series2.add(100.0, 12000.6);
  186. series2.add(110.0, 13000.3);
  187. series2.add(120.0, 17000.2);
  188. series2.add(130.0, 18000.2);
  189. series2.add(140.0, 16000.2);
  190. series2.add(150.0, 17000.2);
  191. XYSeriesCollection collection = new XYSeriesCollection();
  192. collection.addSeries(series1);
  193. collection.addSeries(series2);
  194. return collection;
  195. }
  196. /**
  197. * Creates a sample dataset.
  198. *
  199. * @return Series 2.
  200. */
  201. private XYDataset createDataset2() {
  202. XYSeries series2 = new XYSeries("Series 3");
  203. series2.add(10.0, 16853.2);
  204. series2.add(20.0, 19642.3);
  205. series2.add(30.0, 18253.5);
  206. series2.add(40.0, 15352.3);
  207. series2.add(50.0, 13532.0);
  208. series2.add(100.0, 12635.3);
  209. series2.add(110.0, 13998.2);
  210. series2.add(120.0, 11943.2);
  211. series2.add(130.0, 16943.9);
  212. series2.add(140.0, 17843.2);
  213. series2.add(150.0, 16495.3);
  214. series2.add(160.0, 17943.6);
  215. series2.add(170.0, 18500.7);
  216. series2.add(180.0, 19595.9);
  217. return new XYSeriesCollection(series2);
  218. }
  219. /**
  220. * Creates a sample plot.
  221. *
  222. * @return A sample plot.
  223. */
  224. private CombinedDomainXYPlot createPlot() {
  225. // create subplot 1...
  226. XYDataset data1 = createDataset1();
  227. XYItemRenderer renderer1 = new StandardXYItemRenderer();
  228. NumberAxis rangeAxis1 = new NumberAxis("Range 1");
  229. XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
  230. subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
  231. XYTextAnnotation annotation = new XYTextAnnotation("Hello!", 50.0,
  232. 10000.0);
  233. annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
  234. annotation.setRotationAngle(Math.PI / 4.0);
  235. subplot1.addAnnotation(annotation);
  236. // create subplot 2...
  237. XYDataset data2 = createDataset2();
  238. XYItemRenderer renderer2 = new StandardXYItemRenderer();
  239. NumberAxis rangeAxis2 = new NumberAxis("Range 2");
  240. rangeAxis2.setAutoRangeIncludesZero(false);
  241. XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2);
  242. subplot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
  243. // parent plot...
  244. CombinedDomainXYPlot plot = new CombinedDomainXYPlot(
  245. new NumberAxis("Domain"));
  246. plot.setGap(10.0);
  247. // add the subplots...
  248. plot.add(subplot1, 1);
  249. plot.add(subplot2, 1);
  250. plot.setOrientation(PlotOrientation.VERTICAL);
  251. return plot;
  252. }
  253. }