-
Notifications
You must be signed in to change notification settings - Fork 7
/
kookyArrow.js
76 lines (58 loc) · 1.55 KB
/
kookyArrow.js
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
function kookyArrow() {
var steps = 5,
mean = 0,
deviation = 100,
interpolate = 'basis',
xValue = function(d) { return d[0]; },
yValue = function(d) { return d[1]; };
function render(data) {
if(data.length < 2) return;
data = data.map(function(d, i) {
return [xValue.call(data, d, i), yValue.call(data, d, i)];
});
var rng = d3.random.normal(mean, deviation);
var line = d3.svg.line().interpolate(interpolate);
var points = [];
points.push(data[0]);
data.slice(1).forEach(function(d1,i) {
var d0 = data[i];
d3.range(steps).slice(1).forEach(function(numerator) {
var cx = d0[0] + (numerator/steps) * (d1[0]-d0[0]);
var cy = d0[1] + (numerator/steps) * (d1[1]-d0[1]);
if(numerator < steps-1) {
cx += rng();
cy += rng();
}
points.push([cx, cy])
})
});
points.push(data[data.length-1]);
return line(points);
}
render.steps = function(_) {
if (!arguments.length) return steps;
steps = _;
return render;
}
render.deviation = function(_) {
if (!arguments.length) return deviation;
deviation = _;
return render;
}
render.interpolate = function(_) {
if (!arguments.length) return interpolate;
interpolate = _;
return render;
}
render.x = function(_) {
if (!arguments.length) return xValue;
xValue = _;
return render;
};
render.y = function(_) {
if (!arguments.length) return yValue;
yValue = _;
return render;
};
return render;
}