Created
May 30, 2015 05:09
-
-
Save Mortimerp9/db39445468d639c640d8 to your computer and use it in GitHub Desktop.
Scales in javascript - d3 inspired
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
module.exports = (function() { | |
var linear = function(domain, range) { | |
var d0 = domain[0], r0 = range[0], multipler = ( range[1] - r0 ) / ( domain[1] - d0 ); | |
return function ( num ) { | |
return r0 + ( ( num - d0 ) * multipler ); | |
}; | |
} | |
var log = function(domain, range, base) { | |
var rBase = base || 10; | |
function logVal(v) { | |
if(v === 0) { | |
return 0; | |
} else { | |
return Math.log(v) / Math.log(rBase); | |
} | |
} | |
var linearScale = linear([logVal(domain[0]), logVal(domain[1])], range); | |
return function(num) { | |
return linearScale(logVal(num)); | |
}; | |
} | |
return { | |
linear: linear, | |
log: log | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment