It is often necessary to group numerical data into categories. The range of the data is divided into a number of
intervals, where each interval becomes a category in a numerical scale. This type of numerical scale is implemented
by the NumericalScale class. This class inherits from
CategoricalScale, but provides some additional
functionality.
Constructing Numerical Scales
The NumericalScale class has four constructors. They come in two pairs, each pair offering one
way of defining the intervals that make up the scale.
The first constructor takes one argument: a Double array that contains the
boundaries of the intervals. The values in this array must be in ascending order, or an
ArgumentException will be thrown.
| C# |
Copy
|
double[] bounds = new double[] {50, 62, 74, 88, 100};
NumericalScale scale1 = new NumericalScale(bounds);
|
| Visual Basic |
Copy
|
Dim bounds As Double() = New Double() {50, 62, 74, 88, 100}
Dim scale1 As NumericalScale = New NumericalScale(bounds)
|
The second constructor also has a Double array as its first argument, but has
one additional argument: a SpecialBins value that specifies which special intervals to include in the
scale.
The possible values are as follows:
Values of the SpecialBins enumeration.
Name | TH |
|---|
None | No special intervals are included. |
BelowMinimum | There is a special interval for values below the scale's minimum value. |
AboveMaximum | There is a special interval for values above the scale's maximum value. |
OutOfRange | There is a special interval for values that are outside the scale's range. |
Missing | There is a special interval for missing values. |
If BelowMinimum is included, an interval with lower bound Double.NegativeInfinity is
inserted before all other intervals. If AboveMaximum is included, an interval with upper bound
Double.PositiveInfinity is added at the end. The following creates a scale with the same boundaries as
above, but with an extra interval to hold values less than 50:
| C# | Copy |
|---|
double[] bounds = new double[] {50, 62, 74, 88, 100};
NumericalScale scale2 = new NumericalScale(bounds, SpecialBins.BelowMinimum);
|
| Visual Basic | Copy |
|---|
Dim bounds As Double() = New Double() {50, 62, 74, 88, 100}
Dim scale2 As NumericalScale = New NumericalScale(bounds, SpecialBins.BelowMinimum)
|
The third constructor takes three arguments. The first two are the lower bound of the first interval, and the
upper bound of the last interval. The third argument is the total number of intervals. This creates a scale with the
specified number of intervals that are all equal in width. The fourth constructor has one additional argument: a
SpecialBins value that indicates which special values should be
tabulated in addition to those within the specified interval.
The code below creates a scale with five intervals for values between 50 and 100:
| C# | Copy |
|---|
NumericalScale scale3 = new NumericalScale(50, 100, 5);
|
| Visual Basic | Copy |
|---|
Dim scale3 As NumericalScale = New NumericalScale(50, 100, 5)
|
Properties and Methods
The Count property returns the number of intervals in
the scale. The IsOrdered property indicates whether
the scale is ordered or unordered. It always returns true.
The GetBounds()()()() method returns a Double array containing the boundaries of the intervals. If the BelowMinimum or
AboveMaximum intervals were included, the return value includes these intervals as well.
The GetLowerBound(Int32) and GetUpperBound(Int32) methods take one argument and return the
lower and upper bound of the interval with the specified index.
| C# |
Copy
|
Console.WriteLine(scale3.GetLowerBound(2));
Console.WriteLine(scale3.GetUpperBound(2));
|
| Visual Basic |
Copy
|
Console.WriteLine(scale3.GetLowerBound(2)) ' Prints '70'
Console.WriteLine(scale3.GetUpperBound(2)) ' Prints '80'
|
The GetCaption method returns the text label of
the level at the specified level index. The GetCaptions()()()() method returns a string array containing the
captions for each level.
The GetEnumerator()()()() method returns an
IEnumerator object that can be used to iterate through the levels of the scale.
Mapping Values to Intervals
Numerical scales convert numbers to intervals. The Map()()()() method provides this functionality.
This method is overloaded. The first overload takes any object as its only argument. This object is converted to a
number using the Convert.ToDouble method. If this conversion succeeds, the index of the interval
containing the number is returned. If the number is outside the scale, -1 is returned.
The second overload takes an array of objects and returns an integer array of the indexes corresponding to those
objects. Each of these methods takes an optional IFormatProvider argument that is used to convert the
object value to a number.
The NumericalScale class also provides type-safe overloads. One maps a number to the index of the
interval. The second takes an array and returns an array of indexes.
The example below uses the scale defined earlier. Each line of code prints the same value (1):
| C# |
Copy
|
Console.WriteLine(scale3.Map(63.5));
Console.WriteLine(scale3.Map("63.5"));
IFormatProvider provider = System.Globalization.CultureInfo.CreateSpecificCulture("NL-BE").NumberFormat
Console.WriteLine(scale3.Map("63,5", provider));
|
| Visual Basic |
Copy
|
Console.WriteLine(scale3.Map(63.5))
Console.WriteLine(scale3.Map("63.5"))
Dim provider As IFormatProvider = _
System.Globalization.CultureInfo.CreateSpecificCulture("NL-BE").NumberFormat
Console.WriteLine(scale3.Map("63,5", provider))
|
In the last line, the value is provided in a format that uses a comma as the decimal separator. The right
NumberFormat is required to make the result correct.