View Javadoc
1   package org.opentrafficsim.graphs;
2   
3   import org.djunits.value.vdouble.scalar.DoubleScalar;
4   
5   /**
6    * Definition of one axis for a ContourPlot.
7    * <p>
8    * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9    * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
10   * <p>
11   * $LastChangedDate: 2015-08-30 00:16:51 +0200 (Sun, 30 Aug 2015) $, @version $Revision: 1329 $, by $Author: averbraeck $,
12   * initial version Jul 28, 2014 <br>
13   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
14   */
15  class Axis
16  {
17      /** Lowest value along this axis. */
18      private final DoubleScalar<?> minimumValue;
19  
20      /** Highest value along this axis. */
21      private DoubleScalar<?> maximumValue;
22  
23      /** Aggregation values along this axis (all values must be an integer multiple of the first value). */
24      private final double[] granularities;
25  
26      /** Current aggregation value (must be one of the values in granularities). */
27      private double currentGranularity;
28  
29      /** Name to describe the axis and to name the pop up menu that changes the current granularity. */
30      private final String name;
31  
32      /** Name to identify this axis. */
33      private final String shortName;
34  
35      /** Format for rendering a value along this axis. */
36      private String format;
37  
38      /**
39       * Create a new AxisDefinition.
40       * @param minimumValue DoubleScalar; the minimum value along this axis
41       * @param maximumValue DoubleScalar; the maximum value along this axis
42       * @param granularities double[]; the aggregation values along this axis (all values must be an integer multiple of the
43       *            first value)
44       * @param initialGranularity double; initial aggregation value (must be one of the values in granularities)
45       * @param name String; the name to describe the axis and to name the pop up menu that changes the current granularity
46       * @param shortName String; the name identifying this axis for use in a menu
47       * @param format String; format string for rendering a value along this axis
48       */
49      public Axis(final DoubleScalar<?> minimumValue, final DoubleScalar<?> maximumValue, final double[] granularities,
50          final double initialGranularity, final String name, final String shortName, final String format)
51      {
52          this.minimumValue = minimumValue;
53          this.setMaximumValue(maximumValue);
54          this.granularities = granularities;
55          if (null != granularities)
56          {
57              this.setCurrentGranularity(initialGranularity);
58          }
59          this.name = name;
60          this.shortName = shortName;
61          this.format = format;
62      }
63  
64      /**
65       * Compute the floating point bin number for a value.
66       * @param value DoubleScalar; the value
67       * @return double; the bin that belongs to the value
68       */
69      public double getRelativeBin(final DoubleScalar<?> value)
70      {
71          return (value.getSI() - this.getMinimumValue().getSI()) / this.getGranularities()[0];
72      }
73  
74      /**
75       * Adjust (increase) the range of this AxisDefinition.
76       * @param newMaximum DoubleScalar; the new maximum value of the axis
77       */
78      public void adjustMaximumValue(final DoubleScalar<?> newMaximum)
79      {
80          // System.out.println("extending axis " + this.name + " from " + this.maximumValue + " to " + newMaximum);
81          this.setMaximumValue(newMaximum);
82      }
83  
84      /**
85       * Return the value for an aggregated bin number.
86       * @param aggregatedBin Integer; the number of a bin
87       * @return Double; the value corresponding to the center of aggregateBin
88       */
89      public double getValue(final int aggregatedBin)
90      {
91          return this.getMinimumValue().getSI() + 1.0 * aggregatedBin * this.getCurrentGranularity();
92      }
93  
94      /**
95       * @return Integer; the number of bins along this axis
96       */
97      public int getAggregatedBinCount()
98      {
99          return (int) Math.ceil((this.getMaximumValue().getSI() - this.getMinimumValue().getSI())
100             / this.getCurrentGranularity());
101     }
102 
103     /**
104      * @return Integer; the number of aggregated bins along this axis
105      */
106     public int getBinCount()
107     {
108         return (int) Math.ceil((this.getMaximumValue().getSI() - this.getMinimumValue().getSI())
109             / this.getGranularities()[0]);
110     }
111 
112     /**
113      * Get the granularity of this axis.
114      * @return double; the granularity of this axis
115      */
116     public double getCurrentGranularity()
117     {
118         return this.currentGranularity;
119     }
120 
121     /**
122      * Change the granularity for this axis. <br>
123      * The new value must be present in the granularities.
124      * @param newGranularity double; the new value for the granularity of this axis
125      */
126     public void setCurrentGranularity(final double newGranularity)
127     {
128         for (double g : this.getGranularities())
129         {
130             if (g == newGranularity)
131             {
132                 this.currentGranularity = newGranularity;
133                 return;
134             }
135         }
136         throw new RuntimeException("Illegal granularity " + newGranularity);
137     }
138 
139     /**
140      * Get the maximum value of this axis.
141      * @return DoubleScalar; the current maximum value of this axis
142      */
143     public DoubleScalar<?> getMaximumValue()
144     {
145         return this.maximumValue;
146     }
147 
148     /**
149      * Change the maximum value of this axis. <br>
150      * The maximum value can only be increased.
151      * @param newMaximumValue DoubleScalar; the new maximum value of this axis
152      */
153     public void setMaximumValue(final DoubleScalar<?> newMaximumValue)
154     {
155         if (null != this.maximumValue && newMaximumValue.getSI() < this.maximumValue.getSI())
156         {
157             throw new RuntimeException("maximum value may not be decreased");
158         }
159         this.maximumValue = newMaximumValue;
160     }
161 
162     /**
163      * Get the minimum value of this axis.
164      * @return DoubleScalar; the minimum value of this axis
165      */
166     public DoubleScalar<?> getMinimumValue()
167     {
168         return this.minimumValue;
169     }
170 
171     /**
172      * Retrieve the possible granularities for this Axis.
173      * @return granularities
174      */
175     public final double[] getGranularities()
176     {
177         return this.granularities;
178     }
179 
180     /**
181      * Retrieve the format for displaying values along this Axis.
182      * @return format
183      */
184     public String getFormat()
185     {
186         return this.format;
187     }
188 
189     /**
190      * Retrieve the short name for this Axis.
191      * @return String; the short name for this Axis
192      */
193     public String getShortName()
194     {
195         return this.shortName;
196     }
197 
198     /**
199      * Retrieve the name of this Axis.
200      * @return String; the name of this Axis
201      */
202     public String getName()
203     {
204         return this.name;
205     }
206 
207 }