-
Notifications
You must be signed in to change notification settings - Fork 107
/
legend.js
283 lines (247 loc) · 6.6 KB
/
legend.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
import { select } from "d3-selection"
import { format, formatPrefix } from "d3-format"
const d3_identity = d => d
const d3_reverse = arr => {
const mirror = []
for (let i = 0, l = arr.length; i < l; i++) {
mirror[i] = arr[l - i - 1]
}
return mirror
}
//Text wrapping code adapted from Mike Bostock
const d3_textWrapping = (text, width) => {
text.each(function() {
var text = select(this),
words = text
.text()
.split(/\s+/)
.reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.2, //ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")) || 0,
tspan = text
.text(null)
.append("tspan")
.attr("x", 0)
.attr("dy", dy + "em")
while ((word = words.pop())) {
line.push(word)
tspan.text(line.join(" "))
if (tspan.node().getComputedTextLength() > width && line.length > 1) {
line.pop()
tspan.text(line.join(" "))
line = [word]
tspan = text
.append("tspan")
.attr("x", 0)
.attr("dy", lineHeight + dy + "em")
.text(word)
}
}
})
}
const d3_mergeLabels = (gen = [], labels, domain, range, labelDelimiter) => {
if (typeof labels === "object") {
if (labels.length === 0) return gen
let i = labels.length
for (; i < gen.length; i++) {
labels.push(gen[i])
}
return labels
} else if (typeof labels === "function") {
const customLabels = []
const genLength = gen.length
for (let i = 0; i < genLength; i++) {
customLabels.push(
labels({
i,
genLength,
generatedLabels: gen,
domain,
range,
labelDelimiter
})
)
}
return customLabels
}
return gen
}
const d3_linearLegend = (scale, cells, labelFormat) => {
let data = []
if (cells.length > 1) {
data = cells
} else {
const domain = scale.domain(),
increment = (domain[domain.length - 1] - domain[0]) / (cells - 1)
let i = 0
for (; i < cells; i++) {
data.push(domain[0] + i * increment)
}
}
const labels = data.map(labelFormat)
return {
data: data,
labels: labels,
feature: d => scale(d)
}
}
const d3_quantLegend = (scale, labelFormat, labelDelimiter) => {
const labels = scale.range().map(d => {
const invert = scale.invertExtent(d)
return (
labelFormat(invert[0]) +
" " +
labelDelimiter +
" " +
labelFormat(invert[1])
)
})
return {
data: scale.range(),
labels: labels,
feature: d3_identity
}
}
const d3_ordinalLegend = scale => ({
data: scale.domain(),
labels: scale.domain(),
feature: d => scale(d)
})
const d3_cellOver = (cellDispatcher, d, obj) => {
cellDispatcher.call("cellover", obj, d)
}
const d3_cellOut = (cellDispatcher, d, obj) => {
cellDispatcher.call("cellout", obj, d)
}
const d3_cellClick = (cellDispatcher, d, obj) => {
cellDispatcher.call("cellclick", obj, d)
}
export default {
d3_drawShapes: (
shape,
shapes,
shapeHeight,
shapeWidth,
shapeRadius,
path
) => {
if (shape === "rect") {
shapes.attr("height", shapeHeight).attr("width", shapeWidth)
} else if (shape === "circle") {
shapes.attr("r", shapeRadius)
} else if (shape === "line") {
shapes
.attr("x1", 0)
.attr("x2", shapeWidth)
.attr("y1", 0)
.attr("y2", 0)
} else if (shape === "path") {
shapes.attr("d", path)
}
},
d3_addText: function(svg, enter, labels, classPrefix, labelWidth) {
enter.append("text").attr("class", classPrefix + "label")
const text = svg
.selectAll(`g.${classPrefix}cell text.${classPrefix}label`)
.data(labels)
.text(d3_identity)
if (labelWidth) {
svg
.selectAll(`g.${classPrefix}cell text.${classPrefix}label`)
.call(d3_textWrapping, labelWidth)
}
return text
},
d3_calcType: function(
scale,
ascending,
cells,
labels,
labelFormat,
labelDelimiter
) {
const type = scale.invertExtent
? d3_quantLegend(scale, labelFormat, labelDelimiter)
: scale.ticks
? d3_linearLegend(scale, cells, labelFormat)
: d3_ordinalLegend(scale)
//for d3.scaleSequential that doesn't have a range function
const range = (scale.range && scale.range()) || scale.domain()
type.labels = d3_mergeLabels(
type.labels,
labels,
scale.domain(),
range,
labelDelimiter
)
if (ascending) {
type.labels = d3_reverse(type.labels)
type.data = d3_reverse(type.data)
}
return type
},
d3_filterCells: (type, cellFilter) => {
let filterCells = type.data
.map((d, i) => ({ data: d, label: type.labels[i] }))
.filter(cellFilter)
const dataValues = filterCells.map(d => d.data)
const labelValues = filterCells.map(d => d.label)
type.data = type.data.filter(d => dataValues.indexOf(d) !== -1)
type.labels = type.labels.filter(d => labelValues.indexOf(d) !== -1)
return type
},
d3_placement: (orient, cell, cellTrans, text, textTrans, labelAlign) => {
cell.attr("transform", cellTrans)
text.attr("transform", textTrans)
if (orient === "horizontal") {
text.style("text-anchor", labelAlign)
}
},
d3_addEvents: function(cells, dispatcher) {
cells
.on("mouseover.legend", function(d) {
d3_cellOver(dispatcher, d, this)
})
.on("mouseout.legend", function(d) {
d3_cellOut(dispatcher, d, this)
})
.on("click.legend", function(d) {
d3_cellClick(dispatcher, d, this)
})
},
d3_title: (svg, title, classPrefix, titleWidth) => {
if (title !== "") {
const titleText = svg.selectAll("text." + classPrefix + "legendTitle")
titleText
.data([title])
.enter()
.append("text")
.attr("class", classPrefix + "legendTitle")
svg.selectAll("text." + classPrefix + "legendTitle").text(title)
if (titleWidth) {
svg
.selectAll("text." + classPrefix + "legendTitle")
.call(d3_textWrapping, titleWidth)
}
const cellsSvg = svg.select("." + classPrefix + "legendCells")
const yOffset = svg
.select("." + classPrefix + "legendTitle")
.nodes()
.map(d => d.getBBox().height)[0],
xOffset = -cellsSvg.nodes().map(function(d) {
return d.getBBox().x
})[0]
cellsSvg.attr("transform", "translate(" + xOffset + "," + yOffset + ")")
}
},
d3_defaultLocale: {
format,
formatPrefix
},
d3_defaultFormatSpecifier: ".01f",
d3_defaultDelimiter: "to"
}