-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdu.js
235 lines (204 loc) · 5.66 KB
/
du.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
/**
* D3 utilities
*
* Copyright Robert Monfera
*/
function tupleSorter(t1, t2) {
var a = t1[0], b = t2[0]
return a < b ? -1 : a > b ? 1 : 0
}
function last(a) {
return a[a.length - 1]
}
function add(x, y) {
return x + y
}
function constant(value) {
return function() {
return value
}
}
function identity(x) {
return x
}
function compose(fun1, fun2) {
if(arguments.length === 2) {
return function (/*args*/) {
return fun1(fun2.apply(null, arguments))
}
} else {
var functions = Array.prototype.slice.call(arguments)
var len = functions.length
return function(/*args*/) {
var value = (functions[len - 1]).apply(null, arguments)
var i
for(i = Math.max(0, len - 2); i >= 0; i--) {
value = (functions[i]).call(null, value)
}
return value
}
}
}
function property(key) {
return function(thing) {
return thing[key]
}
}
function pluck(key) {
return function(array) {
var i
var result = []
for(i = 0; i < array.length; i++) {
result.push(array[i][key])
}
return result
}
}
function object(keyValuePairs) {
var result = {}
var i
for(i = 0; i < keyValuePairs.length; i++) {
result[keyValuePairs[i][0]] = keyValuePairs[i][1]
}
return result
}
function pairs(object) {
var keys = Object.keys(object)
var result = []
var key
var i
for(i = 0; i < keys.length; i++) {
key = keys[i]
result.push([key, object[key]])
}
return result
}
function findWhere(key, value) {
// works for objects now...
return function(obj) {
var keys = Object.keys(obj)
var i
for(i = 0; i < keys.length; i++) {
if(obj[[keys[i]]][key] === value) {
return obj[keys[i]]
}
}
return void(0)
}
}
function sortBy(obj, comparisonAccessor) {
// stable sort inspired by the underscore.js implementation
return obj.map(function(element, index) {
return {
value: element,
index: index,
comparedValue: comparisonAccessor(element)
}
}).sort(function(left, right) {
var a = left.comparedValue
var b = right.comparedValue
return a < b ? -1 : a > b ? 1 : left.index - right.index
}).map(function(obj) {return obj.value})
};
function countBy(array, accessorFunction) {
var accessor = accessorFunction || identity
var result = {}
var value
var i
for(i = 0; i < array.length; i++) {
value = accessor(array[i])
if(result[value]) {
result[value]++
} else {
result[value] = 1
}
}
return result
}
function always() {
return true
}
function key(obj) {
return obj.key
}
function value(obj) {
return obj.value
}
function window2(a) {
return a.map(function(value, index, array) {
return [value, array[index + 1]]
}).slice(0, a.length - 1)
}
function tuple(/*args*/) {
var functions = Array.prototype.slice.call(arguments)
return function (x) {
return functions.map(function (elem, i, funs) {
return funs[i](x)
})
}
}
function clamp(base, array) {
var min = base[0]
var max = base[1]
var result = array.map(function(d) {return Math.min(max, Math.max(min, d))})
return result
}
function bind0(rootSelection, cssClass, element, dataFlow) {
cssClass = cssClass || 'boundingBox'
element = element || 'g'
dataFlow = typeof dataFlow === 'function' ? dataFlow : (dataFlow === void(0) ? du.repeat : constant(dataFlow))
var classesToClassAttr = function (classNames) {
return classNames.join(' ')
},
classesToCssSelector = function (classNames) {
return (['']).concat(classNames).join(' .')
},
cssClasses = classesToCssSelector([cssClass]),
binding = rootSelection.selectAll(cssClasses).data(dataFlow, key)
binding.entered = binding.enter().append(element)
binding.entered.attr('class', classesToClassAttr([cssClass]))
return binding
}
function bind(object, key) {
var result = bind0.apply(null, arguments)
object[key] = result
return result
}
function bindd() {
var result = bind.apply(null, arguments)
du.pointMarker(result, [{key: 0}])
return result
}
function translate(funX, funY) {
return function (d, i) {
return 'translate(' + (typeof funX === 'function' ? funX(d, i) : funX) + ',' + (typeof funY === 'function' ? funY(d, i) : funY) + ')';
}
}
function translateX(funX) {
return function (d, i) {
return 'translate(' + (typeof funX === 'function' ? funX(d, i) : funX) + ', 0)'
}
}
function translateY(funY) {
return function (d, i) {
return 'translate(0, ' + (typeof funY === 'function' ? funY(d, i) : funY) + ')'
}
}
var du = {
repeat: tuple(identity),
descend: identity,
bind: bind,
pointMarker: function(selection, data) {
selection.each(function(d) {
var c = bind0(selection, 'point-marker-c', 'circle', data)
c.entered.attr({r: '10px', fill: 'red', 'opacity': 0.2})
c.exit().remove()
var h = bind0(selection, 'point-marker-h', 'line', data)
h.entered.attr({x1: -10000, x2: 10000, 'stroke-dasharray': '2 4', stroke: 'red', 'opacity': 0.5})
h.exit().remove()
var v = bind0(selection, 'point-marker-v', 'line', data)
v.entered.attr({y1: -10000, y2: 10000, 'stroke-dasharray': '2 4', stroke: 'red', 'opacity': 0.5})
v.exit().remove()
})
}
}