This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcg-heatmap.html
executable file
·323 lines (286 loc) · 8.4 KB
/
tcg-heatmap.html
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
<!--
@element tcg-heatmap
@author designbyraychou
@homepage https://github.com/the-control-group/tcg-heatmap
**This element is compatible with Polymer 0.5.**
copied this guy: http://bl.ocks.org/tjdecke/5558084
__Example:__
```
<tcg-heatmap
yAxisLabels="{{ yAxisLabels }}"
xAxisLabels="{{ xAxisLabels }}"
bgColors="{{ bgColors }}"
textColors="{{ textColors }}"
data="{{ data }}"></tcg-heatmap>
```
# Attributes
#### `margin`
Object to set margin around the heatmap. This will leave space for the axis labels and color key.
#### `duration`
Number in milliseconds for the animation time
#### `legendClassName`
String for the class name
#### `cellBackgroundClassName`
String for the class name
#### `cellTextClassName`
String for the class name
#### `xAxisLabelClassName`
String for the class name
#### `yAxisLabelClassName`
String for the class name
#### `xAxisLabels`
Array of strings for the x axis labels
#### `yAxisLabels`
Array of strings for the y axis labels
#### `bgColors`
Array of strings for the heatmap cell colors. Ordered by smallest value to largest value
#### `textColors`
Array of strings for the heatmap text colors. Ordered by smallest value to largest value
#### `data`
Array: Example [{x: 0, y: 0, value: 20}]
-->
<link rel="import" href="../polymer/polymer.html">
<polymer-element name="tcg-heatmap" attributes="margin duration legendClassName cellBackgroundClassName cellTextClassName xAxisLabelClassName yAxisLabelClassName xAxisLabels yAxisLabels bgColors textColors data min max formatLegend">
<template>
<style>
::host,
#chart {
display: -webkit-flex;
display: flex;
width: 100%;
height: 100%;
}
</style>
<div id="chart"></div>
</template>
<script src="../d3/d3.js"></script>
<script src="../lodash/lodash.js"></script>
<script>
Polymer({
publish: {
margin: {
top: 50,
right: 0,
left: 30,
bottom: 100
},
duration: 1000,
legendClassName: 'legend',
cellBackgroundClassName: 'cell',
cellTextClassName: 'text',
xAxisLabelClassName: 'xAxisLabel',
yAxisLabelClassName: 'yAxisLabel',
xAxisLabels: [],
yAxisLabels: [],
bgColors: [],
textColors: [],
data: [],
min: null,
max: null,
_svg: null,
},
observe: {
'xAxisLabels yAxisLabels data': 'reDrawGraph',
},
ready: function() {
var poly = this;
var resizeTimeout;
this._onResize = function() {
if ( !resizeTimeout ) {
resizeTimeout = setTimeout(function() {
resizeTimeout = null;
poly.drawGraph();
}, 60);
}
};
window.addEventListener('resize',this._onResize);
this.async(function() {
this.reDrawGraph();
});
},
detached: function() {
window.removeEventListener('resize',this._onResize);
},
reDrawGraph: function() {
this._isNew = true;
this.drawGraph();
},
drawGraph: function() {
this.job('drawGraph',function(){
this.setDimensions();
if( this.xAxisLabels.length && this.yAxisLabels.length ) {
this.setSVG();
this.setLabels();
}
if( this._svg != null ) {
this.setColors();
this.setLegend();
this.setCells();
}
this.fire('done');
},60);
},
formatLegend: function(data) {
return '≥ ' + data.toFixed(2);
},
setDimensions: function() {
var rect = this.getBoundingClientRect();
this._width = rect.width;
this._height = rect.height;
},
setSVG: function() {
this._gridSize = {
x: Math.floor((this._width - this.margin.left - this.margin.right) / this.xAxisLabels.length),
y: Math.floor((this._height - this.margin.top - this.margin.bottom) / this.yAxisLabels.length)
};
var svg = d3.select(this.$.chart).selectAll('svg');
if( svg.length ) {
svg.remove();
}
this._svg = d3.select(this.$.chart).append('svg')
.attr('width', this._width)
.attr('height', this._height)
.append('g')
.attr('transform', 'translate(' + this.margin.left + ',' + this.margin.top + ')');
},
setColors: function() {
var max = this.max ? Number(this.max) : d3.max(this.data, function (d) {
return Number(d.value);
});
var min = this.min ? Number(this.min) : d3.min(this.data, function (d) {
return Number(d.value);
});
this._bgColorScale = d3.scale.quantile()
.domain([min, max])
.range(this.bgColors);
this._textColorScale = d3.scale.quantile()
.domain([min, max])
.range(this.textColors);
},
setLabels: function() {
var poly = this;
if( this.xAxisLabels.length && this.yAxisLabels.length ) {
this._svg.selectAll('.'+poly.yAxisLabelClassName)
.data(this.yAxisLabels)
.enter().append('text')
.text(function (d) {
return d;
})
.attr('x', -10)
.attr('y', function (d, i) {
return ((i+1) * poly._gridSize.y) - (poly._gridSize.y/2);
})
.style('text-anchor', 'end')
.style('alignment-baseline','middle')
.attr('class', function (d, i) {
var count = i+1;
return poly.yAxisLabelClassName + ' y'+count;
});
this._svg.selectAll('.'+poly.xAxisLabelClassName)
.data(this.xAxisLabels)
.enter().append('text')
.text(function(d) {
return d;
})
.attr('x', function(d, i) {
return ((i+1) * poly._gridSize.x) - (poly._gridSize.x/2);
})
.attr('y', -10)
.style('text-anchor', 'middle')
.attr('class', function(d, i) {
var count = i+1;
return poly.xAxisLabelClassName + ' x'+count;
});
}
},
setLegend: function() {
var poly = this;
var height = this._height - this.margin.top - this.margin.bottom;
var legendWidth = (this._width - this.margin.left - this.margin.right )/ this.bgColors.length;
var legendHeight = 10;
var legend = this._svg.selectAll('.'+poly.legendClassName)
.data([0].concat(this._bgColorScale.quantiles()), function(d) {
return d;
})
.enter().append('g')
.attr('class', poly.legendClassName);
legend.append('rect')
.attr('x', function(d, i) {
return legendWidth * i;
})
.attr('y', height - legendHeight + (poly.margin.bottom / 3) )
.attr('width', legendWidth)
.attr('height', legendHeight)
.style('fill', function(d, i) {
return poly.bgColors[i];
});
legend.append('text')
.text(function(d) {
return poly.formatLegend(d);
})
.style('alignment-baseline','top')
.attr('x', function(d, i) {
return legendWidth * i;
})
.attr('y', height + 15 + (poly.margin.bottom / 3) );
},
setCells: function(oldValue,newValue) {
var poly = this;
var height = this._height - this.margin.top - this.margin.bottom;
var cellBackgrounds = this._svg.selectAll('.'+poly.cellBackgroundClassName)
.data( poly.data )
.enter().append('rect')
.attr('x', function(d) {
return (d.x - 1) * poly._gridSize.x;
})
.attr('y', function(d) {
return (d.y - 1) * poly._gridSize.y;
})
.attr('class', function(d, i) {
return poly.cellBackgroundClassName + ' y'+d.y + ' x'+d.x;
})
.attr('width', poly._gridSize.x)
.attr('height', poly._gridSize.y)
.style('fill',poly.bgColors[0]);
var cellTexts = this._svg.selectAll('.'+poly.cellTextClassName)
.data( poly.data )
.enter().append('text')
.attr('x', function(d) {
return ((d.x - 1) * poly._gridSize.x) + (poly._gridSize.x/2);
})
.attr('y', function(d) {
return ((d.y - 1) * poly._gridSize.y) + (poly._gridSize.y/2);
})
.style('text-anchor', 'middle')
.style('alignment-baseline','middle')
.attr('class', function(d, i) {
return poly.cellTextClassName + ' y'+d.y + ' x'+d.x;
})
.text(function(d) {
return d.value;
})
.style('fill',poly.textColors[0]);
if( this._isNew ) {
cellBackgrounds
.transition().duration(poly.duration).style('fill',function(d) {
return poly._bgColorScale(d.value);
});
cellTexts
.transition().duration(poly.duration*1.25).style('fill',function(d) {
return poly._textColorScale(d.value);
});
} else {
cellTexts
.style('fill',function(d) {
return poly._textColorScale(d.value);
});
cellBackgrounds
.style('fill',function(d) {
return poly._bgColorScale(d.value);
});
}
this._isNew = false;
},
});
</script>
</polymer-element>