I have been working on a project that requires a bar graph to be populated with price results. The chart displays the number of items within a given price range. For instance, if on amazon there are 9 items within the price range of $0-$10 the x-axis would display $0-$10 and the y-axis would be populated with a value of 9.
My bar graph has 8 bars, all with similar price ranges. $0-$10, $10-$20, $20-$30... etc.
My question is this: What is the best way to define those individual points? There is no common price range between these items, so the x-axis cannot be static numbers. They must be dynamically calculated within the range of results.
As such, currently I am creating the x-axis points as follows:
I take the lowest result:
@numbers[0] = results[0];
And I take the highest result:
@numbers[8] = results[-1];
Then I find the median of the two: @numbers[4] = (@numbers[0]+@numbers[8])/2;
I then repeat the process 6 more times
@numbers[2] = (@numbers[0]+@numbers[4])/2; @numbers[6] = (@numbers[4]+@numbers[8])/2; @numbers[1] = (@numbers[0]+@numbers[2])/2; @numbers[3] = (@numbers[2]+@numbers[4])/2; @numbers[5] = (@numbers[4]+@numbers[6])/2; @numbers[7] = (@numbers[6]+@numbers[8])/2;
This gives me the results I need, but it seems awfully repetitive and I would imagine there is a better way.
I tried creating a loop, but I could not write it in a less verbose manner.
Is there a quicker way to do this, or perhaps something more along the lines of DRY?