XYPlotTest.java 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2014, 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. * XYPlotTest.java
  29. * ---------------
  30. * (C) Copyright 2003-2014, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 26-Mar-2003 : Version 1 (DG);
  38. * 22-Mar-2004 : Added new cloning test (DG);
  39. * 05-Oct-2004 : Strengthened test for clone independence (DG);
  40. * 22-Nov-2006 : Added quadrant fields to equals() and clone() tests (DG);
  41. * 09-Jan-2007 : Mark and comment out testGetDatasetCount() (DG);
  42. * 05-Feb-2007 : Added testAddDomainMarker() and testAddRangeMarker() (DG);
  43. * 07-Feb-2007 : Added test1654215() (DG);
  44. * 24-May-2007 : Added testDrawSeriesWithZeroItems() (DG);
  45. * 07-Apr-2008 : Added testRemoveDomainMarker() and
  46. * testRemoveRangeMarker() (DG);
  47. * 10-May-2009 : Extended testEquals(), added testCloning3() (DG);
  48. * 06-Jul-2009 : Added testBug2817504() (DG);
  49. *
  50. */
  51. package org.jfree.chart.plot;
  52. import static org.junit.Assert.assertEquals;
  53. import static org.junit.Assert.assertFalse;
  54. import static org.junit.Assert.assertTrue;
  55. import static org.junit.Assert.fail;
  56. import static org.junit.Assert.assertNotEquals;
  57. import org.junit.Test;
  58. import java.awt.BasicStroke;
  59. import java.awt.Color;
  60. import java.awt.GradientPaint;
  61. import java.awt.Graphics2D;
  62. import java.awt.Stroke;
  63. import java.awt.geom.Point2D;
  64. import java.awt.geom.Rectangle2D;
  65. import java.awt.image.BufferedImage;
  66. import java.util.Arrays;
  67. import java.util.List;
  68. import org.jfree.chart.ChartFactory;
  69. import org.jfree.chart.JFreeChart;
  70. import org.jfree.chart.LegendItem;
  71. import org.jfree.chart.LegendItemCollection;
  72. import org.jfree.chart.TestUtilities;
  73. import org.jfree.chart.annotations.XYTextAnnotation;
  74. import org.jfree.chart.axis.AxisLocation;
  75. import org.jfree.chart.axis.DateAxis;
  76. import org.jfree.chart.axis.NumberAxis;
  77. import org.jfree.chart.event.MarkerChangeListener;
  78. import org.jfree.chart.labels.StandardXYToolTipGenerator;
  79. import org.jfree.chart.renderer.xy.DefaultXYItemRenderer;
  80. import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
  81. import org.jfree.chart.renderer.xy.XYBarRenderer;
  82. import org.jfree.chart.renderer.xy.XYItemRenderer;
  83. import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
  84. import org.jfree.chart.util.DefaultShadowGenerator;
  85. import org.jfree.data.time.Day;
  86. import org.jfree.data.time.TimeSeries;
  87. import org.jfree.data.time.TimeSeriesCollection;
  88. import org.jfree.data.xy.DefaultXYDataset;
  89. import org.jfree.data.xy.IntervalXYDataset;
  90. import org.jfree.data.xy.XYDataset;
  91. import org.jfree.data.xy.XYSeries;
  92. import org.jfree.data.xy.XYSeriesCollection;
  93. import org.jfree.date.MonthConstants;
  94. import org.jfree.ui.Layer;
  95. import org.jfree.ui.RectangleInsets;
  96. /**
  97. * Tests for the {@link XYPlot} class.
  98. */
  99. public class XYPlotTest {
  100. /**
  101. * Added this test in response to a bug report.
  102. */
  103. public void testGetDatasetCount() {
  104. XYPlot plot = new XYPlot();
  105. assertEquals(0, plot.getDatasetCount());
  106. }
  107. /**
  108. * Some checks for the equals() method.
  109. */
  110. @Test
  111. public void testEquals() {
  112. XYPlot plot1 = new XYPlot();
  113. XYPlot plot2 = new XYPlot();
  114. assertTrue(plot1.equals(plot2));
  115. // orientation...
  116. plot1.setOrientation(PlotOrientation.HORIZONTAL);
  117. assertFalse(plot1.equals(plot2));
  118. plot2.setOrientation(PlotOrientation.HORIZONTAL);
  119. assertTrue(plot1.equals(plot2));
  120. // axisOffset...
  121. plot1.setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05, 0.05));
  122. assertFalse(plot1.equals(plot2));
  123. plot2.setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05, 0.05));
  124. assertTrue(plot1.equals(plot2));
  125. // domainAxis...
  126. plot1.setDomainAxis(new NumberAxis("Domain Axis"));
  127. assertFalse(plot1.equals(plot2));
  128. plot2.setDomainAxis(new NumberAxis("Domain Axis"));
  129. assertTrue(plot1.equals(plot2));
  130. // domainAxisLocation...
  131. plot1.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
  132. assertFalse(plot1.equals(plot2));
  133. plot2.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
  134. assertTrue(plot1.equals(plot2));
  135. // secondary DomainAxes...
  136. plot1.setDomainAxis(11, new NumberAxis("Secondary Domain Axis"));
  137. assertFalse(plot1.equals(plot2));
  138. plot2.setDomainAxis(11, new NumberAxis("Secondary Domain Axis"));
  139. assertTrue(plot1.equals(plot2));
  140. // secondary DomainAxisLocations...
  141. plot1.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
  142. assertFalse(plot1.equals(plot2));
  143. plot2.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
  144. assertTrue(plot1.equals(plot2));
  145. // rangeAxis...
  146. plot1.setRangeAxis(new NumberAxis("Range Axis"));
  147. assertFalse(plot1.equals(plot2));
  148. plot2.setRangeAxis(new NumberAxis("Range Axis"));
  149. assertTrue(plot1.equals(plot2));
  150. // rangeAxisLocation...
  151. plot1.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
  152. assertFalse(plot1.equals(plot2));
  153. plot2.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
  154. assertTrue(plot1.equals(plot2));
  155. // secondary RangeAxes...
  156. plot1.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
  157. assertFalse(plot1.equals(plot2));
  158. plot2.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
  159. assertTrue(plot1.equals(plot2));
  160. // secondary RangeAxisLocations...
  161. plot1.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
  162. assertFalse(plot1.equals(plot2));
  163. plot2.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
  164. assertTrue(plot1.equals(plot2));
  165. // secondary DatasetDomainAxisMap...
  166. plot1.mapDatasetToDomainAxis(11, 11);
  167. assertFalse(plot1.equals(plot2));
  168. plot2.mapDatasetToDomainAxis(11, 11);
  169. assertTrue(plot1.equals(plot2));
  170. // secondaryDatasetRangeAxisMap...
  171. plot1.mapDatasetToRangeAxis(11, 11);
  172. assertFalse(plot1.equals(plot2));
  173. plot2.mapDatasetToRangeAxis(11, 11);
  174. assertTrue(plot1.equals(plot2));
  175. // renderer
  176. plot1.setRenderer(new DefaultXYItemRenderer());
  177. assertFalse(plot1.equals(plot2));
  178. plot2.setRenderer(new DefaultXYItemRenderer());
  179. assertTrue(plot1.equals(plot2));
  180. // secondary renderers
  181. plot1.setRenderer(11, new DefaultXYItemRenderer());
  182. assertFalse(plot1.equals(plot2));
  183. plot2.setRenderer(11, new DefaultXYItemRenderer());
  184. assertTrue(plot1.equals(plot2));
  185. // domainGridlinesVisible
  186. plot1.setDomainGridlinesVisible(false);
  187. assertFalse(plot1.equals(plot2));
  188. plot2.setDomainGridlinesVisible(false);
  189. assertTrue(plot1.equals(plot2));
  190. // domainGridlineStroke
  191. Stroke stroke = new BasicStroke(2.0f);
  192. plot1.setDomainGridlineStroke(stroke);
  193. assertFalse(plot1.equals(plot2));
  194. plot2.setDomainGridlineStroke(stroke);
  195. assertTrue(plot1.equals(plot2));
  196. // domainGridlinePaint
  197. plot1.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
  198. 3.0f, 4.0f, Color.red));
  199. assertFalse(plot1.equals(plot2));
  200. plot2.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
  201. 3.0f, 4.0f, Color.red));
  202. assertTrue(plot1.equals(plot2));
  203. // rangeGridlinesVisible
  204. plot1.setRangeGridlinesVisible(false);
  205. assertFalse(plot1.equals(plot2));
  206. plot2.setRangeGridlinesVisible(false);
  207. assertTrue(plot1.equals(plot2));
  208. // rangeGridlineStroke
  209. plot1.setRangeGridlineStroke(stroke);
  210. assertFalse(plot1.equals(plot2));
  211. plot2.setRangeGridlineStroke(stroke);
  212. assertTrue(plot1.equals(plot2));
  213. // rangeGridlinePaint
  214. plot1.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.green,
  215. 3.0f, 4.0f, Color.red));
  216. assertFalse(plot1.equals(plot2));
  217. plot2.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.green,
  218. 3.0f, 4.0f, Color.red));
  219. assertTrue(plot1.equals(plot2));
  220. // rangeZeroBaselineVisible
  221. plot1.setRangeZeroBaselineVisible(true);
  222. assertFalse(plot1.equals(plot2));
  223. plot2.setRangeZeroBaselineVisible(true);
  224. assertTrue(plot1.equals(plot2));
  225. // rangeZeroBaselineStroke
  226. plot1.setRangeZeroBaselineStroke(stroke);
  227. assertFalse(plot1.equals(plot2));
  228. plot2.setRangeZeroBaselineStroke(stroke);
  229. assertTrue(plot1.equals(plot2));
  230. // rangeZeroBaselinePaint
  231. plot1.setRangeZeroBaselinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
  232. 3.0f, 4.0f, Color.red));
  233. assertFalse(plot1.equals(plot2));
  234. plot2.setRangeZeroBaselinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
  235. 3.0f, 4.0f, Color.red));
  236. assertTrue(plot1.equals(plot2));
  237. // rangeCrosshairVisible
  238. plot1.setRangeCrosshairVisible(true);
  239. assertFalse(plot1.equals(plot2));
  240. plot2.setRangeCrosshairVisible(true);
  241. assertTrue(plot1.equals(plot2));
  242. // rangeCrosshairValue
  243. plot1.setRangeCrosshairValue(100.0);
  244. assertFalse(plot1.equals(plot2));
  245. plot2.setRangeCrosshairValue(100.0);
  246. assertTrue(plot1.equals(plot2));
  247. // rangeCrosshairStroke
  248. plot1.setRangeCrosshairStroke(stroke);
  249. assertFalse(plot1.equals(plot2));
  250. plot2.setRangeCrosshairStroke(stroke);
  251. assertTrue(plot1.equals(plot2));
  252. // rangeCrosshairPaint
  253. plot1.setRangeCrosshairPaint(new GradientPaint(1.0f, 2.0f, Color.pink,
  254. 3.0f, 4.0f, Color.red));
  255. assertFalse(plot1.equals(plot2));
  256. plot2.setRangeCrosshairPaint(new GradientPaint(1.0f, 2.0f, Color.pink,
  257. 3.0f, 4.0f, Color.red));
  258. assertTrue(plot1.equals(plot2));
  259. // rangeCrosshairLockedOnData
  260. plot1.setRangeCrosshairLockedOnData(false);
  261. assertFalse(plot1.equals(plot2));
  262. plot2.setRangeCrosshairLockedOnData(false);
  263. assertTrue(plot1.equals(plot2));
  264. // range markers
  265. plot1.addRangeMarker(new ValueMarker(4.0));
  266. assertFalse(plot1.equals(plot2));
  267. plot2.addRangeMarker(new ValueMarker(4.0));
  268. assertTrue(plot1.equals(plot2));
  269. // secondary range markers
  270. plot1.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
  271. assertFalse(plot1.equals(plot2));
  272. plot2.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
  273. assertTrue(plot1.equals(plot2));
  274. plot1.addRangeMarker(1, new ValueMarker(99.0), Layer.BACKGROUND);
  275. assertFalse(plot1.equals(plot2));
  276. plot2.addRangeMarker(1, new ValueMarker(99.0), Layer.BACKGROUND);
  277. assertTrue(plot1.equals(plot2));
  278. // fixed legend items
  279. plot1.setFixedLegendItems(new LegendItemCollection());
  280. assertFalse(plot1.equals(plot2));
  281. plot2.setFixedLegendItems(new LegendItemCollection());
  282. assertTrue(plot1.equals(plot2));
  283. // weight
  284. plot1.setWeight(3);
  285. assertFalse(plot1.equals(plot2));
  286. plot2.setWeight(3);
  287. assertTrue(plot1.equals(plot2));
  288. // quadrant origin
  289. plot1.setQuadrantOrigin(new Point2D.Double(12.3, 45.6));
  290. assertFalse(plot1.equals(plot2));
  291. plot2.setQuadrantOrigin(new Point2D.Double(12.3, 45.6));
  292. assertTrue(plot1.equals(plot2));
  293. // quadrant paint
  294. plot1.setQuadrantPaint(0, new GradientPaint(1.0f, 2.0f, Color.red,
  295. 3.0f, 4.0f, Color.blue));
  296. assertFalse(plot1.equals(plot2));
  297. plot2.setQuadrantPaint(0, new GradientPaint(1.0f, 2.0f, Color.red,
  298. 3.0f, 4.0f, Color.blue));
  299. assertTrue(plot1.equals(plot2));
  300. plot1.setQuadrantPaint(1, new GradientPaint(2.0f, 3.0f, Color.red,
  301. 4.0f, 5.0f, Color.blue));
  302. assertFalse(plot1.equals(plot2));
  303. plot2.setQuadrantPaint(1, new GradientPaint(2.0f, 3.0f, Color.red,
  304. 4.0f, 5.0f, Color.blue));
  305. assertTrue(plot1.equals(plot2));
  306. plot1.setQuadrantPaint(2, new GradientPaint(3.0f, 4.0f, Color.red,
  307. 5.0f, 6.0f, Color.blue));
  308. assertFalse(plot1.equals(plot2));
  309. plot2.setQuadrantPaint(2, new GradientPaint(3.0f, 4.0f, Color.red,
  310. 5.0f, 6.0f, Color.blue));
  311. assertTrue(plot1.equals(plot2));
  312. plot1.setQuadrantPaint(3, new GradientPaint(4.0f, 5.0f, Color.red,
  313. 6.0f, 7.0f, Color.blue));
  314. assertFalse(plot1.equals(plot2));
  315. plot2.setQuadrantPaint(3, new GradientPaint(4.0f, 5.0f, Color.red,
  316. 6.0f, 7.0f, Color.blue));
  317. assertTrue(plot1.equals(plot2));
  318. plot1.setDomainTickBandPaint(Color.red);
  319. assertFalse(plot1.equals(plot2));
  320. plot2.setDomainTickBandPaint(Color.red);
  321. assertTrue(plot1.equals(plot2));
  322. plot1.setRangeTickBandPaint(Color.blue);
  323. assertFalse(plot1.equals(plot2));
  324. plot2.setRangeTickBandPaint(Color.blue);
  325. assertTrue(plot1.equals(plot2));
  326. plot1.setDomainMinorGridlinesVisible(true);
  327. assertFalse(plot1.equals(plot2));
  328. plot2.setDomainMinorGridlinesVisible(true);
  329. assertTrue(plot1.equals(plot2));
  330. plot1.setDomainMinorGridlinePaint(Color.red);
  331. assertFalse(plot1.equals(plot2));
  332. plot2.setDomainMinorGridlinePaint(Color.red);
  333. assertTrue(plot1.equals(plot2));
  334. plot1.setDomainGridlineStroke(new BasicStroke(1.1f));
  335. assertFalse(plot1.equals(plot2));
  336. plot2.setDomainGridlineStroke(new BasicStroke(1.1f));
  337. assertTrue(plot1.equals(plot2));
  338. plot1.setRangeMinorGridlinesVisible(true);
  339. assertFalse(plot1.equals(plot2));
  340. plot2.setRangeMinorGridlinesVisible(true);
  341. assertTrue(plot1.equals(plot2));
  342. plot1.setRangeMinorGridlinePaint(Color.blue);
  343. assertFalse(plot1.equals(plot2));
  344. plot2.setRangeMinorGridlinePaint(Color.blue);
  345. assertTrue(plot1.equals(plot2));
  346. plot1.setRangeMinorGridlineStroke(new BasicStroke(1.23f));
  347. assertFalse(plot1.equals(plot2));
  348. plot2.setRangeMinorGridlineStroke(new BasicStroke(1.23f));
  349. assertTrue(plot1.equals(plot2));
  350. List axisIndices = Arrays.asList(new Integer[] {new Integer(0),
  351. new Integer(1)});
  352. plot1.mapDatasetToDomainAxes(0, axisIndices);
  353. assertFalse(plot1.equals(plot2));
  354. plot2.mapDatasetToDomainAxes(0, axisIndices);
  355. assertTrue(plot1.equals(plot2));
  356. plot1.mapDatasetToRangeAxes(0, axisIndices);
  357. assertFalse(plot1.equals(plot2));
  358. plot2.mapDatasetToRangeAxes(0, axisIndices);
  359. assertTrue(plot1.equals(plot2));
  360. // shadowGenerator
  361. plot1.setShadowGenerator(new DefaultShadowGenerator(5, Color.gray,
  362. 0.6f, 4, -Math.PI / 4));
  363. assertFalse(plot1.equals(plot2));
  364. plot2.setShadowGenerator(new DefaultShadowGenerator(5, Color.gray,
  365. 0.6f, 4, -Math.PI / 4));
  366. assertTrue(plot1.equals(plot2));
  367. plot1.setShadowGenerator(null);
  368. assertFalse(plot1.equals(plot2));
  369. plot2.setShadowGenerator(null);
  370. assertTrue(plot1.equals(plot2));
  371. LegendItemCollection lic1 = new LegendItemCollection();
  372. lic1.add(new LegendItem("XYZ", Color.red));
  373. plot1.setFixedLegendItems(lic1);
  374. assertFalse(plot1.equals(plot2));
  375. LegendItemCollection lic2 = new LegendItemCollection();
  376. lic2.add(new LegendItem("XYZ", Color.red));
  377. plot2.setFixedLegendItems(lic2);
  378. assertTrue(plot1.equals(plot2));
  379. }
  380. /**
  381. * This test covers a flaw in the ObjectList equals() method.
  382. */
  383. @Test
  384. public void testEquals_ObjectList() {
  385. XYPlot p1 = new XYPlot();
  386. p1.setDomainAxis(new NumberAxis("A"));
  387. XYPlot p2 = new XYPlot();
  388. p2.setDomainAxis(new NumberAxis("A"));
  389. assertEquals(p1, p2);
  390. p2.setDomainAxis(1, new NumberAxis("B"));
  391. assertNotEquals(p1, p2);
  392. }
  393. /**
  394. * This test covers a flaw in the ObjectList equals() method.
  395. */
  396. @Test
  397. public void testEquals_ObjectList2() {
  398. XYPlot p1 = new XYPlot();
  399. p1.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
  400. XYPlot p2 = new XYPlot();
  401. p2.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
  402. assertEquals(p1, p2);
  403. p2.setDomainAxisLocation(1, AxisLocation.TOP_OR_LEFT);
  404. assertNotEquals(p1, p2);
  405. }
  406. /**
  407. * This test covers a flaw in the ObjectList equals() method.
  408. */
  409. @Test
  410. public void testEquals_ObjectList3() {
  411. XYPlot p1 = new XYPlot();
  412. p1.setRangeAxis(new NumberAxis("A"));
  413. XYPlot p2 = new XYPlot();
  414. p2.setRangeAxis(new NumberAxis("A"));
  415. assertEquals(p1, p2);
  416. p2.setRangeAxis(1, new NumberAxis("B"));
  417. assertNotEquals(p1, p2);
  418. }
  419. /**
  420. * This test covers a flaw in the ObjectList equals() method.
  421. */
  422. @Test
  423. public void testEquals_ObjectList4() {
  424. XYPlot p1 = new XYPlot();
  425. p1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
  426. XYPlot p2 = new XYPlot();
  427. p2.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
  428. assertEquals(p1, p2);
  429. p2.setRangeAxisLocation(1, AxisLocation.TOP_OR_LEFT);
  430. assertNotEquals(p1, p2);
  431. }
  432. /**
  433. * This test covers a flaw in the ObjectList equals() method.
  434. */
  435. @Test
  436. public void testEquals_ObjectList5() {
  437. XYPlot p1 = new XYPlot();
  438. p1.setRenderer(new XYBarRenderer());
  439. XYPlot p2 = new XYPlot();
  440. p2.setRenderer(new XYBarRenderer());
  441. assertEquals(p1, p2);
  442. p2.setRenderer(1, new XYLineAndShapeRenderer());
  443. assertNotEquals(p1, p2);
  444. }
  445. /**
  446. * Confirm that basic cloning works.
  447. */
  448. @Test
  449. public void testCloning() throws CloneNotSupportedException {
  450. XYPlot p1 = new XYPlot();
  451. XYPlot p2 = (XYPlot) p1.clone();
  452. assertTrue(p1 != p2);
  453. assertTrue(p1.getClass() == p2.getClass());
  454. assertTrue(p1.equals(p2));
  455. }
  456. /**
  457. * Tests cloning for a more complex plot.
  458. */
  459. @Test
  460. public void testCloning2() throws CloneNotSupportedException {
  461. XYPlot p1 = new XYPlot(null, new NumberAxis("Domain Axis"),
  462. new NumberAxis("Range Axis"), new StandardXYItemRenderer());
  463. p1.setRangeAxis(1, new NumberAxis("Range Axis 2"));
  464. List axisIndices = Arrays.asList(new Integer[] {new Integer(0),
  465. new Integer(1)});
  466. p1.mapDatasetToDomainAxes(0, axisIndices);
  467. p1.mapDatasetToRangeAxes(0, axisIndices);
  468. p1.setRenderer(1, new XYBarRenderer());
  469. XYPlot p2 = (XYPlot) p1.clone();
  470. assertTrue(p1 != p2);
  471. assertTrue(p1.getClass() == p2.getClass());
  472. assertTrue(p1.equals(p2));
  473. }
  474. /**
  475. * Tests cloning for a plot where the fixed legend items have been
  476. * specified.
  477. */
  478. @Test
  479. public void testCloning3() throws CloneNotSupportedException {
  480. XYPlot p1 = new XYPlot(null, new NumberAxis("Domain Axis"),
  481. new NumberAxis("Range Axis"), new StandardXYItemRenderer());
  482. LegendItemCollection c1 = new LegendItemCollection();
  483. p1.setFixedLegendItems(c1);
  484. XYPlot p2 = (XYPlot) p1.clone();
  485. assertTrue(p1 != p2);
  486. assertTrue(p1.getClass() == p2.getClass());
  487. assertTrue(p1.equals(p2));
  488. // verify independence of fixed legend item collection
  489. c1.add(new LegendItem("X"));
  490. assertFalse(p1.equals(p2));
  491. }
  492. /**
  493. * Tests cloning to ensure that the cloned plot is registered as a listener
  494. * on the cloned renderer.
  495. */
  496. @Test
  497. public void testCloning4() throws CloneNotSupportedException {
  498. XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
  499. XYPlot p1 = new XYPlot(null, new NumberAxis("Domain Axis"),
  500. new NumberAxis("Range Axis"), r1);
  501. XYPlot p2 = (XYPlot) p1.clone();
  502. assertTrue(p1 != p2);
  503. assertTrue(p1.getClass() == p2.getClass());
  504. assertTrue(p1.equals(p2));
  505. // verify that the plot is listening to the cloned renderer
  506. XYLineAndShapeRenderer r2 = (XYLineAndShapeRenderer) p2.getRenderer();
  507. assertTrue(r2.hasListener(p2));
  508. }
  509. /**
  510. * Confirm that cloning captures the quadrantOrigin field.
  511. */
  512. @Test
  513. public void testCloning_QuadrantOrigin() throws CloneNotSupportedException {
  514. XYPlot p1 = new XYPlot();
  515. Point2D p = new Point2D.Double(1.2, 3.4);
  516. p1.setQuadrantOrigin(p);
  517. XYPlot p2 = (XYPlot) p1.clone();
  518. assertTrue(p1 != p2);
  519. assertTrue(p1.getClass() == p2.getClass());
  520. assertTrue(p1.equals(p2));
  521. assertTrue(p2.getQuadrantOrigin() != p);
  522. }
  523. /**
  524. * Confirm that cloning captures the quadrantOrigin field.
  525. */
  526. @Test
  527. public void testCloning_QuadrantPaint() throws CloneNotSupportedException {
  528. XYPlot p1 = new XYPlot();
  529. p1.setQuadrantPaint(3, new GradientPaint(1.0f, 2.0f, Color.red,
  530. 3.0f, 4.0f, Color.blue));
  531. XYPlot p2 = (XYPlot) p1.clone();
  532. assertTrue(p1 != p2);
  533. assertTrue(p1.getClass() == p2.getClass());
  534. assertTrue(p1.equals(p2));
  535. // check for independence
  536. p1.setQuadrantPaint(1, Color.red);
  537. assertFalse(p1.equals(p2));
  538. p2.setQuadrantPaint(1, Color.red);
  539. assertTrue(p1.equals(p2));
  540. }
  541. /**
  542. * Renderers that belong to the plot are being cloned but they are
  543. * retaining a reference to the original plot.
  544. */
  545. @Test
  546. public void testBug2817504() throws CloneNotSupportedException {
  547. XYPlot p1 = new XYPlot();
  548. XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer();
  549. p1.setRenderer(r1);
  550. XYPlot p2 = (XYPlot) p1.clone();
  551. assertTrue(p1 != p2);
  552. assertTrue(p1.getClass() == p2.getClass());
  553. assertTrue(p1.equals(p2));
  554. // check for independence
  555. XYLineAndShapeRenderer r2 = (XYLineAndShapeRenderer) p2.getRenderer();
  556. assertTrue(r2.getPlot() == p2);
  557. }
  558. /**
  559. * Tests the independence of the clones.
  560. */
  561. @Test
  562. public void testCloneIndependence() throws CloneNotSupportedException {
  563. XYPlot p1 = new XYPlot(null, new NumberAxis("Domain Axis"),
  564. new NumberAxis("Range Axis"), new StandardXYItemRenderer());
  565. p1.setDomainAxis(1, new NumberAxis("Domain Axis 2"));
  566. p1.setDomainAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
  567. p1.setRangeAxis(1, new NumberAxis("Range Axis 2"));
  568. p1.setRangeAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
  569. p1.setRenderer(1, new XYBarRenderer());
  570. XYPlot p2 = (XYPlot) p1.clone();
  571. assertTrue(p1.equals(p2));
  572. p1.getDomainAxis().setLabel("Label");
  573. assertFalse(p1.equals(p2));
  574. p2.getDomainAxis().setLabel("Label");
  575. assertTrue(p1.equals(p2));
  576. p1.getDomainAxis(1).setLabel("S1");
  577. assertFalse(p1.equals(p2));
  578. p2.getDomainAxis(1).setLabel("S1");
  579. assertTrue(p1.equals(p2));
  580. p1.setDomainAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
  581. assertFalse(p1.equals(p2));
  582. p2.setDomainAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
  583. assertTrue(p1.equals(p2));
  584. p1.mapDatasetToDomainAxis(2, 1);
  585. assertFalse(p1.equals(p2));
  586. p2.mapDatasetToDomainAxis(2, 1);
  587. assertTrue(p1.equals(p2));
  588. p1.getRangeAxis().setLabel("Label");
  589. assertFalse(p1.equals(p2));
  590. p2.getRangeAxis().setLabel("Label");
  591. assertTrue(p1.equals(p2));
  592. p1.getRangeAxis(1).setLabel("S1");
  593. assertFalse(p1.equals(p2));
  594. p2.getRangeAxis(1).setLabel("S1");
  595. assertTrue(p1.equals(p2));
  596. p1.setRangeAxisLocation(1, AxisLocation.TOP_OR_LEFT);
  597. assertFalse(p1.equals(p2));
  598. p2.setRangeAxisLocation(1, AxisLocation.TOP_OR_LEFT);
  599. assertTrue(p1.equals(p2));
  600. p1.mapDatasetToRangeAxis(2, 1);
  601. assertFalse(p1.equals(p2));
  602. p2.mapDatasetToRangeAxis(2, 1);
  603. assertTrue(p1.equals(p2));
  604. p1.getRenderer().setOutlinePaint(Color.cyan);
  605. assertFalse(p1.equals(p2));
  606. p2.getRenderer().setOutlinePaint(Color.cyan);
  607. assertTrue(p1.equals(p2));
  608. p1.getRenderer(1).setOutlinePaint(Color.red);
  609. assertFalse(p1.equals(p2));
  610. p2.getRenderer(1).setOutlinePaint(Color.red);
  611. assertTrue(p1.equals(p2));
  612. }
  613. /**
  614. * Setting a null renderer should be allowed, but is generating a null
  615. * pointer exception in 0.9.7.
  616. */
  617. @Test
  618. public void testSetNullRenderer() {
  619. boolean failed = false;
  620. try {
  621. XYPlot plot = new XYPlot(null, new NumberAxis("X"),
  622. new NumberAxis("Y"), null);
  623. plot.setRenderer(null);
  624. }
  625. catch (Exception e) {
  626. failed = true;
  627. }
  628. assertTrue(!failed);
  629. }
  630. /**
  631. * Serialize an instance, restore it, and check for equality.
  632. */
  633. @Test
  634. public void testSerialization1() {
  635. XYDataset data = new XYSeriesCollection();
  636. NumberAxis domainAxis = new NumberAxis("Domain");
  637. NumberAxis rangeAxis = new NumberAxis("Range");
  638. StandardXYItemRenderer renderer = new StandardXYItemRenderer();
  639. XYPlot p1 = new XYPlot(data, domainAxis, rangeAxis, renderer);
  640. XYPlot p2 = (XYPlot) TestUtilities.serialised(p1);
  641. assertEquals(p1, p2);
  642. }
  643. /**
  644. * Serialize an instance, restore it, and check for equality. This test
  645. * uses a {@link DateAxis} and a {@link StandardXYToolTipGenerator}.
  646. */
  647. @Test
  648. public void testSerialization2() {
  649. IntervalXYDataset data1 = createDataset1();
  650. XYItemRenderer renderer1 = new XYBarRenderer(0.20);
  651. renderer1.setBaseToolTipGenerator(
  652. StandardXYToolTipGenerator.getTimeSeriesInstance());
  653. XYPlot p1 = new XYPlot(data1, new DateAxis("Date"), null, renderer1);
  654. XYPlot p2 = (XYPlot) TestUtilities.serialised(p1);
  655. assertEquals(p1, p2);
  656. }
  657. /**
  658. * Problem to reproduce a bug in serialization. The bug (first reported
  659. * against the {@link org.jfree.chart.plot.CategoryPlot} class) is a null
  660. * pointer exception that occurs when drawing a plot after deserialization.
  661. * It is caused by four temporary storage structures (axesAtTop,
  662. * axesAtBottom, axesAtLeft and axesAtRight - all initialized as empty
  663. * lists in the constructor) not being initialized by the readObject()
  664. * method following deserialization. This test has been written to
  665. * reproduce the bug (now fixed).
  666. */
  667. @Test
  668. public void testSerialization3() {
  669. XYSeriesCollection dataset = new XYSeriesCollection();
  670. JFreeChart chart = ChartFactory.createXYLineChart("Test Chart",
  671. "Domain Axis", "Range Axis", dataset);
  672. JFreeChart chart2 = (JFreeChart) TestUtilities.serialised(chart);
  673. assertEquals(chart, chart2);
  674. try {
  675. chart2.createBufferedImage(300, 200);
  676. }
  677. catch (Exception e) {
  678. fail("No exception should be thrown.");
  679. }
  680. }
  681. /**
  682. * A test to reproduce a bug in serialization: the domain and/or range
  683. * markers for a plot are not being serialized.
  684. */
  685. @Test
  686. public void testSerialization4() {
  687. XYSeriesCollection dataset = new XYSeriesCollection();
  688. JFreeChart chart = ChartFactory.createXYLineChart("Test Chart",
  689. "Domain Axis", "Range Axis", dataset);
  690. XYPlot plot = (XYPlot) chart.getPlot();
  691. plot.addDomainMarker(new ValueMarker(1.0), Layer.FOREGROUND);
  692. plot.addDomainMarker(new IntervalMarker(2.0, 3.0), Layer.BACKGROUND);
  693. plot.addRangeMarker(new ValueMarker(4.0), Layer.FOREGROUND);
  694. plot.addRangeMarker(new IntervalMarker(5.0, 6.0), Layer.BACKGROUND);
  695. JFreeChart chart2 = (JFreeChart) TestUtilities.serialised(chart);
  696. assertEquals(chart, chart2);
  697. try {
  698. chart2.createBufferedImage(300, 200);
  699. }
  700. catch (Exception e) {
  701. fail("No exception should be thrown.");
  702. }
  703. }
  704. /**
  705. * Tests a bug where the plot is no longer registered as a listener
  706. * with the dataset(s) and axes after deserialization. See patch 1209475
  707. * at SourceForge.
  708. */
  709. @Test
  710. public void testSerialization5() {
  711. XYSeriesCollection dataset1 = new XYSeriesCollection();
  712. NumberAxis domainAxis1 = new NumberAxis("Domain 1");
  713. NumberAxis rangeAxis1 = new NumberAxis("Range 1");
  714. StandardXYItemRenderer renderer1 = new StandardXYItemRenderer();
  715. XYPlot p1 = new XYPlot(dataset1, domainAxis1, rangeAxis1, renderer1);
  716. NumberAxis domainAxis2 = new NumberAxis("Domain 2");
  717. NumberAxis rangeAxis2 = new NumberAxis("Range 2");
  718. StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
  719. XYSeriesCollection dataset2 = new XYSeriesCollection();
  720. p1.setDataset(1, dataset2);
  721. p1.setDomainAxis(1, domainAxis2);
  722. p1.setRangeAxis(1, rangeAxis2);
  723. p1.setRenderer(1, renderer2);
  724. XYPlot p2 = (XYPlot) TestUtilities.serialised(p1);
  725. assertEquals(p1, p2);
  726. // now check that all datasets, renderers and axes are being listened
  727. // too...
  728. NumberAxis domainAxisA = (NumberAxis) p2.getDomainAxis(0);
  729. NumberAxis rangeAxisA = (NumberAxis) p2.getRangeAxis(0);
  730. XYSeriesCollection datasetA = (XYSeriesCollection) p2.getDataset(0);
  731. StandardXYItemRenderer rendererA
  732. = (StandardXYItemRenderer) p2.getRenderer(0);
  733. NumberAxis domainAxisB = (NumberAxis) p2.getDomainAxis(1);
  734. NumberAxis rangeAxisB = (NumberAxis) p2.getRangeAxis(1);
  735. XYSeriesCollection datasetB = (XYSeriesCollection) p2.getDataset(1);
  736. StandardXYItemRenderer rendererB
  737. = (StandardXYItemRenderer) p2.getRenderer(1);
  738. assertTrue(datasetA.hasListener(p2));
  739. assertTrue(domainAxisA.hasListener(p2));
  740. assertTrue(rangeAxisA.hasListener(p2));
  741. assertTrue(rendererA.hasListener(p2));
  742. assertTrue(datasetB.hasListener(p2));
  743. assertTrue(domainAxisB.hasListener(p2));
  744. assertTrue(rangeAxisB.hasListener(p2));
  745. assertTrue(rendererB.hasListener(p2));
  746. }
  747. /**
  748. * Some checks for the getRendererForDataset() method.
  749. */
  750. @Test
  751. public void testGetRendererForDataset() {
  752. XYDataset d0 = new XYSeriesCollection();
  753. XYDataset d1 = new XYSeriesCollection();
  754. XYDataset d2 = new XYSeriesCollection();
  755. XYDataset d3 = new XYSeriesCollection(); // not used by plot
  756. XYItemRenderer r0 = new XYLineAndShapeRenderer();
  757. XYItemRenderer r2 = new XYLineAndShapeRenderer();
  758. XYPlot plot = new XYPlot();
  759. plot.setDataset(0, d0);
  760. plot.setDataset(1, d1);
  761. plot.setDataset(2, d2);
  762. plot.setRenderer(0, r0);
  763. // no renderer 1
  764. plot.setRenderer(2, r2);
  765. assertEquals(r0, plot.getRendererForDataset(d0));
  766. assertEquals(r0, plot.getRendererForDataset(d1));
  767. assertEquals(r2, plot.getRendererForDataset(d2));
  768. assertEquals(null, plot.getRendererForDataset(d3));
  769. assertEquals(null, plot.getRendererForDataset(null));
  770. }
  771. /**
  772. * Some checks for the getLegendItems() method.
  773. */
  774. @Test
  775. public void testGetLegendItems() {
  776. // check the case where there is a secondary dataset that doesn't
  777. // have a renderer (i.e. falls back to renderer 0)
  778. XYDataset d0 = createDataset1();
  779. XYDataset d1 = createDataset2();
  780. XYItemRenderer r0 = new XYLineAndShapeRenderer();
  781. XYPlot plot = new XYPlot();
  782. plot.setDataset(0, d0);
  783. plot.setDataset(1, d1);
  784. plot.setRenderer(0, r0);
  785. LegendItemCollection items = plot.getLegendItems();
  786. assertEquals(2, items.getItemCount());
  787. }
  788. /**
  789. * Creates a sample dataset.
  790. *
  791. * @return Series 1.
  792. */
  793. private IntervalXYDataset createDataset1() {
  794. // create dataset 1...
  795. TimeSeries series1 = new TimeSeries("Series 1", Day.class);
  796. series1.add(new Day(1, MonthConstants.MARCH, 2002), 12353.3);
  797. series1.add(new Day(2, MonthConstants.MARCH, 2002), 13734.4);
  798. series1.add(new Day(3, MonthConstants.MARCH, 2002), 14525.3);
  799. series1.add(new Day(4, MonthConstants.MARCH, 2002), 13984.3);
  800. series1.add(new Day(5, MonthConstants.MARCH, 2002), 12999.4);
  801. series1.add(new Day(6, MonthConstants.MARCH, 2002), 14274.3);
  802. series1.add(new Day(7, MonthConstants.MARCH, 2002), 15943.5);
  803. series1.add(new Day(8, MonthConstants.MARCH, 2002), 14845.3);
  804. series1.add(new Day(9, MonthConstants.MARCH, 2002), 14645.4);
  805. series1.add(new Day(10, MonthConstants.MARCH, 2002), 16234.6);
  806. series1.add(new Day(11, MonthConstants.MARCH, 2002), 17232.3);
  807. series1.add(new Day(12, MonthConstants.MARCH, 2002), 14232.2);
  808. series1.add(new Day(13, MonthConstants.MARCH, 2002), 13102.2);
  809. series1.add(new Day(14, MonthConstants.MARCH, 2002), 14230.2);
  810. series1.add(new Day(15, MonthConstants.MARCH, 2002), 11235.2);
  811. TimeSeriesCollection collection = new TimeSeriesCollection(series1);
  812. return collection;
  813. }
  814. /**
  815. * Creates a sample dataset.
  816. *
  817. * @return A sample dataset.
  818. */
  819. private XYDataset createDataset2() {
  820. // create dataset 1...
  821. XYSeries series = new XYSeries("Series 2");
  822. XYSeriesCollection collection = new XYSeriesCollection(series);
  823. return collection;
  824. }
  825. /**
  826. * A test for a bug where setting the renderer doesn't register the plot
  827. * as a RendererChangeListener.
  828. */
  829. @Test
  830. public void testSetRenderer() {
  831. XYPlot plot = new XYPlot();
  832. XYItemRenderer renderer = new XYLineAndShapeRenderer();
  833. plot.setRenderer(renderer);
  834. // now make a change to the renderer and see if it triggers a plot
  835. // change event...
  836. MyPlotChangeListener listener = new MyPlotChangeListener();
  837. plot.addChangeListener(listener);
  838. renderer.setSeriesPaint(0, Color.black);
  839. assertTrue(listener.getEvent() != null);
  840. }
  841. /**
  842. * Some checks for the removeAnnotation() method.
  843. */
  844. @Test
  845. public void testRemoveAnnotation() {
  846. XYPlot plot = new XYPlot();
  847. XYTextAnnotation a1 = new XYTextAnnotation("X", 1.0, 2.0);
  848. XYTextAnnotation a2 = new XYTextAnnotation("X", 3.0, 4.0);
  849. XYTextAnnotation a3 = new XYTextAnnotation("X", 1.0, 2.0);
  850. plot.addAnnotation(a1);
  851. plot.addAnnotation(a2);
  852. plot.addAnnotation(a3);
  853. plot.removeAnnotation(a2);
  854. XYTextAnnotation x = (XYTextAnnotation) plot.getAnnotations().get(0);
  855. assertEquals(x, a1);
  856. // now remove a3, but since a3.equals(a1), this will in fact remove
  857. // a1...
  858. assertTrue(a1.equals(a3));
  859. plot.removeAnnotation(a3); // actually removes a1
  860. x = (XYTextAnnotation) plot.getAnnotations().get(0);
  861. assertEquals(x, a3);
  862. }
  863. /**
  864. * Some tests for the addDomainMarker() method(s).
  865. */
  866. @Test
  867. public void testAddDomainMarker() {
  868. XYPlot plot = new XYPlot();
  869. Marker m = new ValueMarker(1.0);
  870. plot.addDomainMarker(m);
  871. List listeners = Arrays.asList(m.getListeners(
  872. MarkerChangeListener.class));
  873. assertTrue(listeners.contains(plot));
  874. plot.clearDomainMarkers();
  875. listeners = Arrays.asList(m.getListeners(MarkerChangeListener.class));
  876. assertFalse(listeners.contains(plot));
  877. }
  878. /**
  879. * Some tests for the addRangeMarker() method(s).
  880. */
  881. @Test
  882. public void testAddRangeMarker() {
  883. XYPlot plot = new XYPlot();
  884. Marker m = new ValueMarker(1.0);
  885. plot.addRangeMarker(m);
  886. List listeners = Arrays.asList(m.getListeners(
  887. MarkerChangeListener.class));
  888. assertTrue(listeners.contains(plot));
  889. plot.clearRangeMarkers();
  890. listeners = Arrays.asList(m.getListeners(MarkerChangeListener.class));
  891. assertFalse(listeners.contains(plot));
  892. }
  893. /**
  894. * A test for bug 1654215 (where a renderer is added to the plot without
  895. * a corresponding dataset and it throws an exception at drawing time).
  896. */
  897. @Test
  898. public void test1654215() {
  899. DefaultXYDataset dataset = new DefaultXYDataset();
  900. JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y",
  901. dataset, PlotOrientation.VERTICAL, true, false, false);
  902. XYPlot plot = (XYPlot) chart.getPlot();
  903. plot.setRenderer(1, new XYLineAndShapeRenderer());
  904. try {
  905. BufferedImage image = new BufferedImage(200 , 100,
  906. BufferedImage.TYPE_INT_RGB);
  907. Graphics2D g2 = image.createGraphics();
  908. chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
  909. g2.dispose();
  910. }
  911. catch (Exception e) {
  912. fail("No exception should be thrown.");
  913. }
  914. }
  915. /**
  916. * A test for drawing range grid lines when there is no primary renderer.
  917. * In 1.0.4, this is throwing a NullPointerException.
  918. */
  919. @Test
  920. public void testDrawRangeGridlines() {
  921. DefaultXYDataset dataset = new DefaultXYDataset();
  922. JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y",
  923. dataset, PlotOrientation.VERTICAL, true, false, false);
  924. XYPlot plot = (XYPlot) chart.getPlot();
  925. plot.setRenderer(null);
  926. try {
  927. BufferedImage image = new BufferedImage(200 , 100,
  928. BufferedImage.TYPE_INT_RGB);
  929. Graphics2D g2 = image.createGraphics();
  930. chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
  931. g2.dispose();
  932. }
  933. catch (Exception e) {
  934. fail("No exception should be thrown.");
  935. }
  936. }
  937. /**
  938. * A test for drawing a plot where a series has zero items. With
  939. * JFreeChart 1.0.5+cvs this was throwing an exception at one point.
  940. */
  941. @Test
  942. public void testDrawSeriesWithZeroItems() {
  943. DefaultXYDataset dataset = new DefaultXYDataset();
  944. dataset.addSeries("Series 1", new double[][] {{1.0, 2.0}, {3.0, 4.0}});
  945. dataset.addSeries("Series 2", new double[][] {{}, {}});
  946. JFreeChart chart = ChartFactory.createXYLineChart("Title", "X", "Y",
  947. dataset, PlotOrientation.VERTICAL, true, false, false);
  948. try {
  949. BufferedImage image = new BufferedImage(200 , 100,
  950. BufferedImage.TYPE_INT_RGB);
  951. Graphics2D g2 = image.createGraphics();
  952. chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
  953. g2.dispose();
  954. }
  955. catch (Exception e) {
  956. fail("No exception should be thrown.");
  957. }
  958. }
  959. /**
  960. * Check that removing a marker that isn't assigned to the plot returns
  961. * false.
  962. */
  963. @Test
  964. public void testRemoveDomainMarker() {
  965. XYPlot plot = new XYPlot();
  966. assertFalse(plot.removeDomainMarker(new ValueMarker(0.5)));
  967. }
  968. /**
  969. * Check that removing a marker that isn't assigned to the plot returns
  970. * false.
  971. */
  972. @Test
  973. public void testRemoveRangeMarker() {
  974. XYPlot plot = new XYPlot();
  975. assertFalse(plot.removeRangeMarker(new ValueMarker(0.5)));
  976. }
  977. /**
  978. * Some tests for the getDomainAxisForDataset() method.
  979. */
  980. @Test
  981. public void testGetDomainAxisForDataset() {
  982. XYDataset dataset = new XYSeriesCollection();
  983. NumberAxis xAxis = new NumberAxis("X");
  984. NumberAxis yAxis = new NumberAxis("Y");
  985. XYItemRenderer renderer = new DefaultXYItemRenderer();
  986. XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
  987. assertEquals(xAxis, plot.getDomainAxisForDataset(0));
  988. // should get IllegalArgumentException for negative index
  989. boolean pass = false;
  990. try {
  991. plot.getDomainAxisForDataset(-1);
  992. }
  993. catch (IllegalArgumentException e) {
  994. pass = true;
  995. }
  996. assertTrue(pass);
  997. // if multiple axes are mapped, the first in the list should be
  998. // returned...
  999. NumberAxis xAxis2 = new NumberAxis("X2");
  1000. plot.setDomainAxis(1, xAxis2);
  1001. assertEquals(xAxis, plot.getDomainAxisForDataset(0));
  1002. plot.mapDatasetToDomainAxis(0, 1);
  1003. assertEquals(xAxis2, plot.getDomainAxisForDataset(0));
  1004. List axisIndices = Arrays.asList(new Integer[] {new Integer(0),
  1005. new Integer(1)});
  1006. plot.mapDatasetToDomainAxes(0, axisIndices);
  1007. assertEquals(xAxis, plot.getDomainAxisForDataset(0));
  1008. axisIndices = Arrays.asList(new Integer[] {new Integer(1),
  1009. new Integer(2)});
  1010. plot.mapDatasetToDomainAxes(0, axisIndices);
  1011. assertEquals(xAxis2, plot.getDomainAxisForDataset(0));
  1012. }
  1013. /**
  1014. * Some tests for the getRangeAxisForDataset() method.
  1015. */
  1016. @Test
  1017. public void testGetRangeAxisForDataset() {
  1018. XYDataset dataset = new XYSeriesCollection();
  1019. NumberAxis xAxis = new NumberAxis("X");
  1020. NumberAxis yAxis = new NumberAxis("Y");
  1021. XYItemRenderer renderer = new DefaultXYItemRenderer();
  1022. XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
  1023. assertEquals(yAxis, plot.getRangeAxisForDataset(0));
  1024. // should get IllegalArgumentException for negative index
  1025. boolean pass = false;
  1026. try {
  1027. plot.getRangeAxisForDataset(-1);
  1028. }
  1029. catch (IllegalArgumentException e) {
  1030. pass = true;
  1031. }
  1032. assertTrue(pass);
  1033. // if multiple axes are mapped, the first in the list should be
  1034. // returned...
  1035. NumberAxis yAxis2 = new NumberAxis("Y2");
  1036. plot.setRangeAxis(1, yAxis2);
  1037. assertEquals(yAxis, plot.getRangeAxisForDataset(0));
  1038. plot.mapDatasetToRangeAxis(0, 1);
  1039. assertEquals(yAxis2, plot.getRangeAxisForDataset(0));
  1040. List axisIndices = Arrays.asList(new Integer[] {new Integer(0),
  1041. new Integer(1)});
  1042. plot.mapDatasetToRangeAxes(0, axisIndices);
  1043. assertEquals(yAxis, plot.getRangeAxisForDataset(0));
  1044. axisIndices = Arrays.asList(new Integer[] {new Integer(1),
  1045. new Integer(2)});
  1046. plot.mapDatasetToRangeAxes(0, axisIndices);
  1047. assertEquals(yAxis2, plot.getRangeAxisForDataset(0));
  1048. }
  1049. /**
  1050. * Datasets are now stored in a Map, and it should be possible to assign
  1051. * them an arbitrary key (index).
  1052. */
  1053. @Test
  1054. public void testDatasetIndices() {
  1055. XYDataset dataset = new XYSeriesCollection();
  1056. NumberAxis xAxis = new NumberAxis("X");
  1057. NumberAxis yAxis = new NumberAxis("Y");
  1058. XYItemRenderer renderer = new DefaultXYItemRenderer();
  1059. XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
  1060. assertEquals(dataset, plot.getDataset(0));
  1061. XYSeriesCollection dataset2 = new XYSeriesCollection();
  1062. dataset2.addSeries(new XYSeries("Series in dataset 2"));
  1063. // we should be able to give a dataset an arbitrary index
  1064. plot.setDataset(99, dataset2);
  1065. assertEquals(2, plot.getDatasetCount());
  1066. assertEquals(dataset2, plot.getDataset(99));
  1067. assertEquals(0, plot.indexOf(dataset));
  1068. assertEquals(99, plot.indexOf(dataset2));
  1069. }
  1070. @Test
  1071. public void testAxisIndices() {
  1072. XYDataset dataset = new XYSeriesCollection();
  1073. NumberAxis xAxis = new NumberAxis("X");
  1074. NumberAxis yAxis = new NumberAxis("Y");
  1075. XYItemRenderer renderer = new DefaultXYItemRenderer();
  1076. XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
  1077. assertEquals(xAxis, plot.getDomainAxis(0));
  1078. assertEquals(yAxis, plot.getRangeAxis(0));
  1079. NumberAxis xAxis2 = new NumberAxis("X2");
  1080. plot.setDomainAxis(99, xAxis2);
  1081. assertEquals(xAxis2, plot.getDomainAxis(99));
  1082. NumberAxis yAxis2 = new NumberAxis("Y2");
  1083. plot.setRangeAxis(99, yAxis2);
  1084. assertEquals(yAxis2, plot.getRangeAxis(99));
  1085. }
  1086. @Test
  1087. public void testAxisLocationIndices() {
  1088. XYDataset dataset = new XYSeriesCollection();
  1089. NumberAxis xAxis = new NumberAxis("X");
  1090. NumberAxis yAxis = new NumberAxis("Y");
  1091. XYItemRenderer renderer = new DefaultXYItemRenderer();
  1092. XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
  1093. NumberAxis xAxis2 = new NumberAxis("X2");
  1094. NumberAxis yAxis2 = new NumberAxis("Y2");
  1095. plot.setDomainAxis(99, xAxis2);
  1096. plot.setRangeAxis(99, yAxis2);
  1097. plot.setDomainAxisLocation(99, AxisLocation.BOTTOM_OR_RIGHT);
  1098. assertEquals(AxisLocation.BOTTOM_OR_RIGHT,
  1099. plot.getDomainAxisLocation(99));
  1100. plot.setRangeAxisLocation(99, AxisLocation.BOTTOM_OR_LEFT);
  1101. assertEquals(AxisLocation.BOTTOM_OR_LEFT,
  1102. plot.getRangeAxisLocation(99));
  1103. }
  1104. @Test
  1105. public void testRendererIndices() {
  1106. XYDataset dataset = new XYSeriesCollection();
  1107. NumberAxis xAxis = new NumberAxis("X");
  1108. NumberAxis yAxis = new NumberAxis("Y");
  1109. XYItemRenderer renderer = new DefaultXYItemRenderer();
  1110. XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
  1111. assertEquals(renderer, plot.getRenderer(0));
  1112. // we should be able to give a renderer an arbitrary index
  1113. XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
  1114. plot.setRenderer(20, renderer2);
  1115. assertEquals(2, plot.getRendererCount());
  1116. assertEquals(renderer2, plot.getRenderer(20));
  1117. assertEquals(20, plot.getIndexOf(renderer2));
  1118. }
  1119. @Test
  1120. public void testGetRendererForDataset2() {
  1121. XYDataset dataset = new XYSeriesCollection();
  1122. NumberAxis xAxis = new NumberAxis("X");
  1123. NumberAxis yAxis = new NumberAxis("Y");
  1124. XYItemRenderer renderer = new DefaultXYItemRenderer();
  1125. XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
  1126. // add a second dataset
  1127. XYSeriesCollection dataset2 = new XYSeriesCollection();
  1128. dataset2.addSeries(new XYSeries("Series in dataset 2"));
  1129. plot.setDataset(99, dataset2);
  1130. // by default, the renderer with index 0 is used
  1131. assertEquals(renderer, plot.getRendererForDataset(dataset2));
  1132. // add a second renderer with the same index as dataset2, now it will
  1133. // be used
  1134. XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
  1135. plot.setRenderer(99, renderer);
  1136. assertEquals(renderer2, plot.getRendererForDataset(dataset2));
  1137. }
  1138. @Test
  1139. public void testMapDatasetToDomainAxis() {
  1140. XYDataset dataset = new XYSeriesCollection();
  1141. NumberAxis xAxis = new NumberAxis("X");
  1142. NumberAxis yAxis = new NumberAxis("Y");
  1143. XYItemRenderer renderer = new DefaultXYItemRenderer();
  1144. XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
  1145. NumberAxis xAxis2 = new NumberAxis("X2");
  1146. plot.setDomainAxis(11, xAxis2);
  1147. // add a second dataset
  1148. XYSeriesCollection dataset2 = new XYSeriesCollection();
  1149. dataset2.addSeries(new XYSeries("Series in dataset 2"));
  1150. plot.setDataset(99, dataset);
  1151. assertEquals(xAxis, plot.getDomainAxisForDataset(99));
  1152. // now map the dataset to the second xAxis
  1153. plot.mapDatasetToDomainAxis(99, 11);
  1154. assertEquals(xAxis2, plot.getDomainAxisForDataset(99));
  1155. }
  1156. @Test
  1157. public void testMapDatasetToRangeAxis() {
  1158. XYDataset dataset = new XYSeriesCollection();
  1159. NumberAxis xAxis = new NumberAxis("X");
  1160. NumberAxis yAxis = new NumberAxis("Y");
  1161. XYItemRenderer renderer = new DefaultXYItemRenderer();
  1162. XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
  1163. NumberAxis yAxis2 = new NumberAxis("Y2");
  1164. plot.setRangeAxis(22, yAxis2);
  1165. // add a second dataset
  1166. XYSeriesCollection dataset2 = new XYSeriesCollection();
  1167. dataset2.addSeries(new XYSeries("Series in dataset 2"));
  1168. plot.setDataset(99, dataset);
  1169. assertEquals(yAxis, plot.getRangeAxisForDataset(99));
  1170. // now map the dataset to the second xAxis
  1171. plot.mapDatasetToRangeAxis(99, 22);
  1172. assertEquals(yAxis2, plot.getRangeAxisForDataset(99));
  1173. }
  1174. @Test
  1175. public void testDomainMarkerIndices() {
  1176. XYDataset dataset = new XYSeriesCollection();
  1177. NumberAxis xAxis = new NumberAxis("X");
  1178. NumberAxis yAxis = new NumberAxis("Y");
  1179. XYItemRenderer renderer = new DefaultXYItemRenderer();
  1180. XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
  1181. // add a second dataset, plotted against a second x axis
  1182. XYSeriesCollection dataset2 = new XYSeriesCollection();
  1183. dataset2.addSeries(new XYSeries("Series in dataset 2"));
  1184. plot.setDataset(99, dataset);
  1185. NumberAxis xAxis2 = new NumberAxis("X2");
  1186. plot.setDomainAxis(1, xAxis2);
  1187. XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
  1188. plot.setRenderer(99, renderer2);
  1189. plot.mapDatasetToDomainAxis(99, 1);
  1190. ValueMarker xMarker1 = new ValueMarker(123);
  1191. plot.addDomainMarker(99, xMarker1, Layer.FOREGROUND);
  1192. assertTrue(plot.getDomainMarkers(99, Layer.FOREGROUND).contains(
  1193. xMarker1));
  1194. }
  1195. @Test
  1196. public void testRangeMarkerIndices() {
  1197. XYDataset dataset = new XYSeriesCollection();
  1198. NumberAxis xAxis = new NumberAxis("X");
  1199. NumberAxis yAxis = new NumberAxis("Y");
  1200. XYItemRenderer renderer = new DefaultXYItemRenderer();
  1201. XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
  1202. // add a second dataset, plotted against a second axis
  1203. XYSeriesCollection dataset2 = new XYSeriesCollection();
  1204. dataset2.addSeries(new XYSeries("Series in dataset 2"));
  1205. plot.setDataset(99, dataset);
  1206. NumberAxis yAxis2 = new NumberAxis("Y2");
  1207. plot.setRangeAxis(1, yAxis2);
  1208. XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
  1209. plot.setRenderer(99, renderer2);
  1210. plot.mapDatasetToRangeAxis(99, 1);
  1211. ValueMarker yMarker1 = new ValueMarker(123);
  1212. plot.addRangeMarker(99, yMarker1, Layer.FOREGROUND);
  1213. assertTrue(plot.getRangeMarkers(99, Layer.FOREGROUND).contains(yMarker1));
  1214. }
  1215. }