NumberAxisTest.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. * NumberAxisTest.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. * 26-Mar-2003 : Version 1 (DG);
  38. * 14-Aug-2003 : Added tests for equals() method (DG);
  39. * 05-Oct-2004 : Added tests to pick up a bug in the auto-range calculation for
  40. * a domain axis on an XYPlot using an XYSeriesCollection (DG);
  41. * 07-Jan-2005 : Added test for hashCode() (DG);
  42. * 11-Jan-2006 : Fixed testAutoRange2() and testAutoRange3() following changes
  43. * to BarRenderer (DG);
  44. * 20-Feb-2006 : Added rangeType field to equals() test (DG);
  45. *
  46. */
  47. package org.jfree.chart.axis;
  48. import static org.junit.Assert.assertEquals;
  49. import static org.junit.Assert.assertFalse;
  50. import static org.junit.Assert.assertTrue;
  51. import java.awt.geom.Rectangle2D;
  52. import java.text.DecimalFormat;
  53. import org.jfree.chart.ChartFactory;
  54. import org.jfree.chart.JFreeChart;
  55. import org.jfree.chart.TestUtilities;
  56. import org.jfree.chart.plot.CategoryPlot;
  57. import org.jfree.chart.plot.PlotOrientation;
  58. import org.jfree.chart.plot.XYPlot;
  59. import org.jfree.chart.renderer.category.BarRenderer;
  60. import org.jfree.data.RangeType;
  61. import org.jfree.data.category.DefaultCategoryDataset;
  62. import org.jfree.data.xy.XYSeries;
  63. import org.jfree.data.xy.XYSeriesCollection;
  64. import org.jfree.ui.RectangleEdge;
  65. import org.junit.Test;
  66. /**
  67. * Tests for the {@link NumberAxis} class.
  68. */
  69. public class NumberAxisTest {
  70. /**
  71. * Confirm that cloning works.
  72. */
  73. @Test
  74. public void testCloning() throws CloneNotSupportedException {
  75. NumberAxis a1 = new NumberAxis("Test");
  76. NumberAxis a2 = (NumberAxis) a1.clone();
  77. assertTrue(a1 != a2);
  78. assertTrue(a1.getClass() == a2.getClass());
  79. assertTrue(a1.equals(a2));
  80. }
  81. /**
  82. * Confirm that the equals method can distinguish all the required fields.
  83. */
  84. @Test
  85. public void testEquals() {
  86. NumberAxis a1 = new NumberAxis("Test");
  87. NumberAxis a2 = new NumberAxis("Test");
  88. assertTrue(a1.equals(a2));
  89. //private boolean autoRangeIncludesZero;
  90. a1.setAutoRangeIncludesZero(false);
  91. assertFalse(a1.equals(a2));
  92. a2.setAutoRangeIncludesZero(false);
  93. assertTrue(a1.equals(a2));
  94. //private boolean autoRangeStickyZero;
  95. a1.setAutoRangeStickyZero(false);
  96. assertFalse(a1.equals(a2));
  97. a2.setAutoRangeStickyZero(false);
  98. assertTrue(a1.equals(a2));
  99. //private NumberTickUnit tickUnit;
  100. a1.setTickUnit(new NumberTickUnit(25.0));
  101. assertFalse(a1.equals(a2));
  102. a2.setTickUnit(new NumberTickUnit(25.0));
  103. assertTrue(a1.equals(a2));
  104. //private NumberFormat numberFormatOverride;
  105. a1.setNumberFormatOverride(new DecimalFormat("0.00"));
  106. assertFalse(a1.equals(a2));
  107. a2.setNumberFormatOverride(new DecimalFormat("0.00"));
  108. assertTrue(a1.equals(a2));
  109. a1.setRangeType(RangeType.POSITIVE);
  110. assertFalse(a1.equals(a2));
  111. a2.setRangeType(RangeType.POSITIVE);
  112. assertTrue(a1.equals(a2));
  113. }
  114. /**
  115. * Two objects that are equal are required to return the same hashCode.
  116. */
  117. @Test
  118. public void testHashCode() {
  119. NumberAxis a1 = new NumberAxis("Test");
  120. NumberAxis a2 = new NumberAxis("Test");
  121. assertTrue(a1.equals(a2));
  122. int h1 = a1.hashCode();
  123. int h2 = a2.hashCode();
  124. assertEquals(h1, h2);
  125. }
  126. private static final double EPSILON = 0.0000001;
  127. /**
  128. * Test the translation of Java2D values to data values.
  129. */
  130. @Test
  131. public void testTranslateJava2DToValue() {
  132. NumberAxis axis = new NumberAxis();
  133. axis.setRange(50.0, 100.0);
  134. Rectangle2D dataArea = new Rectangle2D.Double(10.0, 50.0, 400.0, 300.0);
  135. double y1 = axis.java2DToValue(75.0, dataArea, RectangleEdge.LEFT);
  136. assertEquals(y1, 95.8333333, EPSILON);
  137. double y2 = axis.java2DToValue(75.0, dataArea, RectangleEdge.RIGHT);
  138. assertEquals(y2, 95.8333333, EPSILON);
  139. double x1 = axis.java2DToValue(75.0, dataArea, RectangleEdge.TOP);
  140. assertEquals(x1, 58.125, EPSILON);
  141. double x2 = axis.java2DToValue(75.0, dataArea, RectangleEdge.BOTTOM);
  142. assertEquals(x2, 58.125, EPSILON);
  143. axis.setInverted(true);
  144. double y3 = axis.java2DToValue(75.0, dataArea, RectangleEdge.LEFT);
  145. assertEquals(y3, 54.1666667, EPSILON);
  146. double y4 = axis.java2DToValue(75.0, dataArea, RectangleEdge.RIGHT);
  147. assertEquals(y4, 54.1666667, EPSILON);
  148. double x3 = axis.java2DToValue(75.0, dataArea, RectangleEdge.TOP);
  149. assertEquals(x3, 91.875, EPSILON);
  150. double x4 = axis.java2DToValue(75.0, dataArea, RectangleEdge.BOTTOM);
  151. assertEquals(x4, 91.875, EPSILON);
  152. }
  153. /**
  154. * Serialize an instance, restore it, and check for equality.
  155. */
  156. @Test
  157. public void testSerialization() {
  158. NumberAxis a1 = new NumberAxis("Test Axis");
  159. NumberAxis a2 = (NumberAxis) TestUtilities.serialised(a1);
  160. assertEquals(a1, a2);
  161. }
  162. /**
  163. * A simple test for the auto-range calculation looking at a
  164. * NumberAxis used as the range axis for a CategoryPlot.
  165. */
  166. @Test
  167. public void testAutoRange1() {
  168. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  169. dataset.setValue(100.0, "Row 1", "Column 1");
  170. dataset.setValue(200.0, "Row 1", "Column 2");
  171. JFreeChart chart = ChartFactory.createBarChart("Test", "Categories",
  172. "Value", dataset);
  173. CategoryPlot plot = (CategoryPlot) chart.getPlot();
  174. NumberAxis axis = (NumberAxis) plot.getRangeAxis();
  175. assertEquals(axis.getLowerBound(), 0.0, EPSILON);
  176. assertEquals(axis.getUpperBound(), 210.0, EPSILON);
  177. }
  178. /**
  179. * A simple test for the auto-range calculation looking at a
  180. * NumberAxis used as the range axis for a CategoryPlot. In this
  181. * case, the 'autoRangeIncludesZero' flag is set to false.
  182. */
  183. @Test
  184. public void testAutoRange2() {
  185. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  186. dataset.setValue(100.0, "Row 1", "Column 1");
  187. dataset.setValue(200.0, "Row 1", "Column 2");
  188. JFreeChart chart = ChartFactory.createLineChart("Test", "Categories",
  189. "Value", dataset, PlotOrientation.VERTICAL, false, false,
  190. false);
  191. CategoryPlot plot = (CategoryPlot) chart.getPlot();
  192. NumberAxis axis = (NumberAxis) plot.getRangeAxis();
  193. axis.setAutoRangeIncludesZero(false);
  194. assertEquals(axis.getLowerBound(), 95.0, EPSILON);
  195. assertEquals(axis.getUpperBound(), 205.0, EPSILON);
  196. }
  197. /**
  198. * A simple test for the auto-range calculation looking at a
  199. * NumberAxis used as the range axis for a CategoryPlot. In this
  200. * case, the 'autoRangeIncludesZero' flag is set to false AND the
  201. * original dataset is replaced with a new dataset.
  202. */
  203. @Test
  204. public void testAutoRange3() {
  205. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  206. dataset.setValue(100.0, "Row 1", "Column 1");
  207. dataset.setValue(200.0, "Row 1", "Column 2");
  208. JFreeChart chart = ChartFactory.createLineChart("Test", "Categories",
  209. "Value", dataset, PlotOrientation.VERTICAL, false, false,
  210. false);
  211. CategoryPlot plot = (CategoryPlot) chart.getPlot();
  212. NumberAxis axis = (NumberAxis) plot.getRangeAxis();
  213. axis.setAutoRangeIncludesZero(false);
  214. assertEquals(axis.getLowerBound(), 95.0, EPSILON);
  215. assertEquals(axis.getUpperBound(), 205.0, EPSILON);
  216. // now replacing the dataset should update the axis range...
  217. DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
  218. dataset2.setValue(900.0, "Row 1", "Column 1");
  219. dataset2.setValue(1000.0, "Row 1", "Column 2");
  220. plot.setDataset(dataset2);
  221. assertEquals(axis.getLowerBound(), 895.0, EPSILON);
  222. assertEquals(axis.getUpperBound(), 1005.0, EPSILON);
  223. }
  224. /**
  225. * A check for the interaction between the 'autoRangeIncludesZero' flag
  226. * and the base setting in the BarRenderer.
  227. */
  228. @Test
  229. public void testAutoRange4() {
  230. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  231. dataset.setValue(100.0, "Row 1", "Column 1");
  232. dataset.setValue(200.0, "Row 1", "Column 2");
  233. JFreeChart chart = ChartFactory.createBarChart("Test", "Categories",
  234. "Value", dataset, PlotOrientation.VERTICAL, false, false,
  235. false);
  236. CategoryPlot plot = (CategoryPlot) chart.getPlot();
  237. NumberAxis axis = (NumberAxis) plot.getRangeAxis();
  238. axis.setAutoRangeIncludesZero(false);
  239. BarRenderer br = (BarRenderer) plot.getRenderer();
  240. br.setIncludeBaseInRange(false);
  241. assertEquals(95.0, axis.getLowerBound(), EPSILON);
  242. assertEquals(205.0, axis.getUpperBound(), EPSILON);
  243. br.setIncludeBaseInRange(true);
  244. assertEquals(0.0, axis.getLowerBound(), EPSILON);
  245. assertEquals(210.0, axis.getUpperBound(), EPSILON);
  246. axis.setAutoRangeIncludesZero(true);
  247. assertEquals(0.0, axis.getLowerBound(), EPSILON);
  248. assertEquals(210.0, axis.getUpperBound(), EPSILON);
  249. br.setIncludeBaseInRange(true);
  250. assertEquals(0.0, axis.getLowerBound(), EPSILON);
  251. assertEquals(210.0, axis.getUpperBound(), EPSILON);
  252. // now replacing the dataset should update the axis range...
  253. DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
  254. dataset2.setValue(900.0, "Row 1", "Column 1");
  255. dataset2.setValue(1000.0, "Row 1", "Column 2");
  256. plot.setDataset(dataset2);
  257. assertEquals(0.0, axis.getLowerBound(), EPSILON);
  258. assertEquals(1050.0, axis.getUpperBound(), EPSILON);
  259. br.setIncludeBaseInRange(false);
  260. assertEquals(0.0, axis.getLowerBound(), EPSILON);
  261. assertEquals(1050.0, axis.getUpperBound(), EPSILON);
  262. axis.setAutoRangeIncludesZero(false);
  263. assertEquals(895.0, axis.getLowerBound(), EPSILON);
  264. assertEquals(1005.0, axis.getUpperBound(), EPSILON);
  265. }
  266. /**
  267. * Checks that the auto-range for the domain axis on an XYPlot is
  268. * working as expected.
  269. */
  270. @Test
  271. public void testXYAutoRange1() {
  272. XYSeries series = new XYSeries("Series 1");
  273. series.add(1.0, 1.0);
  274. series.add(2.0, 2.0);
  275. series.add(3.0, 3.0);
  276. XYSeriesCollection dataset = new XYSeriesCollection();
  277. dataset.addSeries(series);
  278. JFreeChart chart = ChartFactory.createScatterPlot("Test", "X", "Y",
  279. dataset);
  280. XYPlot plot = (XYPlot) chart.getPlot();
  281. NumberAxis axis = (NumberAxis) plot.getDomainAxis();
  282. axis.setAutoRangeIncludesZero(false);
  283. assertEquals(0.9, axis.getLowerBound(), EPSILON);
  284. assertEquals(3.1, axis.getUpperBound(), EPSILON);
  285. }
  286. /**
  287. * Checks that the auto-range for the range axis on an XYPlot is
  288. * working as expected.
  289. */
  290. @Test
  291. public void testXYAutoRange2() {
  292. XYSeries series = new XYSeries("Series 1");
  293. series.add(1.0, 1.0);
  294. series.add(2.0, 2.0);
  295. series.add(3.0, 3.0);
  296. XYSeriesCollection dataset = new XYSeriesCollection();
  297. dataset.addSeries(series);
  298. JFreeChart chart = ChartFactory.createScatterPlot("Test", "X", "Y",
  299. dataset);
  300. XYPlot plot = (XYPlot) chart.getPlot();
  301. NumberAxis axis = (NumberAxis) plot.getRangeAxis();
  302. axis.setAutoRangeIncludesZero(false);
  303. assertEquals(0.9, axis.getLowerBound(), EPSILON);
  304. assertEquals(3.1, axis.getUpperBound(), EPSILON);
  305. }
  306. // /**
  307. // * Some checks for the setRangeType() method.
  308. // */
  309. // public void testSetRangeType() {
  310. //
  311. // NumberAxis axis = new NumberAxis("X");
  312. // axis.setRangeType(RangeType.POSITIVE);
  313. // assertEquals(RangeType.POSITIVE, axis.getRangeType());
  314. //
  315. // // test a change to RangeType.POSITIVE
  316. // axis.setRangeType(RangeType.FULL);
  317. // axis.setRange(-5.0, 5.0);
  318. // axis.setRangeType(RangeType.POSITIVE);
  319. // assertEquals(new Range(0.0, 5.0), axis.getRange());
  320. //
  321. // axis.setRangeType(RangeType.FULL);
  322. // axis.setRange(-10.0, -5.0);
  323. // axis.setRangeType(RangeType.POSITIVE);
  324. // assertEquals(new Range(0.0, axis.getAutoRangeMinimumSize()),
  325. // axis.getRange());
  326. //
  327. // // test a change to RangeType.NEGATIVE
  328. // axis.setRangeType(RangeType.FULL);
  329. // axis.setRange(-5.0, 5.0);
  330. // axis.setRangeType(RangeType.NEGATIVE);
  331. // assertEquals(new Range(-5.0, 0.0), axis.getRange());
  332. //
  333. // axis.setRangeType(RangeType.FULL);
  334. // axis.setRange(5.0, 10.0);
  335. // axis.setRangeType(RangeType.NEGATIVE);
  336. // assertEquals(new Range(-axis.getAutoRangeMinimumSize(), 0.0),
  337. // axis.getRange());
  338. //
  339. // // try null
  340. // boolean pass = false;
  341. // try {
  342. // axis.setRangeType(null);
  343. // }
  344. // catch (IllegalArgumentException e) {
  345. // pass = true;
  346. // }
  347. // assertTrue(pass);
  348. // }
  349. /**
  350. * Some checks for the setLowerBound() method.
  351. */
  352. @Test
  353. public void testSetLowerBound() {
  354. NumberAxis axis = new NumberAxis("X");
  355. axis.setRange(0.0, 10.0);
  356. axis.setLowerBound(5.0);
  357. assertEquals(5.0, axis.getLowerBound(), EPSILON);
  358. axis.setLowerBound(10.0);
  359. assertEquals(10.0, axis.getLowerBound(), EPSILON);
  360. assertEquals(11.0, axis.getUpperBound(), EPSILON);
  361. //axis.setRangeType(RangeType.POSITIVE);
  362. //axis.setLowerBound(-5.0);
  363. //assertEquals(0.0, axis.getLowerBound(), EPSILON);
  364. }
  365. }