-
Notifications
You must be signed in to change notification settings - Fork 4
/
heatmap.js
511 lines (443 loc) · 14.3 KB
/
heatmap.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import { select, format, range, scaleLinear, axisLeft, axisTop, axisBottom } from 'd3'
function cantorPair (x, y) {
var z = ((x + y) * (x + y + 1)) / 2 + y
return z
}
export default function () {
var svg = null
var columns = 0
var rows = 0
var title = ''
var subtitle = ''
var legendLabel = ''
var width = 960
var margin = {
top: 20,
right: 0,
bottom: 0,
left: 0
}
var gridSize = null
var colorScale = null
var xAxisScale = null
var yAxisScale = null
var xAxisTickFormat = format('.0f')
var yAxisTickFormat = format('.2s')
var xAxisScaleTicks = 20
var yAxisScaleTicks = 20
var xAxisLabels = null
var yAxisLabels = null
var xAxisLabelFormat = function (d) { return d }
var yAxisLabelFormat = function (d) { return d }
var hideLegend = false
var legendScaleTicks = 5
var clickHandler = null
var mouseOverHandler = null
var highlight = []
var highlightColor = '#936EB5'
var highlightOpacity = '0.4'
var invertHighlightRows = false
var gridStrokeOpacity = 0.6
function click (d, i, j) {
if (typeof clickHandler === 'function') {
clickHandler(d, i, j)
}
}
function mouseOver (d, i, j) {
if (typeof mouseOverHandler === 'function') {
mouseOverHandler(d, i, j)
}
}
function getHighlightFrames () {
var highlightFrames = []
for (var k in highlight) { // all highlights
if (highlight[k].start[0] <= highlight[k].end[0]) { // no reverse column range highlight
for (var i = highlight[k].start[0]; i <= highlight[k].end[0]; i++) {
var j = null
if (i > highlight[k].start[0] && i < highlight[k].end[0]) { // middle columns
for (j = 0; j < rows; j++) {
highlightFrames.push([i, j])
}
} else if (i === highlight[k].start[0]) { // start column, or start and end are the same
if (!invertHighlightRows) { // not inverted row selection
if (i === highlight[k].end[0]) { // ends in the same column
if (highlight[k].start[1] <= highlight[k].end[1]) { // no reverse row range highlight
for (j = highlight[k].start[1]; j <= highlight[k].end[1]; j++) {
highlightFrames.push([i, j])
}
} else {
console.log('Error: Start row is higher than end row. No reverse range highlight.')
}
} else { // doesn't end in the same column
for (j = highlight[k].start[1]; j < rows; j++) {
highlightFrames.push([i, j])
}
}
} else { // inverted row selection
if (i === highlight[k].end[0]) { // ends in the same column
if (highlight[k].start[1] >= highlight[k].end[1]) { // no reverse row range highlight. backwards in inverted.
for (j = highlight[k].start[1]; j >= highlight[k].end[1]; j--) {
highlightFrames.push([i, j])
}
} else {
console.log('Error: Start row is higher than end row. No reverse range highlight.')
}
} else { // doesn't end in the same column
for (j = highlight[k].start[1]; j >= 0; j--) {
highlightFrames.push([i, j])
}
}
}
} else { // end column, when different than start column
if (!invertHighlightRows) { // not inverted row selection
for (j = highlight[k].end[1]; j >= 0; j--) {
highlightFrames.push([i, j])
}
} else { // inverted row selection
for (j = highlight[k].end[1]; j < rows; j++) {
highlightFrames.push([i, j])
}
}
}
}
} else {
console.log('Error: Start column is higher than end column. No reverse range highlight.')
}
}
return highlightFrames
}
function updateHighlight () {
if (svg && rows && gridSize) {
var highlightFrames = getHighlightFrames()
var frames = svg.selectAll('g.highlight')
.data(highlightFrames, function (d) { return cantorPair(d[0], d[1]) })
frames.exit().remove()
frames.enter().append('g')
.attr('class', 'highlight')
.append('rect')
.attr('x', function (d) { return (d[0] * gridSize) + 3 })
.attr('y', function (d) { return d[1] * gridSize })
.attr('width', gridSize)
.attr('height', gridSize)
.style('fill', highlightColor)
.style('fill-opacity', highlightOpacity)
.style('pointer-events', 'none')
} else {
console.log("Error: Can't update highlight. Heatmap was not initialized.")
}
}
function heatmap (selection) {
var data = selection.datum()
columns = data.length
rows = data[0].length
if (title) {
margin.top = margin.top + 50
}
if (subtitle) {
margin.top = margin.top + 20
}
if (!hideLegend) {
margin.bottom = margin.bottom + 50
}
if (yAxisScale || yAxisLabels) {
margin.left = margin.left + 50
}
gridSize = (width - margin.left - margin.right) / columns
var height = gridSize * (rows + 2)
var max = 0
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].length; j++) {
if (data[i][j] > max) { max = data[i][j] }
}
}
if (!colorScale) {
colorScale = scaleLinear()
.domain([0, max / 2, max])
.range(['#FFFFDD', '#3E9583', '#1F2D86'])
// .interpolate(d3.interpolateHcl);
}
svg = selection
.append('svg')
.attr('width', width + margin.left + margin.right + 9)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
if (yAxisScale || yAxisLabels) {
if (yAxisScale) {
var y = scaleLinear()
.domain(yAxisScale)
.range([(gridSize * rows), 0])
svg.append('g')
.attr('transform', 'translate(3, 0)')
.attr('class', 'rowLabel axis')
.call(axisLeft(y)
.ticks(yAxisScaleTicks)
.tickFormat(yAxisTickFormat))
} else {
svg.selectAll('.rowLabel')
.data(yAxisLabels)
.enter().append('text')
.text(yAxisLabelFormat)
.attr('x', 0)
.attr('y', function (d, i) { return i * gridSize })
.style('text-anchor', 'end')
.attr('transform', 'translate(-6,' + gridSize / 1.2 + ')')
.attr('class', 'rowLabel mono axis')
}
}
if (xAxisScale || xAxisLabels) {
if (xAxisScale) {
var x = scaleLinear()
.domain(xAxisScale)
// .range([0, width - margin.left - margin.right - 40])
.range([0, width - margin.left - margin.right])
svg.append('g')
.attr('transform', 'translate(3, 3)')
.attr('class', 'columnLabel axis')
.call(axisTop(x)
.ticks(xAxisScaleTicks)
.tickFormat(xAxisTickFormat))
} else {
svg.selectAll('.columnLabel')
.data(xAxisLabels)
.enter().append('text')
.text(xAxisLabelFormat)
.attr('y', function (d, i) { return i * gridSize })
.attr('x', 0)
.style('text-anchor', 'beginning')
.attr('transform', 'translate(' + gridSize / 1.4 + ', -6) rotate(270)')
.attr('class', 'columnLabel mono axis')
}
}
svg.selectAll('g.column')
.data(data)
.enter().append('g')
.each(function (d, i) { // function (d, i, j) might replace .each.
select(this).selectAll('rect')
.data(d)
.enter().append('rect')
.attr('x', function (d) { return (i * gridSize) + 3 }) // column
.attr('y', function (d, j) { return j * gridSize }) // row
.attr('class', 'bordered')
.attr('width', gridSize)
.attr('height', gridSize)
.style('stroke', 'white')
.style('stroke-opacity', gridStrokeOpacity)
.style('fill', function (d) { return colorScale(d) })
.style('pointer-events', 'all')
.on('mouseover', function (d, j) { return mouseOver(d, i, j) })
.on('click', function (d, j) { return click(d, i, j) })
})
if (highlight && highlight.length > 0) {
updateHighlight()
}
// Append title to the top
if (title) {
svg.append('text')
.attr('class', 'title')
.attr('x', width / 2)
.attr('y', -60)
.style('text-anchor', 'middle')
.text(title)
}
if (subtitle) {
svg.append('text')
.attr('class', 'subtitle')
.attr('x', width / 2)
.attr('y', -40)
.style('text-anchor', 'middle')
.text(subtitle)
}
if (!hideLegend) {
// Extra scale since the color scale is interpolated
var countScale = scaleLinear()
.domain([0, max])
.range([0, width])
// Calculate the variables for the temp gradient
var numStops = 10
var countRange = countScale.domain()
var countPoint = []
countRange[2] = countRange[1] - countRange[0]
for (var i = 0; i < numStops; i++) {
countPoint.push(i * countRange[2] / (numStops - 1) + countRange[0])
}// for i
// Create the gradient
svg.append('defs')
.append('linearGradient')
.attr('id', 'legend-traffic')
.attr('x1', '0%').attr('y1', '0%')
.attr('x2', '100%').attr('y2', '0%')
.selectAll('stop')
.data(range(numStops))
.enter().append('stop')
.attr('offset', function (d, i) {
return countScale(countPoint[i]) / width
})
.attr('stop-color', function (d, i) {
return colorScale(countPoint[i])
})
var legendWidth = Math.min(width * 0.8, 400)
// Color Legend container
var legendsvg = svg.append('g')
.attr('class', 'legendWrapper')
.attr('transform', 'translate(' + (width / 2) + ',' + (gridSize * rows + 40) + ')')
// Draw the Rectangle
legendsvg.append('rect')
.attr('class', 'legendRect')
.attr('x', -legendWidth / 2)
.attr('y', 0)
// .attr("rx", hexRadius*1.25/2)
.attr('width', legendWidth)
.attr('height', 10)
.style('fill', 'url(#legend-traffic)')
// Append title
legendsvg.append('text')
.attr('class', 'legendTitle')
.attr('x', 0)
.attr('y', -10)
.style('text-anchor', 'middle')
.text(legendLabel)
// Set scale for x-axis
var xScale = scaleLinear()
.range([-legendWidth / 2, legendWidth / 2])
.domain([0, max])
// Define x-axis
var xAxis = axisBottom()
.ticks(legendScaleTicks)
// .tickFormat(formatPercent)
.scale(xScale)
// Set up X axis
legendsvg.append('g')
.attr('class', 'axis')
.attr('transform', 'translate(0,' + (10) + ')')
.call(xAxis)
}
}
heatmap.title = function (_) {
if (!arguments.length) { return title }
title = _
return heatmap
}
heatmap.subtitle = function (_) {
if (!arguments.length) { return subtitle }
subtitle = _
return heatmap
}
heatmap.legendLabel = function (_) {
if (!arguments.length) { return legendLabel }
legendLabel = _
return heatmap
}
heatmap.width = function (_) {
if (!arguments.length) { return width }
width = _
return heatmap
}
heatmap.margin = function (_) {
if (!arguments.length) { return margin }
margin = _
return heatmap
}
heatmap.colorScale = function (_) {
if (!arguments.length) { return colorScale }
colorScale = _
return heatmap
}
heatmap.xAxisScale = function (_) {
if (!arguments.length) { return xAxisScale }
xAxisScale = _
return heatmap
}
heatmap.yAxisScale = function (_) {
if (!arguments.length) { return yAxisScale }
yAxisScale = _
return heatmap
}
heatmap.xAxisScaleTicks = function (_) {
if (!arguments.length) { return xAxisScaleTicks }
xAxisScaleTicks = _
return heatmap
}
heatmap.yAxisScaleTicks = function (_) {
if (!arguments.length) { return yAxisScaleTicks }
yAxisScaleTicks = _
return heatmap
}
heatmap.xAxisLabelFormat = function (_) {
if (!arguments.length) { return xAxisLabelFormat }
xAxisLabelFormat = _
return heatmap
}
heatmap.yAxisLabelFormat = function (_) {
if (!arguments.length) { return yAxisLabelFormat }
yAxisLabelFormat = _
return heatmap
}
heatmap.xAxisTickFormat = function (_) {
if (!arguments.length) { return xAxisTickFormat }
xAxisTickFormat = _
return heatmap
}
heatmap.yAxisTickFormat = function (_) {
if (!arguments.length) { return yAxisTickFormat }
yAxisTickFormat = _
return heatmap
}
heatmap.hideLegend = function (_) {
if (!arguments.length) { return hideLegend }
hideLegend = _
return heatmap
}
heatmap.legendScaleTicks = function (_) {
if (!arguments.length) { return legendScaleTicks }
legendScaleTicks = _
return heatmap
}
heatmap.onClick = function (_) {
if (!arguments.length) { return clickHandler }
clickHandler = _
return heatmap
}
heatmap.onMouseOver = function (_) {
if (!arguments.length) { return mouseOverHandler }
mouseOverHandler = _
return heatmap
}
heatmap.xAxisLabels = function (_) {
if (!arguments.length) { return xAxisLabels }
xAxisLabels = _
return heatmap
}
heatmap.yAxisLabels = function (_) {
if (!arguments.length) { return yAxisLabels }
yAxisLabels = _
return heatmap
}
heatmap.gridStrokeOpacity = function (_) {
if (!arguments.length) { return gridStrokeOpacity }
gridStrokeOpacity = _
return heatmap
}
heatmap.highlightColor = function (_) {
if (!arguments.length) { return highlightColor }
highlightColor = _
return heatmap
}
heatmap.highlightOpacity = function (_) {
if (!arguments.length) { return highlightOpacity }
highlightOpacity = _
return heatmap
}
heatmap.setHighlight = function (_) {
if (!arguments.length) { return highlight }
highlight = _
return heatmap
}
heatmap.invertHighlightRows = function (_) {
if (!arguments.length) { return invertHighlightRows }
invertHighlightRows = _
return heatmap
}
heatmap.updateHighlight = updateHighlight
return heatmap
}