-
Notifications
You must be signed in to change notification settings - Fork 12
/
d3.slider.js
289 lines (243 loc) · 6.68 KB
/
d3.slider.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
d3.slider = function module() {
"use strict";
var div, min = 0, max = 100, svg, svgGroup, value, classPrefix, axis,
height=40, rect,
rectHeight = 12,
tickSize = 6,
margin = {top: 25, right: 25, bottom: 15, left: 25},
ticks = 0, tickValues, scale, tickFormat, dragger, width,
range = false,
callbackFn, stepValues, focus;
function slider(selection) {
selection.each(function() {
div = d3.select(this).classed('d3slider', true);
width = parseInt(div.style("width"), 10)-(margin.left
+ margin.right);
value = value || min;
scale = d3.scale.linear().domain([min, max]).range([0, width])
.clamp(true);
// SVG
svg = div.append("svg")
.attr("class", "d3slider-axis")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left +
"," + margin.top + ")");
// Range rect
svg.append("rect")
.attr("class", "d3slider-rect-range")
.attr("width", width)
.attr("height", rectHeight);
// Range rect
if (range) {
svg.append("rect")
.attr("class", "d3slider-rect-value")
.attr("width", scale(value))
.attr("height", rectHeight);
}
// Axis
var axis = d3.svg.axis()
.scale(scale)
.orient("bottom");
if (ticks != 0) {
axis.ticks(ticks);
axis.tickSize(tickSize);
} else if (tickValues) {
axis.tickValues(tickValues);
axis.tickSize(tickSize);
} else {
axis.ticks(0);
axis.tickSize(0);
}
if (tickFormat) {
axis.tickFormat(tickFormat);
}
svg.append("g")
.attr("transform", "translate(0," + rectHeight + ")")
.call(axis)
//.selectAll(".tick")
//.data(tickValues, function(d) { return d; })
//.exit()
//.classed("minor", true);
var values = [value];
dragger = svg.selectAll(".dragger")
.data(values)
.enter()
.append("g")
.attr("class", "dragger")
.attr("transform", function(d) {
return "translate(" + scale(d) + ")";
})
var displayValue = null;
if (tickFormat) {
displayValue = tickFormat(value);
} else {
displayValue = d3.format(",.0f")(value);
}
dragger.append("text")
.attr("x", 0)
.attr("y", -15)
.attr("text-anchor", "middle")
.attr("class", "draggertext")
.text(displayValue);
dragger.append("circle")
.attr("class", "dragger-outer")
.attr("r", 10)
.attr("transform", function(d) {
return "translate(0,6)";
});
dragger.append("circle")
.attr("class", "dragger-inner")
.attr("r", 4)
.attr("transform", function(d) {
return "translate(0,6)";
});
// Enable dragger drag
var dragBehaviour = d3.behavior.drag();
dragBehaviour.on("drag", slider.drag);
dragger.call(dragBehaviour);
// Move dragger on click
svg.on("click", slider.click);
});
}
slider.draggerTranslateFn = function() {
return function(d) {
return "translate(" + scale(d) + ")";
}
}
slider.click = function() {
var pos = d3.event.offsetX || d3.event.layerX;
slider.move(pos);
}
slider.drag = function() {
var pos = d3.event.x;
slider.move(pos+margin.left);
}
slider.move = function(pos) {
var l,u;
var newValue = scale.invert(pos - margin.left);
// find tick values that are closest to newValue
// lower bound
if (stepValues != undefined) {
l = stepValues.reduce(function(p, c, i, arr){
if (c < newValue) {
return c;
} else {
return p;
}
});
// upper bound
if (stepValues.indexOf(l) < stepValues.length-1) {
u = stepValues[stepValues.indexOf(l) + 1];
} else {
u = l;
}
// set values
var oldValue = value;
value = ((newValue-l) <= (u-newValue)) ? l : u;
} else {
var oldValue = value;
value = newValue;
}
var values = [value];
// Move dragger
svg.selectAll(".dragger").data(values)
.attr("transform", function(d) {
return "translate(" + scale(d) + ")";
});
var displayValue = null;
if (tickFormat) {
displayValue = tickFormat(value);
} else {
displayValue = d3.format(",.0f")(value);
}
svg.selectAll(".dragger").select("text")
.text(displayValue);
if (range) {
svg.selectAll(".d3slider-rect-value")
.attr("width", scale(value));
}
if (callbackFn) {
callbackFn(slider);
}
}
// Getter/setter functions
slider.min = function(_) {
if (!arguments.length) return min;
min = _;
return slider;
};
slider.max = function(_) {
if (!arguments.length) return max;
max = _;
return slider;
};
slider.classPrefix = function(_) {
if (!arguments.length) return classPrefix;
classPrefix = _;
return slider;
}
slider.tickValues = function(_) {
if (!arguments.length) return tickValues;
tickValues = _;
return slider;
}
slider.ticks = function(_) {
if (!arguments.length) return ticks;
ticks = _;
return slider;
}
slider.stepValues = function(_) {
if (!arguments.length) return stepValues;
stepValues = _;
return slider;
}
slider.tickFormat = function(_) {
if (!arguments.length) return tickFormat;
tickFormat = _;
return slider;
}
slider.value = function(_) {
if (!arguments.length) return value;
value = _;
return slider;
}
slider.showRange = function(_) {
if (!arguments.length) return range;
range = _;
return slider;
}
slider.callback = function(_) {
if (!arguments.length) return callbackFn;
callbackFn = _;
return slider;
}
slider.setValue = function(newValue) {
var pos = scale(newValue) + margin.left;
slider.move(pos);
}
slider.mousemove = function() {
var pos = d3.mouse(this)[0];
var val = slider.getNearest(scale.invert(pos), stepValues);
focus.attr("transform", "translate(" + scale(val) + ",0)");
focus.selectAll("text").text(val);
}
slider.getNearest = function(val, arr) {
var l = arr.reduce(function(p, c, i, a){
if (c < val) {
return c;
} else {
return p;
}
});
var u = arr[arr.indexOf(l)+1];
var nearest = ((value-l) <= (u-value)) ? l : u;
return nearest;
}
slider.destroy = function() {
div.selectAll('svg').remove();
return slider;
}
return slider;
};