forked from d3/d3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.js
More file actions
107 lines (90 loc) · 2.76 KB
/
Copy pathlog.js
File metadata and controls
107 lines (90 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import "../format/format";
import "linear";
import "nice";
import "scale";
d3.scale.log = function() {
return d3_scale_log(d3.scale.linear().domain([0, Math.LN10]), 10, d3_scale_logp, d3_scale_powp);
};
function d3_scale_log(linear, base, log, pow) {
function scale(x) {
return linear(log(x));
}
scale.invert = function(x) {
return pow(linear.invert(x));
};
scale.domain = function(x) {
if (!arguments.length) return linear.domain().map(pow);
if (x[0] < 0) log = d3_scale_logn, pow = d3_scale_pown;
else log = d3_scale_logp, pow = d3_scale_powp;
linear.domain(x.map(log));
return scale;
};
scale.base = function(_) {
if (!arguments.length) return base;
base = +_;
return scale;
};
scale.nice = function() {
linear.domain(d3_scale_nice(linear.domain(), d3_scale_logNice(base)));
return scale;
};
scale.ticks = function() {
var extent = d3_scaleExtent(linear.domain()),
ticks = [];
if (extent.every(isFinite)) {
var b = Math.log(base),
i = Math.floor(extent[0] / b),
j = Math.ceil(extent[1] / b),
u = pow(extent[0]),
v = pow(extent[1]),
n = base % 1 ? 2 : base;
if (log === d3_scale_logn) {
ticks.push(-Math.pow(base, -i));
for (; i++ < j;) for (var k = n - 1; k > 0; k--) ticks.push(-Math.pow(base, -i) * k);
} else {
for (; i < j; i++) for (var k = 1; k < n; k++) ticks.push(Math.pow(base, i) * k);
ticks.push(Math.pow(base, i));
}
for (i = 0; ticks[i] < u; i++) {} // strip small values
for (j = ticks.length; ticks[j - 1] > v; j--) {} // strip big values
ticks = ticks.slice(i, j);
}
return ticks;
};
scale.tickFormat = function(n, format) {
if (arguments.length < 2) format = d3_scale_logFormat;
if (!arguments.length) return format;
var b = Math.log(base),
k = Math.max(.1, n / scale.ticks().length),
f = log === d3_scale_logn ? (e = -1e-12, Math.floor) : (e = 1e-12, Math.ceil),
e;
return function(d) {
return d / pow(b * f(log(d) / b + e)) <= k ? format(d) : "";
};
};
scale.copy = function() {
return d3_scale_log(linear.copy(), base, log, pow);
};
return d3_scale_linearRebind(scale, linear);
}
var d3_scale_logFormat = d3.format(".0e");
function d3_scale_logp(x) {
return Math.log(x < 0 ? 0 : x);
}
function d3_scale_powp(x) {
return Math.exp(x);
}
function d3_scale_logn(x) {
return -Math.log(x > 0 ? 0 : -x);
}
function d3_scale_pown(x) {
return -Math.exp(-x);
}
function d3_scale_logNice(base) {
base = Math.log(base);
var nice = {
floor: function(x) { return Math.floor(x / base) * base; },
ceil: function(x) { return Math.ceil(x / base) * base; }
};
return function() { return nice; };
}