Ok so I am rendering a partial:
<%= render :partial => "box", :collection => @dashboard.charts %>
In the partial I am organising each “box” into rows of 3. Like this:
if box_counter % 3 == 0 && box_counter != 0
%></div><div class='row'><%
end
which works great.
Here is my problem. I got my maths down but I’m not sure how to translate it into Ruby. Let me explain.
Each Box has a size. Small, Medium and Large. All sizes have been assigned a width. 20%, 30% and 50%. Which adds up to 100%.
A user can have any arrangement of these boxes in each row. What I need to do is check that the boxes in the row add up to 100%. So if its under or over 100% I will get the difference, divide it by 3 and add or remove that percent to each box therefor making each box add up to 100%.
For example. A user could have a combination of a Large Box, Large Box and then a Small Box. This adds up to 120%. So we get the difference, which is 20%, divide it by 3 = 6.6%, and then remove it from the 3 boxes (cause we went over 100%). So that becomes: LargeBox = 43.4%, LargeBox = 43.4%, Small Box = 13.4%. Which will add up to 100%. (or close to which is fine).
I’ve been working on this with Ruby and just don’t know how to get my maths into the loop or something. Really stuck here guys. I would appreciate the help.
Cheers.
I don’t know Ruby, but since this is also tagged ‘math’, let me present a slightly different approach to the math that might be easier.
If your sum is 120% (for instance), calculate a ratio of 100%/120% which gives you .833. Then multiply .833 by each of the three individual widths. There’s no need to perform three subtractions of different amounts this way. And 100%/100% = 1.0, so this same process still works correctly in either case.