~smagoun/whoopsie/whoopsie-lp1017637

« back to all changes in this revision

Viewing changes to backend/stats/static/js/d3/src/scale/log.js

  • Committer: Evan Dandrea
  • Date: 2012-05-09 05:53:45 UTC
  • Revision ID: evan.dandrea@canonical.com-20120509055345-z2j41tmcbf4as5uf
The backend now lives in lp:daisy and the website (errors.ubuntu.com) now lives in lp:errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
d3.scale.log = function() {
2
 
  return d3_scale_log(d3.scale.linear(), d3_scale_logp);
3
 
};
4
 
 
5
 
function d3_scale_log(linear, log) {
6
 
  var pow = log.pow;
7
 
 
8
 
  function scale(x) {
9
 
    return linear(log(x));
10
 
  }
11
 
 
12
 
  scale.invert = function(x) {
13
 
    return pow(linear.invert(x));
14
 
  };
15
 
 
16
 
  scale.domain = function(x) {
17
 
    if (!arguments.length) return linear.domain().map(pow);
18
 
    log = x[0] < 0 ? d3_scale_logn : d3_scale_logp;
19
 
    pow = log.pow;
20
 
    linear.domain(x.map(log));
21
 
    return scale;
22
 
  };
23
 
 
24
 
  scale.nice = function() {
25
 
    linear.domain(d3_scale_nice(linear.domain(), d3_scale_niceDefault));
26
 
    return scale;
27
 
  };
28
 
 
29
 
  scale.ticks = function() {
30
 
    var extent = d3_scaleExtent(linear.domain()),
31
 
        ticks = [];
32
 
    if (extent.every(isFinite)) {
33
 
      var i = Math.floor(extent[0]),
34
 
          j = Math.ceil(extent[1]),
35
 
          u = pow(extent[0]),
36
 
          v = pow(extent[1]);
37
 
      if (log === d3_scale_logn) {
38
 
        ticks.push(pow(i));
39
 
        for (; i++ < j;) for (var k = 9; k > 0; k--) ticks.push(pow(i) * k);
40
 
      } else {
41
 
        for (; i < j; i++) for (var k = 1; k < 10; k++) ticks.push(pow(i) * k);
42
 
        ticks.push(pow(i));
43
 
      }
44
 
      for (i = 0; ticks[i] < u; i++) {} // strip small values
45
 
      for (j = ticks.length; ticks[j - 1] > v; j--) {} // strip big values
46
 
      ticks = ticks.slice(i, j);
47
 
    }
48
 
    return ticks;
49
 
  };
50
 
 
51
 
  scale.tickFormat = function(n, format) {
52
 
    if (arguments.length < 2) format = d3_scale_logFormat;
53
 
    if (arguments.length < 1) return format;
54
 
    var k = n / scale.ticks().length,
55
 
        f = log === d3_scale_logn ? (e = -1e-12, Math.floor) : (e = 1e-12, Math.ceil),
56
 
        e;
57
 
    return function(d) {
58
 
      return d / pow(f(log(d) + e)) < k ? format(d) : "";
59
 
    };
60
 
  };
61
 
 
62
 
  scale.copy = function() {
63
 
    return d3_scale_log(linear.copy(), log);
64
 
  };
65
 
 
66
 
  return d3_scale_linearRebind(scale, linear);
67
 
}
68
 
 
69
 
var d3_scale_logFormat = d3.format(".0e");
70
 
 
71
 
function d3_scale_logp(x) {
72
 
  return Math.log(x < 0 ? 0 : x) / Math.LN10;
73
 
}
74
 
 
75
 
function d3_scale_logn(x) {
76
 
  return -Math.log(x > 0 ? 0 : -x) / Math.LN10;
77
 
}
78
 
 
79
 
d3_scale_logp.pow = function(x) {
80
 
  return Math.pow(10, x);
81
 
};
82
 
 
83
 
d3_scale_logn.pow = function(x) {
84
 
  return -Math.pow(10, -x);
85
 
};