Calculating a Bar Graph Scale

I’m not sure if anyone will ever have an need for this, but just to share something interesting…

This morning I was tasked with coming up with a formula for calculating a bar graph scale, based off of the bar graph data.  The scale had to include 3 to 4 numbers that indicate even portions of a maximum number.  For example, if the maximum number in the bar graph is 95, then I had to come up with 25, 50, 75 and 100 in the scale; if the maximum number in the bar graph is 145, the I had to come up with 50, 100 and 150 in the scale, etc.

Here’s an example of a bar graph with 100 as the maximum number in the scale.  The 4 number scale (not including 0) is on the left side:


bargraph1













Here’s an example of a bar graph with 150 as the maximum number in the scale.  The 3 number scale (not including 0) is on the left side:


bargraph2












The first thing I needed to do was compile a list of numbers.  Since my numbers were coming from thousands of records of data, I’ll fabricate some numbers to use for this demonstration.

'Create a list of Decimals
Dim list As New List(Of Decimal)
list.Add(94.2)
list.Add(64.8)
list.Add(45.4)
list.Add(20.1)

 

After I had the list of numbers, I created a scaleList to store a list of the numbers that would be added to my scale.

 

'Create a list of Integers to store our scale
Dim scaleList As New List(Of Integer)


The next thing I needed to do was figure out what the maximum number in the list was.  So, I added a project reference to the System.Core.dll, then I was able to use LINQ’s Max() generic list extension to get it.

'Get the Max number in the list (using LINQ)
Dim max As Integer = CInt(list.Max())


Then, I had to get the number that is in the “ones” position of the max number.

'The ones variable will store the digit in the ones
'  position of the integer
'
'  Example:  12345  ("5" is in the ones position)
Dim ones As Integer = 0

'Extract number in the ones position
With max.ToString()
    ones = CInt(.Substring(.Length - 1, 1))
End With


Once I had the maximum number, and the number from the “ones” position of the maximum number, I was able to calculate the ceiling for the numbers in the scale.

'Calculate the ceiling by subtracting ones from max
'  and adding 10
'
'  Example:  33
'      (33 - 3) + 10 = 40 (40 is the ceiling)
Dim ceiling As Integer = (max - ones) + 10


The final thing to do was to figure out if the ceiling that I had was evenly divisible for 4 or 3.  If it was, then I could calculate the numbers of the scale, but if it was not, then I needed to adjust my ceiling, and try again.

'The quotient stores the result of dividing
'  the ceiling by 3 or 4
Dim quotient As Integer = 0

'Begin looping until we have a ceiling that
'  is evenly divisible by 4 or 3,then create
'  a scale list of integer.
Do
    If ceiling Mod 4 = 0 Then

        'Example:  If the ceiling is 100,
        '  the(quotient = 25)
        '
        '  quotient = 100 / 4
        quotient = CInt(ceiling / 4)

        'Add numbers to the scale
        '  (not adding 0, which is the min)
        '
        '  Example:  25, 50, 75, 100
        scaleList.Add(quotient)
        scaleList.Add(quotient * 2)
        scaleList.Add(quotient * 3)
        scaleList.Add(quotient * 4)

        Exit Do

    ElseIf ceiling Mod 3 = 0 Then

        'Example:  If the ceiling is 150,
        '  the(quotient = 50)
        '
        '  quotient = 150 / 3
        quotient = CInt(ceiling / 3)

        'Add numbers to the scale (not
        '  adding 0, which is the min)
        '
        '  Example:  50, 100, 150
        scaleList.Add(quotient)
        scaleList.Add(quotient * 2)
        scaleList.Add(quotient * 3)

        Exit Do

    Else
        'If the number is not evenly divisible
        '  by 4 or 3 then add 10 to it.
        '
        '  Example:  70
        '    70 / 4 = 17.5
        '    70 / 3 = 23.333333...
        '    70 + 10 = 80  (add 10, because 70
        '        is not evenly divisible by 4 or 3)
        '
        '    80 / 4 = 20  <-  80 is the number we
        '        want, because 20 is evenly
        '        divisible by 4

        ceiling += 10

    End If

Loop


Now we have our list.  To view the list, write it to the Output Window:

'Loop through each number in the list and write it
'  to the Output Window
For Each i As Integer In scaleList
    Debug.WriteLine(i) : Next


And there it is!  Calculating a Bar Graph scale made easy!

0 comments:

Leave a Reply

Translate

Google-Translate-Chinese (Simplified) BETA Google-Translate-English to French Google-Translate-English to German Google-Translate-English to Italian Google-Translate-English to Japanese BETA Google-Translate-English to Korean BETA Google-Translate-English to Russian BETA Google-Translate-English to Spanish

Tags