source: Dev/trunk/d3/src/scale/pow.js @ 76

Last change on this file since 76 was 76, checked in by fpvanagthoven, 14 years ago

d3

File size: 1.1 KB
Line 
1d3.scale.pow = function() {
2  var linear = d3.scale.linear(),
3      exponent = 1,
4      powp = Number,
5      powb = powp;
6
7  function scale(x) {
8    return linear(powp(x));
9  }
10
11  scale.invert = function(x) {
12    return powb(linear.invert(x));
13  };
14
15  scale.domain = function(x) {
16    if (!arguments.length) return linear.domain().map(powb);
17    powp = d3_scale_powPow(exponent);
18    powb = d3_scale_powPow(1 / exponent);
19    linear.domain(x.map(powp));
20    return scale;
21  };
22
23  scale.ticks = function(m) {
24    return d3_scale_linearTicks(scale.domain(), m);
25  };
26
27  scale.tickFormat = function(m) {
28    return d3_scale_linearTickFormat(scale.domain(), m);
29  };
30
31  scale.nice = function() {
32    return scale.domain(d3_scale_nice(scale.domain(), d3_scale_linearNice));
33  };
34
35  scale.exponent = function(x) {
36    if (!arguments.length) return exponent;
37    var domain = scale.domain();
38    exponent = x;
39    return scale.domain(domain);
40  };
41
42  return d3_scale_linearRebind(scale, linear);
43};
44
45function d3_scale_powPow(e) {
46  return function(x) {
47    return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e);
48  };
49}
Note: See TracBrowser for help on using the repository browser.