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:
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:
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.
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.
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.
Then, I had to get the number that is in the “ones” position of the max number.
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.
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.
Now we have our list. To view the list, write it to the Output Window:
And there it is! Calculating a Bar Graph scale made easy!