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