How to round numbers down into a range
I have a strange request on a project.
I have an HTML table with 30 columns - each column is displayed as a solid bar with the height set to the value of the column. The max value for the columns is set to 200开发者_Go百科. Each column is dynamically generated and sometimes the values exceed that of 200. ( for instance 680, 340, 210 etc etc).
What I want to know is how do I work this number down to less than 200? This sum would need to be applied to all each of the columns. So, value = x% of 200.
Any ideas?
You need to normalise. Take the maximum value (either the maximum of the data set - which is dynamic and must be calculated, or the maximum possible, which is static) and divide each actual value by this max, and multiply by column height:
renderedHeight = actualValue / max * columnHeight
This is possible using javascript and its Math
functions. Or just a simple if
statement.
if (value > 200){
value = 200;
}
精彩评论