/yscalejs

Y-Scale calculation function in javascript

Primary LanguageJavaScript

yScaleJs

Utility functions to get y-scale (non-negative) of graphs.
yScaleMax : Given max value  this function gives scaled range and number of units
yScale : Given min and max this function returns all scales

Routines would fit y-Scale from 6 to 10 units and starts with 0 or specified min value.

Quick check with node :
node -e "var ys = require('./yscale-1.0.4'); console.log(ys.yScaleMax(237));"
node -e "var ys = require('./yscale-1.0.4'); console.log(ys.yScale(0.2, 1.94));"

Windows users may run quickrun.bat

Examples (yScaleMax):
  > js.yScaleMax(17452);
  [ 2000, 9 ]

  > js.yScaleMax(57452);
  [ 10000, 6 ]

  > js.yScaleMax(5000);
  [ 500, 10 ]

  > js.yScaleMax(1000);
  [ 100, 10 ]

  > js.yScaleMax(23.23);
  [ 2.5, 10 ]

  > js.yScaleMax(999999);
  [ 100000, 10 ]

  It tries to make a best fit for possible ranges.
  Give 329 instead of choosing 100 * 3 or 40 * 9, it fits to 50 * 7.

  > js.yScaleMax(329);
  [ 50, 7 ]

Similarly given value is 5000 instead of choosing 1000 * 5 it tries to make up 500 * 10.

Examples (yScale):
  > js.yScale(0.2, 2.077);
  [ 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25 ]
  
  > js.yScale(0.2, 2.77);
  [ 0, 0.5, 1, 1.5, 2, 2.5, 3 ]

  > js.yScale(19284, 19359);
  [ 19275, 19300, 19325, 19350, 19375 ]

  > js.yScale(0 4391)
  [ 0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500 ]