-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathhorizon.js
More file actions
220 lines (182 loc) · 6.32 KB
/
horizon.js
File metadata and controls
220 lines (182 loc) · 6.32 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
cubism_contextPrototype.horizon = function() {
var context = this,
mode = "offset",
buffer = document.createElement("canvas"),
width = buffer.width = context.size(),
height = buffer.height = 30,
scale = d3.scale.linear().interpolate(d3.interpolateRound),
scale_neg = d3.scale.linear().interpolate(d3.interpolateRound),
metric = cubism_identity,
extent = null,
title = cubism_identity,
format = d3.format(".2s"),
colors = ["#08519c","#3182bd","#6baed6","#bdd7e7","#bae4b3","#74c476","#31a354","#006d2c"];
function horizon(selection) {
selection
.on("mousemove.horizon", function() { context.focus(Math.round(d3.mouse(this)[0])); })
.on("mouseout.horizon", function() { context.focus(null); });
selection.append("canvas")
.attr("width", width)
.attr("height", height);
selection.append("span")
.attr("class", "title")
.text(title);
selection.append("span")
.attr("class", "value");
selection.each(function(d, i) {
var that = this,
id = ++cubism_id,
metric_ = typeof metric === "function" ? metric.call(that, d, i) : metric,
colors_ = typeof colors === "function" ? colors.call(that, d, i) : colors,
extent_ = typeof extent === "function" ? extent.call(that, d, i) : extent,
start = -Infinity,
step = context.step(),
canvas = d3.select(that).select("canvas"),
span = d3.select(that).select(".value"),
max_,
m = colors_.length >> 1,
ready;
canvas.datum({id: id, metric: metric_});
canvas = canvas.node().getContext("2d");
function change(start1, stop) {
canvas.save();
// compute the new extent and ready flag
var extent = metric_.extent();
ready = extent.every(isFinite);
if (extent_ != null) extent = extent_;
// if this is an update (with no extent change), copy old values!
var i0 = 0, max = Math.max(-extent[0], extent[1]);
if (this === context) {
if (max == max_) {
i0 = width - cubism_metricOverlap;
var dx = (start1 - start) / step;
if (dx < width) {
var canvas0 = buffer.getContext("2d");
canvas0.clearRect(0, 0, width, height);
canvas0.drawImage(canvas.canvas, dx, 0, width - dx, height, 0, 0, width - dx, height);
canvas.clearRect(0, 0, width, height);
canvas.drawImage(canvas0.canvas, 0, 0);
}
}
start = start1;
}
// update the domain
max_ = max;
scale.domain([0, extent[1]]);
scale_neg.domain([0, -extent[0]]);
// clear for the new data
canvas.clearRect(i0, 0, width - i0, height);
// record whether there are negative values to display
var negative;
// positive bands
for (var j = 0; j < m; ++j) {
canvas.fillStyle = colors_[m + j];
// Adjust the range based on the current band index.
var y0 = (j - m + 1) * height;
scale.range([m * height + y0, y0]);
y0 = scale(0);
for (var i = i0, n = width, y1; i < n; ++i) {
y1 = metric_.valueAt(i);
if (y1 <= 0) { negative = true; continue; }
if (y1 === undefined) continue;
canvas.fillRect(i, y1 = scale(y1), 1, y0 - y1);
}
}
if (negative) {
// enable offset mode
if (mode === "offset") {
canvas.translate(0, height);
canvas.scale(1, -1);
}
// negative bands
for (var j = 0; j < m; ++j) {
canvas.fillStyle = colors_[m - 1 - j];
// Adjust the range based on the current band index.
var y0 = (j - m + 1) * height;
scale_neg.range([m * height + y0, y0]);
y0 = scale_neg(0);
for (var i = i0, n = width, y1; i < n; ++i) {
y1 = metric_.valueAt(i);
if (y1 >= 0) continue;
canvas.fillRect(i, scale_neg(-y1), 1, y0 - scale_neg(-y1));
}
}
}
canvas.restore();
}
function focus(i) {
if (i == null) i = width - 1;
var value = metric_.valueAt(i);
span.datum(value).text(isNaN(value) ? null : format);
}
// Update the chart when the context changes.
context.on("change.horizon-" + id, change);
context.on("focus.horizon-" + id, focus);
// Display the first metric change immediately,
// but defer subsequent updates to the canvas change.
// Note that someone still needs to listen to the metric,
// so that it continues to update automatically.
metric_.on("change.horizon-" + id, function(start, stop) {
change(start, stop), focus();
if (ready) metric_.on("change.horizon-" + id, cubism_identity);
});
});
}
horizon.remove = function(selection) {
selection
.on("mousemove.horizon", null)
.on("mouseout.horizon", null);
selection.selectAll("canvas")
.each(remove)
.remove();
selection.selectAll(".title,.value")
.remove();
function remove(d) {
d.metric.on("change.horizon-" + d.id, null);
context.on("change.horizon-" + d.id, null);
context.on("focus.horizon-" + d.id, null);
}
};
horizon.mode = function(_) {
if (!arguments.length) return mode;
mode = _ + "";
return horizon;
};
horizon.height = function(_) {
if (!arguments.length) return height;
buffer.height = height = +_;
return horizon;
};
horizon.metric = function(_) {
if (!arguments.length) return metric;
metric = _;
return horizon;
};
horizon.scale = function(_) {
if (!arguments.length) return scale;
scale = _;
scale_neg = _;
return horizon;
};
horizon.extent = function(_) {
if (!arguments.length) return extent;
extent = _;
return horizon;
};
horizon.title = function(_) {
if (!arguments.length) return title;
title = _;
return horizon;
};
horizon.format = function(_) {
if (!arguments.length) return format;
format = _;
return horizon;
};
horizon.colors = function(_) {
if (!arguments.length) return colors;
colors = _;
return horizon;
};
return horizon;
};