ValueAxisTest.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. * ValueAxisTest.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. * 13-Aug-2003 : Version 1 (DG);
  38. * 22-Mar-2007 : Extended testEquals() for new field (DG);
  39. * 04-Sep-2012 : Added test3555275() (DG);
  40. *
  41. */
  42. package org.jfree.chart.axis;
  43. import static org.junit.Assert.assertEquals;
  44. import static org.junit.Assert.assertFalse;
  45. import static org.junit.Assert.assertTrue;
  46. import java.awt.BasicStroke;
  47. import java.awt.Color;
  48. import java.awt.Graphics2D;
  49. import java.awt.Stroke;
  50. import java.awt.geom.Rectangle2D;
  51. import java.awt.image.BufferedImage;
  52. import org.jfree.chart.ChartFactory;
  53. import org.jfree.chart.ChartRenderingInfo;
  54. import org.jfree.chart.JFreeChart;
  55. import org.jfree.chart.plot.CategoryPlot;
  56. import org.jfree.chart.plot.PlotOrientation;
  57. import org.jfree.chart.plot.XYPlot;
  58. import org.jfree.data.Range;
  59. import org.jfree.data.category.DefaultCategoryDataset;
  60. import org.jfree.data.xy.XYSeries;
  61. import org.jfree.data.xy.XYSeriesCollection;
  62. import org.jfree.ui.RectangleInsets;
  63. import org.junit.Test;
  64. /**
  65. * Tests for the {@link ValueAxis} class.
  66. */
  67. public class ValueAxisTest {
  68. private static final double EPSILON = 0.000000001;
  69. /**
  70. * Confirm that cloning works.
  71. */
  72. @Test
  73. public void testCloning() throws CloneNotSupportedException {
  74. ValueAxis a1 = new NumberAxis("Test");
  75. ValueAxis a2 = (NumberAxis) a1.clone();
  76. assertTrue(a1 != a2);
  77. assertTrue(a1.getClass() == a2.getClass());
  78. assertTrue(a1.equals(a2));
  79. }
  80. /**
  81. * Confirm that the equals method can distinguish all the required fields.
  82. */
  83. @Test
  84. public void testEquals() {
  85. NumberAxis a1 = new NumberAxis("Test");
  86. NumberAxis a2 = new NumberAxis("Test");
  87. assertTrue(a1.equals(a2));
  88. // axis line visible flag...
  89. a1.setAxisLineVisible(false);
  90. assertFalse(a1.equals(a2));
  91. a2.setAxisLineVisible(false);
  92. assertTrue(a1.equals(a2));
  93. // positiveArrowVisible;
  94. a1.setPositiveArrowVisible(true);
  95. assertFalse(a1.equals(a2));
  96. a2.setPositiveArrowVisible(true);
  97. assertTrue(a1.equals(a2));
  98. // negativeArrowVisible;
  99. a1.setNegativeArrowVisible(true);
  100. assertFalse(a1.equals(a2));
  101. a2.setNegativeArrowVisible(true);
  102. assertTrue(a1.equals(a2));
  103. //private Shape upArrow;
  104. //private Shape downArrow;
  105. //private Shape leftArrow;
  106. //private Shape rightArrow;
  107. // axisLinePaint
  108. a1.setAxisLinePaint(Color.blue);
  109. assertFalse(a1.equals(a2));
  110. a2.setAxisLinePaint(Color.blue);
  111. assertTrue(a1.equals(a2));
  112. // axisLineStroke
  113. Stroke stroke = new BasicStroke(2.0f);
  114. a1.setAxisLineStroke(stroke);
  115. assertFalse(a1.equals(a2));
  116. a2.setAxisLineStroke(stroke);
  117. assertTrue(a1.equals(a2));
  118. // inverted
  119. a1.setInverted(true);
  120. assertFalse(a1.equals(a2));
  121. a2.setInverted(true);
  122. assertTrue(a1.equals(a2));
  123. // range
  124. a1.setRange(new Range(50.0, 75.0));
  125. assertFalse(a1.equals(a2));
  126. a2.setRange(new Range(50.0, 75.0));
  127. assertTrue(a1.equals(a2));
  128. // autoRange
  129. a1.setAutoRange(true);
  130. assertFalse(a1.equals(a2));
  131. a2.setAutoRange(true);
  132. assertTrue(a1.equals(a2));
  133. // autoRangeMinimumSize
  134. a1.setAutoRangeMinimumSize(3.33);
  135. assertFalse(a1.equals(a2));
  136. a2.setAutoRangeMinimumSize(3.33);
  137. assertTrue(a1.equals(a2));
  138. a1.setDefaultAutoRange(new Range(1.2, 3.4));
  139. assertFalse(a1.equals(a2));
  140. a2.setDefaultAutoRange(new Range(1.2, 3.4));
  141. assertTrue(a1.equals(a2));
  142. // upperMargin
  143. a1.setUpperMargin(0.09);
  144. assertFalse(a1.equals(a2));
  145. a2.setUpperMargin(0.09);
  146. assertTrue(a1.equals(a2));
  147. // lowerMargin
  148. a1.setLowerMargin(0.09);
  149. assertFalse(a1.equals(a2));
  150. a2.setLowerMargin(0.09);
  151. assertTrue(a1.equals(a2));
  152. //private double fixedAutoRange;
  153. a1.setFixedAutoRange(50.0);
  154. assertFalse(a1.equals(a2));
  155. a2.setFixedAutoRange(50.0);
  156. assertTrue(a1.equals(a2));
  157. //private boolean autoTickUnitSelection;
  158. a1.setAutoTickUnitSelection(false);
  159. assertFalse(a1.equals(a2));
  160. a2.setAutoTickUnitSelection(false);
  161. assertTrue(a1.equals(a2));
  162. //private TickUnits standardTickUnits;
  163. a1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
  164. assertFalse(a1.equals(a2));
  165. a2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
  166. assertTrue(a1.equals(a2));
  167. // verticalTickLabels
  168. a1.setVerticalTickLabels(true);
  169. assertFalse(a1.equals(a2));
  170. a2.setVerticalTickLabels(true);
  171. assertTrue(a1.equals(a2));
  172. //private int autoTickIndex;
  173. //protected double reservedForTickLabels;
  174. //protected double reservedForAxisLabel;
  175. }
  176. /**
  177. * Tests the the lower and upper margin settings produce the expected
  178. * results.
  179. */
  180. @Test
  181. public void testAxisMargins() {
  182. XYSeries series = new XYSeries("S1");
  183. series.add(100.0, 1.1);
  184. series.add(200.0, 2.2);
  185. XYSeriesCollection dataset = new XYSeriesCollection(series);
  186. dataset.setIntervalWidth(0.0);
  187. JFreeChart chart = ChartFactory.createScatterPlot("Title", "X", "Y",
  188. dataset);
  189. ValueAxis domainAxis = ((XYPlot) chart.getPlot()).getDomainAxis();
  190. Range r = domainAxis.getRange();
  191. assertEquals(110.0, r.getLength(), EPSILON);
  192. domainAxis.setLowerMargin(0.10);
  193. domainAxis.setUpperMargin(0.10);
  194. r = domainAxis.getRange();
  195. assertEquals(120.0, r.getLength(), EPSILON);
  196. }
  197. /**
  198. * A test for bug 3555275 (where the fixed axis space is calculated
  199. * incorrectly).
  200. */
  201. @Test
  202. public void test3555275() {
  203. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  204. JFreeChart chart = ChartFactory.createLineChart("Title", "X", "Y",
  205. dataset, PlotOrientation.VERTICAL, true, false, false);
  206. CategoryPlot plot = (CategoryPlot) chart.getPlot();
  207. plot.setInsets(RectangleInsets.ZERO_INSETS);
  208. plot.setAxisOffset(RectangleInsets.ZERO_INSETS);
  209. ValueAxis yAxis = plot.getRangeAxis();
  210. yAxis.setFixedDimension(100.0);
  211. BufferedImage image = new BufferedImage(500, 300,
  212. BufferedImage.TYPE_INT_RGB);
  213. Graphics2D g2 = image.createGraphics();
  214. ChartRenderingInfo info = new ChartRenderingInfo();
  215. chart.draw(g2, new Rectangle2D.Double(0, 0, 500, 300), info);
  216. g2.dispose();
  217. Rectangle2D rect = info.getPlotInfo().getDataArea();
  218. double x = rect.getMinX();
  219. assertEquals(100.0, x, 1.0);
  220. }
  221. }