-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
heatmap.js
411 lines (364 loc) · 13.2 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
/**
* A heat map is matrix that represents the values of two dimensions of data using colors.
* @class heatMap
* @memberof dc
* @mixes dc.colorMixin
* @mixes dc.marginMixin
* @mixes dc.baseMixin
* @example
* // create a heat map under #chart-container1 element using the default global chart group
* var heatMap1 = dc.heatMap('#chart-container1');
* // create a heat map under #chart-container2 element using chart group A
* var heatMap2 = dc.heatMap('#chart-container2', 'chartGroupA');
* @param {String|node|d3.selection} parent - Any valid
* {@link https://github.com/d3/d3-selection/blob/master/README.md#select d3 single selector} specifying
* a dom block element such as a div; or a dom element or d3 selection.
* @param {String} [chartGroup] - The name of the chart group this chart instance should be placed in.
* Interaction with a chart will only trigger events and redraws within the chart's group.
* @returns {dc.heatMap}
*/
dc.heatMap = function (parent, chartGroup) {
var DEFAULT_BORDER_RADIUS = 6.75;
var _chartBody;
var _cols;
var _rows;
var _colOrdering = d3.ascending;
var _rowOrdering = d3.ascending;
var _colScale = d3.scaleBand();
var _rowScale = d3.scaleBand();
var _xBorderRadius = DEFAULT_BORDER_RADIUS;
var _yBorderRadius = DEFAULT_BORDER_RADIUS;
var _chart = dc.colorMixin(dc.marginMixin(dc.baseMixin({})));
_chart._mandatoryAttributes(['group']);
_chart.title(_chart.colorAccessor());
var _colsLabel = function (d) {
return d;
};
var _rowsLabel = function (d) {
return d;
};
/**
* Set or get the column label function. The chart class uses this function to render
* column labels on the X axis. It is passed the column name.
* @method colsLabel
* @memberof dc.heatMap
* @instance
* @example
* // the default label function just returns the name
* chart.colsLabel(function(d) { return d; });
* @param {Function} [labelFunction=function(d) { return d; }]
* @returns {Function|dc.heatMap}
*/
_chart.colsLabel = function (labelFunction) {
if (!arguments.length) {
return _colsLabel;
}
_colsLabel = labelFunction;
return _chart;
};
/**
* Set or get the row label function. The chart class uses this function to render
* row labels on the Y axis. It is passed the row name.
* @method rowsLabel
* @memberof dc.heatMap
* @instance
* @example
* // the default label function just returns the name
* chart.rowsLabel(function(d) { return d; });
* @param {Function} [labelFunction=function(d) { return d; }]
* @returns {Function|dc.heatMap}
*/
_chart.rowsLabel = function (labelFunction) {
if (!arguments.length) {
return _rowsLabel;
}
_rowsLabel = labelFunction;
return _chart;
};
var _xAxisOnClick = function (d) { filterAxis(0, d); };
var _yAxisOnClick = function (d) { filterAxis(1, d); };
var _boxOnClick = function (d) {
var filter = d.key;
dc.events.trigger(function () {
_chart.filter(filter);
_chart.redrawGroup();
});
};
function filterAxis (axis, value) {
var cellsOnAxis = _chart.selectAll('.box-group').filter(function (d) {
return d.key[axis] === value;
});
var unfilteredCellsOnAxis = cellsOnAxis.filter(function (d) {
return !_chart.hasFilter(d.key);
});
dc.events.trigger(function () {
var selection = unfilteredCellsOnAxis.empty() ? cellsOnAxis : unfilteredCellsOnAxis;
var filters = selection.data().map(function (kv) {
return dc.filters.TwoDimensionalFilter(kv.key);
});
_chart._filter([filters]);
_chart.redrawGroup();
});
}
dc.override(_chart, 'filter', function (filter) {
if (!arguments.length) {
return _chart._filter();
}
return _chart._filter(dc.filters.TwoDimensionalFilter(filter));
});
/**
* Gets or sets the values used to create the rows of the heatmap, as an array. By default, all
* the values will be fetched from the data using the value accessor.
* @method rows
* @memberof dc.heatMap
* @instance
* @param {Array<String|Number>} [rows]
* @returns {Array<String|Number>|dc.heatMap}
*/
_chart.rows = function (rows) {
if (!arguments.length) {
return _rows;
}
_rows = rows;
return _chart;
};
/**
#### .rowOrdering([orderFunction])
Get or set an accessor to order the rows. Default is d3.ascending.
*/
_chart.rowOrdering = function (_) {
if (!arguments.length) {
return _rowOrdering;
}
_rowOrdering = _;
return _chart;
};
/**
* Gets or sets the keys used to create the columns of the heatmap, as an array. By default, all
* the values will be fetched from the data using the key accessor.
* @method cols
* @memberof dc.heatMap
* @instance
* @param {Array<String|Number>} [cols]
* @returns {Array<String|Number>|dc.heatMap}
*/
_chart.cols = function (cols) {
if (!arguments.length) {
return _cols;
}
_cols = cols;
return _chart;
};
/**
#### .colOrdering([orderFunction])
Get or set an accessor to order the cols. Default is ascending.
*/
_chart.colOrdering = function (_) {
if (!arguments.length) {
return _colOrdering;
}
_colOrdering = _;
return _chart;
};
_chart._doRender = function () {
_chart.resetSvg();
_chartBody = _chart.svg()
.append('g')
.attr('class', 'heatmap')
.attr('transform', 'translate(' + _chart.margins().left + ',' + _chart.margins().top + ')');
return _chart._doRedraw();
};
_chart._doRedraw = function () {
var data = _chart.data(),
rows = _chart.rows() || data.map(_chart.valueAccessor()),
cols = _chart.cols() || data.map(_chart.keyAccessor());
if (_rowOrdering) {
rows = rows.sort(_rowOrdering);
}
if (_colOrdering) {
cols = cols.sort(_colOrdering);
}
rows = _rowScale.domain(rows);
cols = _colScale.domain(cols);
var rowCount = rows.domain().length,
colCount = cols.domain().length,
boxWidth = Math.floor(_chart.effectiveWidth() / colCount),
boxHeight = Math.floor(_chart.effectiveHeight() / rowCount);
cols.rangeRound([0, _chart.effectiveWidth()]);
rows.rangeRound([_chart.effectiveHeight(), 0]);
var boxes = _chartBody.selectAll('g.box-group').data(_chart.data(), function (d, i) {
return _chart.keyAccessor()(d, i) + '\0' + _chart.valueAccessor()(d, i);
});
boxes.exit().remove();
var gEnter = boxes.enter().append('g')
.attr('class', 'box-group');
gEnter.append('rect')
.attr('class', 'heat-box')
.attr('fill', 'white')
.attr('x', function (d, i) { return cols(_chart.keyAccessor()(d, i)); })
.attr('y', function (d, i) { return rows(_chart.valueAccessor()(d, i)); })
.on('click', _chart.boxOnClick());
if (_chart.renderTitle()) {
gEnter.append('title');
boxes.select('title').text(_chart.title());
}
boxes = gEnter.merge(boxes);
dc.transition(boxes.select('rect'), _chart.transitionDuration(), _chart.transitionDelay())
.attr('x', function (d, i) { return cols(_chart.keyAccessor()(d, i)); })
.attr('y', function (d, i) { return rows(_chart.valueAccessor()(d, i)); })
.attr('rx', _xBorderRadius)
.attr('ry', _yBorderRadius)
.attr('fill', _chart.getColor)
.attr('width', boxWidth)
.attr('height', boxHeight);
var gCols = _chartBody.select('g.cols');
if (gCols.empty()) {
gCols = _chartBody.append('g').attr('class', 'cols axis');
}
var gColsText = gCols.selectAll('text').data(cols.domain());
gColsText.exit().remove();
gColsText = gColsText
.enter()
.append('text')
.attr('x', function (d) {
return cols(d) + boxWidth / 2;
})
.style('text-anchor', 'middle')
.attr('y', _chart.effectiveHeight())
.attr('dy', 12)
.on('click', _chart.xAxisOnClick())
.text(_chart.colsLabel())
.merge(gColsText);
dc.transition(gColsText, _chart.transitionDuration(), _chart.transitionDelay())
.text(_chart.colsLabel())
.attr('x', function (d) { return cols(d) + boxWidth / 2; })
.attr('y', _chart.effectiveHeight());
var gRows = _chartBody.select('g.rows');
if (gRows.empty()) {
gRows = _chartBody.append('g').attr('class', 'rows axis');
}
var gRowsText = gRows.selectAll('text').data(rows.domain());
gRowsText.exit().remove();
gRowsText = gRowsText
.enter()
.append('text')
.style('text-anchor', 'end')
.attr('x', 0)
.attr('dx', -2)
.attr('y', function (d) { return rows(d) + boxHeight / 2; })
.attr('dy', 6)
.on('click', _chart.yAxisOnClick())
.text(_chart.rowsLabel())
.merge(gRowsText);
dc.transition(gRowsText, _chart.transitionDuration(), _chart.transitionDelay())
.text(_chart.rowsLabel())
.attr('y', function (d) { return rows(d) + boxHeight / 2; });
if (_chart.hasFilter()) {
_chart.selectAll('g.box-group').each(function (d) {
if (_chart.isSelectedNode(d)) {
_chart.highlightSelected(this);
} else {
_chart.fadeDeselected(this);
}
});
} else {
_chart.selectAll('g.box-group').each(function () {
_chart.resetHighlight(this);
});
}
return _chart;
};
/**
* Gets or sets the handler that fires when an individual cell is clicked in the heatmap.
* By default, filtering of the cell will be toggled.
* @method boxOnClick
* @memberof dc.heatMap
* @instance
* @example
* // default box on click handler
* chart.boxOnClick(function (d) {
* var filter = d.key;
* dc.events.trigger(function () {
* _chart.filter(filter);
* _chart.redrawGroup();
* });
* });
* @param {Function} [handler]
* @returns {Function|dc.heatMap}
*/
_chart.boxOnClick = function (handler) {
if (!arguments.length) {
return _boxOnClick;
}
_boxOnClick = handler;
return _chart;
};
/**
* Gets or sets the handler that fires when a column tick is clicked in the x axis.
* By default, if any cells in the column are unselected, the whole column will be selected,
* otherwise the whole column will be unselected.
* @method xAxisOnClick
* @memberof dc.heatMap
* @instance
* @param {Function} [handler]
* @returns {Function|dc.heatMap}
*/
_chart.xAxisOnClick = function (handler) {
if (!arguments.length) {
return _xAxisOnClick;
}
_xAxisOnClick = handler;
return _chart;
};
/**
* Gets or sets the handler that fires when a row tick is clicked in the y axis.
* By default, if any cells in the row are unselected, the whole row will be selected,
* otherwise the whole row will be unselected.
* @method yAxisOnClick
* @memberof dc.heatMap
* @instance
* @param {Function} [handler]
* @returns {Function|dc.heatMap}
*/
_chart.yAxisOnClick = function (handler) {
if (!arguments.length) {
return _yAxisOnClick;
}
_yAxisOnClick = handler;
return _chart;
};
/**
* Gets or sets the X border radius. Set to 0 to get full rectangles.
* @method xBorderRadius
* @memberof dc.heatMap
* @instance
* @param {Number} [xBorderRadius=6.75]
* @returns {Number|dc.heatMap}
*/
_chart.xBorderRadius = function (xBorderRadius) {
if (!arguments.length) {
return _xBorderRadius;
}
_xBorderRadius = xBorderRadius;
return _chart;
};
/**
* Gets or sets the Y border radius. Set to 0 to get full rectangles.
* @method yBorderRadius
* @memberof dc.heatMap
* @instance
* @param {Number} [yBorderRadius=6.75]
* @returns {Number|dc.heatMap}
*/
_chart.yBorderRadius = function (yBorderRadius) {
if (!arguments.length) {
return _yBorderRadius;
}
_yBorderRadius = yBorderRadius;
return _chart;
};
_chart.isSelectedNode = function (d) {
return _chart.hasFilter(d.key);
};
return _chart.anchor(parent, chartGroup);
};