PolarPlotTest.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. * PolarPlotTest.java
  29. * ------------------
  30. * (C) Copyright 2005-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. * 23-Feb-2005 : Version 1 (DG);
  38. * 08-Jun-2005 : Extended testEquals() (DG);
  39. * 07-Feb-2007 : Extended testEquals() and testCloning() (DG);
  40. * 17-Feb-2008 : Tests for new angleTickUnit field (DG);
  41. * 09-Dec-2009 : Added new tests (DG);
  42. * 12-Nov-2011 : Added tests for translateToJava2D (MH);
  43. * 17-Dec-2011 : Updated testEquals() (DG);
  44. *
  45. */
  46. package org.jfree.chart.plot;
  47. import static org.junit.Assert.assertEquals;
  48. import static org.junit.Assert.assertFalse;
  49. import static org.junit.Assert.assertTrue;
  50. import java.awt.BasicStroke;
  51. import java.awt.Color;
  52. import java.awt.Font;
  53. import java.awt.GradientPaint;
  54. import java.awt.Point;
  55. import java.awt.Stroke;
  56. import java.awt.geom.Rectangle2D;
  57. import org.jfree.chart.LegendItem;
  58. import org.jfree.chart.LegendItemCollection;
  59. import org.jfree.chart.TestUtilities;
  60. import org.jfree.chart.axis.LogAxis;
  61. import org.jfree.chart.axis.NumberAxis;
  62. import org.jfree.chart.axis.NumberTickUnit;
  63. import org.jfree.chart.axis.ValueAxis;
  64. import org.jfree.chart.renderer.DefaultPolarItemRenderer;
  65. import org.jfree.data.xy.DefaultXYDataset;
  66. import org.jfree.data.xy.XYSeries;
  67. import org.jfree.data.xy.XYSeriesCollection;
  68. import org.junit.Test;
  69. /**
  70. * Some tests for the {@link PolarPlot} class.
  71. */
  72. public class PolarPlotTest {
  73. /**
  74. * Some checks for the getLegendItems() method.
  75. */
  76. @Test
  77. public void testGetLegendItems() {
  78. XYSeriesCollection d = new XYSeriesCollection();
  79. d.addSeries(new XYSeries("A"));
  80. d.addSeries(new XYSeries("B"));
  81. DefaultPolarItemRenderer r = new DefaultPolarItemRenderer();
  82. PolarPlot plot = new PolarPlot();
  83. plot.setDataset(d);
  84. plot.setRenderer(r);
  85. LegendItemCollection items = plot.getLegendItems();
  86. assertEquals(2, items.getItemCount());
  87. LegendItem item1 = items.get(0);
  88. assertEquals("A", item1.getLabel());
  89. LegendItem item2 = items.get(1);
  90. assertEquals("B", item2.getLabel());
  91. }
  92. /**
  93. * Some checks for the getLegendItems() method with multiple datasets.
  94. */
  95. @Test
  96. public void testGetLegendItems2() {
  97. XYSeriesCollection d1 = new XYSeriesCollection();
  98. d1.addSeries(new XYSeries("A"));
  99. d1.addSeries(new XYSeries("B"));
  100. XYSeriesCollection d2 = new XYSeriesCollection();
  101. d2.addSeries(new XYSeries("C"));
  102. d2.addSeries(new XYSeries("D"));
  103. DefaultPolarItemRenderer r = new DefaultPolarItemRenderer();
  104. PolarPlot plot = new PolarPlot();
  105. plot.setDataset(d1);
  106. plot.setDataset(1, d2);
  107. plot.setRenderer(r);
  108. plot.setRenderer(1, new DefaultPolarItemRenderer());
  109. LegendItemCollection items = plot.getLegendItems();
  110. assertEquals(4, items.getItemCount());
  111. LegendItem item1 = items.get(0);
  112. assertEquals("A", item1.getLabel());
  113. LegendItem item2 = items.get(1);
  114. assertEquals("B", item2.getLabel());
  115. LegendItem item3 = items.get(2);
  116. assertEquals("C", item3.getLabel());
  117. LegendItem item4 = items.get(3);
  118. assertEquals("D", item4.getLabel());
  119. }
  120. /**
  121. * Some checks for the equals() method.
  122. */
  123. @Test
  124. public void testEquals() {
  125. PolarPlot plot1 = new PolarPlot();
  126. PolarPlot plot2 = new PolarPlot();
  127. assertTrue(plot1.equals(plot2));
  128. assertTrue(plot2.equals(plot1));
  129. plot1.setAngleGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.red,
  130. 3.0f, 4.0f, Color.blue));
  131. assertFalse(plot1.equals(plot2));
  132. plot2.setAngleGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.red,
  133. 3.0f, 4.0f, Color.blue));
  134. assertTrue(plot1.equals(plot2));
  135. Stroke s = new BasicStroke(1.23f);
  136. plot1.setAngleGridlineStroke(s);
  137. assertFalse(plot1.equals(plot2));
  138. plot2.setAngleGridlineStroke(s);
  139. assertTrue(plot1.equals(plot2));
  140. plot1.setAngleTickUnit(new NumberTickUnit(11.0));
  141. assertFalse(plot1.equals(plot2));
  142. plot2.setAngleTickUnit(new NumberTickUnit(11.0));
  143. assertTrue(plot1.equals(plot2));
  144. plot1.setAngleGridlinesVisible(false);
  145. assertFalse(plot1.equals(plot2));
  146. plot2.setAngleGridlinesVisible(false);
  147. assertTrue(plot1.equals(plot2));
  148. plot1.setAngleLabelFont(new Font("Serif", Font.PLAIN, 9));
  149. assertFalse(plot1.equals(plot2));
  150. plot2.setAngleLabelFont(new Font("Serif", Font.PLAIN, 9));
  151. assertTrue(plot1.equals(plot2));
  152. plot1.setAngleLabelPaint(new GradientPaint(9.0f, 8.0f, Color.blue,
  153. 7.0f, 6.0f, Color.red));
  154. assertFalse(plot1.equals(plot2));
  155. plot2.setAngleLabelPaint(new GradientPaint(9.0f, 8.0f, Color.blue,
  156. 7.0f, 6.0f, Color.red));
  157. assertTrue(plot1.equals(plot2));
  158. plot1.setAngleLabelsVisible(false);
  159. assertFalse(plot1.equals(plot2));
  160. plot2.setAngleLabelsVisible(false);
  161. assertTrue(plot1.equals(plot2));
  162. plot1.setAxis(new NumberAxis("Test"));
  163. assertFalse(plot1.equals(plot2));
  164. plot2.setAxis(new NumberAxis("Test"));
  165. assertTrue(plot1.equals(plot2));
  166. plot1.setRadiusGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
  167. 3.0f, 4.0f, Color.black));
  168. assertFalse(plot1.equals(plot2));
  169. plot2.setRadiusGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.white,
  170. 3.0f, 4.0f, Color.black));
  171. assertTrue(plot1.equals(plot2));
  172. plot1.setRadiusGridlineStroke(s);
  173. assertFalse(plot1.equals(plot2));
  174. plot2.setRadiusGridlineStroke(s);
  175. assertTrue(plot1.equals(plot2));
  176. plot1.setRadiusGridlinesVisible(false);
  177. assertFalse(plot1.equals(plot2));
  178. plot2.setRadiusGridlinesVisible(false);
  179. assertTrue(plot1.equals(plot2));
  180. plot1.setRadiusMinorGridlinesVisible(false);
  181. assertFalse(plot1.equals(plot2));
  182. plot2.setRadiusMinorGridlinesVisible(false);
  183. assertTrue(plot1.equals(plot2));
  184. plot1.addCornerTextItem("XYZ");
  185. assertFalse(plot1.equals(plot2));
  186. plot2.addCornerTextItem("XYZ");
  187. assertTrue(plot1.equals(plot2));
  188. plot1.setMargin(6);
  189. assertFalse(plot1.equals(plot2));
  190. plot2.setMargin(6);
  191. assertTrue(plot1.equals(plot2));
  192. LegendItemCollection lic1 = new LegendItemCollection();
  193. lic1.add(new LegendItem("XYZ", Color.red));
  194. plot1.setFixedLegendItems(lic1);
  195. assertFalse(plot1.equals(plot2));
  196. LegendItemCollection lic2 = new LegendItemCollection();
  197. lic2.add(new LegendItem("XYZ", Color.red));
  198. plot2.setFixedLegendItems(lic2);
  199. assertTrue(plot1.equals(plot2));
  200. }
  201. /**
  202. * Some basic checks for the clone() method.
  203. */
  204. @Test
  205. public void testCloning() throws CloneNotSupportedException {
  206. PolarPlot p1 = new PolarPlot();
  207. PolarPlot p2 = (PolarPlot) p1.clone();
  208. assertTrue(p1 != p2);
  209. assertTrue(p1.getClass() == p2.getClass());
  210. assertTrue(p1.equals(p2));
  211. // check independence
  212. p1.addCornerTextItem("XYZ");
  213. assertFalse(p1.equals(p2));
  214. p2.addCornerTextItem("XYZ");
  215. assertTrue(p1.equals(p2));
  216. p1 = new PolarPlot(new DefaultXYDataset(), new NumberAxis("A1"),
  217. new DefaultPolarItemRenderer());
  218. p2 = (PolarPlot) p1.clone();
  219. assertTrue(p1 != p2);
  220. assertTrue(p1.getClass() == p2.getClass());
  221. assertTrue(p1.equals(p2));
  222. // check independence
  223. p1.getAxis().setLabel("ABC");
  224. assertFalse(p1.equals(p2));
  225. p2.getAxis().setLabel("ABC");
  226. assertTrue(p1.equals(p2));
  227. }
  228. /**
  229. * Serialize an instance, restore it, and check for equality.
  230. */
  231. @Test
  232. public void testSerialization() {
  233. PolarPlot p1 = new PolarPlot();
  234. p1.setAngleGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
  235. 4.0f, Color.blue));
  236. p1.setAngleLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
  237. 4.0f, Color.blue));
  238. p1.setRadiusGridlinePaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
  239. 4.0f, Color.blue));
  240. PolarPlot p2 = (PolarPlot) TestUtilities.serialised(p1);
  241. assertEquals(p1, p2);
  242. }
  243. @Test
  244. public void testTranslateToJava2D_NumberAxis() {
  245. Rectangle2D dataArea = new Rectangle2D.Double(0.0, 0.0, 100.0, 100.0);
  246. ValueAxis axis = new NumberAxis();
  247. axis.setRange(0.0, 20.0);
  248. PolarPlot plot = new PolarPlot(null, axis, null);
  249. plot.setMargin(0);
  250. plot.setAngleOffset(0.0);
  251. Point point = plot.translateToJava2D(0.0, 10.0, axis, dataArea );
  252. assertEquals(75.0, point.getX(), 0.5);
  253. assertEquals(50.0, point.getY(), 0.5);
  254. point = plot.translateToJava2D(90.0, 5.0, axis, dataArea );
  255. assertEquals(50.0, point.getX(), 0.5);
  256. assertEquals(62.5, point.getY(), 0.5);
  257. point = plot.translateToJava2D(45.0, 20.0, axis, dataArea );
  258. assertEquals(85.0, point.getX(), 0.5);
  259. assertEquals(85.0, point.getY(), 0.5);
  260. point = plot.translateToJava2D(135.0, 20.0, axis, dataArea );
  261. assertEquals(15.0, point.getX(), 0.5);
  262. assertEquals(85.0, point.getY(), 0.5);
  263. point = plot.translateToJava2D(225.0, 15.0, axis, dataArea );
  264. assertEquals(23.0, point.getX(), 0.5);
  265. assertEquals(23.0, point.getY(), 0.5);
  266. point = plot.translateToJava2D(315.0, 15.0, axis, dataArea );
  267. assertEquals(77.0, point.getX(), 0.5);
  268. assertEquals(23.0, point.getY(), 0.5);
  269. point = plot.translateToJava2D(21.0, 11.5, axis, dataArea );
  270. assertEquals(77.0, point.getX(), 0.5);
  271. assertEquals(60.0, point.getY(), 0.5);
  272. point = plot.translateToJava2D(162.0, 7.0, axis, dataArea );
  273. assertEquals(33.0, point.getX(), 0.5);
  274. assertEquals(55.0, point.getY(), 0.5);
  275. }
  276. @Test
  277. public void testTranslateToJava2D_NumberAxisAndMargin() {
  278. Rectangle2D dataArea = new Rectangle2D.Double(10.0, 10.0, 80.0, 80.0);
  279. ValueAxis axis = new NumberAxis();
  280. axis.setRange(-2.0, 2.0);
  281. PolarPlot plot = new PolarPlot(null, axis, null);
  282. plot.setAngleOffset(0.0);
  283. Point point = plot.translateToJava2D(0.0, 10.0, axis, dataArea );
  284. assertEquals(110.0, point.getX(), 0.5);
  285. assertEquals(50.0, point.getY(), 0.5);
  286. point = plot.translateToJava2D(90.0, 5.0, axis, dataArea );
  287. assertEquals(50.0, point.getX(), 0.5);
  288. assertEquals(85.0, point.getY(), 0.5);
  289. point = plot.translateToJava2D(45.0, 20.0, axis, dataArea );
  290. assertEquals(128.0, point.getX(), 0.5);
  291. assertEquals(128.0, point.getY(), 0.5);
  292. point = plot.translateToJava2D(135.0, 20.0, axis, dataArea );
  293. assertEquals(-28.0, point.getX(), 0.5);
  294. assertEquals(128.0, point.getY(), 0.5);
  295. point = plot.translateToJava2D(225.0, 15.0, axis, dataArea );
  296. assertEquals(-10.0, point.getX(), 0.5);
  297. assertEquals(-10.0, point.getY(), 0.5);
  298. point = plot.translateToJava2D(315.0, 15.0, axis, dataArea );
  299. assertEquals(110.0, point.getX(), 0.5);
  300. assertEquals(-10.0, point.getY(), 0.5);
  301. point = plot.translateToJava2D(21.0, 11.5, axis, dataArea );
  302. assertEquals(113.0, point.getX(), 0.5);
  303. assertEquals(74.0, point.getY(), 0.5);
  304. point = plot.translateToJava2D(162.0, 7.0, axis, dataArea );
  305. assertEquals(7.0, point.getX(), 0.5);
  306. assertEquals(64.0, point.getY(), 0.5);
  307. }
  308. @Test
  309. public void testTranslateToJava2D_LogAxis() {
  310. Rectangle2D dataArea = new Rectangle2D.Double(0.0, 0.0, 100.0, 100.0);
  311. ValueAxis axis = new LogAxis();
  312. axis.setRange(1.0, 100.0);
  313. PolarPlot plot = new PolarPlot(null, axis, null);
  314. plot.setMargin(0);
  315. plot.setAngleOffset(0.0);
  316. Point point = plot.translateToJava2D(0.0, 10.0, axis, dataArea );
  317. assertEquals(75.0, point.getX(), 0.5);
  318. assertEquals(50.0, point.getY(), 0.5);
  319. point = plot.translateToJava2D(90.0, 5.0, axis, dataArea );
  320. assertEquals(50.0, point.getX(), 0.5);
  321. assertEquals(67.5, point.getY(), 0.5);
  322. point = plot.translateToJava2D(45.0, 20.0, axis, dataArea );
  323. assertEquals(73.0, point.getX(), 0.5);
  324. assertEquals(73.0, point.getY(), 0.5);
  325. }
  326. }