Another task for underscore.js – I need to do ‘calibrate’ the data, for displaying it in the chart.
Given: available space: 160 and data: data: [10, 50, 80, 90] I want to get calibratedData: [0, 80, 140, 160].
here’s the algorithm:
- get the peaks for the data,
Min=10,Max = 90. - get the difference between
MinandMax,80. - get the
Unitspace available to1unit of change as160 / 80 = 2 - calibrate each item in data, using the ratio from the previous step as:
d[i] = (d[i]-Min) * Unit, which gives[0, 80, 140, 160].
Wonder if I can accomplish the task using ‘linq’able syntax of underscore.js.
I don’t understand why people downvoted your question, but it was fun to find the answer. Here it is:
You can try it in this JSFiddle.
The key is to use the
map()function in Underscore with this function:This function has access to the min, max and spacePerUnit values in the parent function which makes it very efficient (because it doesn’t do the calculations in every iteration).