-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3.timecard.js
257 lines (224 loc) · 6.18 KB
/
d3.timecard.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
/*! D3punchcard v0.1.0 - MIT license */
;(function (global) { function moduleDefinition( d3 ) {
function D3punchcard( options ) {
// Reverse the data as we draw
// from the bottom up.
this.data = options.data.reverse();
this.element = options.element;
// Find the max value to normalize the size of the circles.
this.max = d3.max( this.data , function(array) {
// we ignore the first element as it is metadata
return d3.max(array.slice(1), function ( obj ) {
// and we only return the interger verion of the value, not the key
return parseFloat( obj.value );
});
});
// set the upperlimit if we have it
// otherwise use the max
if( options.upperLimit ){
this.upperLimit = options.upperLimit;
} else {
this.upperLimit = this.max;
}
return this;
}
D3punchcard.prototype.draw = function( options ){
var _this = this,
margin = 10,
lineHeight = 5,
width = options.width,
paneLeft = 80,
paneRight = width - paneLeft,
sectionHeight = 35,
height = ( sectionHeight * this.data.length ),
sectionWidth = paneRight / this.data[0].length,
circleRadius = 14,
x,
y,
punchcard,
punchcardRow,
xAxis,
rScale;
// X-Axis.
x = d3.scale.linear().domain([0, this.data[0].length-1]).
range([ paneLeft + (sectionWidth / 2) , paneRight + (sectionWidth / 2)]);
// Y-Axis.
y = d3.scale.linear().domain([0, this.data.length-1]).
range([0, height - sectionHeight]);
rScale = d3.scale.linear()
.domain([0, this.upperLimit, this.max])
.range([0, circleRadius, circleRadius]);
// these functions hide and show the circles and text values
function handleRowMouseover(){
var g = d3.select(this).node().parentNode;
d3.select(g).selectAll('circle').style('display','none');
d3.select(g).selectAll('text.value').style('display','block');
}
function handleRowMouseout(){
var g = d3.select(this).node().parentNode;
d3.select(g).selectAll('circle').style('display','block');
d3.select(g).selectAll('text.value').style('display','none');
}
// The main SVG element.
punchcard = d3.select(this.element)
.html('')
.append('svg')
.attr('width', width )
.attr('height', height + (margin*3))
.append('g');
// create the x axis holder
xAxis = punchcard.selectAll('.row')
.data( [this.data[0].slice(1)] )
.enter()
.append('g')
.attr('class', 'xaxis');
// create the x axis line
xAxis.
append('line').
attr('x1', 0).
attr('x2', width).
attr('y1', (margin * 3)).
attr('y2', (margin * 3)).
style('stroke-width', 1).
style('stroke', '#efefef');
// create x-axis ticks
xAxis.
selectAll('line.tick').
data(function(d, i ) {
return d;
}).
enter().
append('line').
attr('class', 'tick').
attr('x1', function(d,i) { return paneLeft + x(i); }).
attr('x2', function(d,i) { return paneLeft + x(i); }).
attr('y1', function (d, i) {
return margin * 2;
}).
attr('y2', function (d, i) {
return (margin * 3);
}).
style('stroke-width', 1).
style('stroke', '#efefef');
// create x-axis tick text.
xAxis.
selectAll('.rule').
data(function(d, i ) {
return d;
}).
enter().
append('text').
attr('class', 'rule').
attr('x', function(d, i) {
return paneLeft + x(i);
}).
attr('y', margin + lineHeight).
attr('text-anchor', 'middle').
text(function(d) {
return d.key;
});
// create rows
punchcardRow = punchcard.selectAll('.row')
.data( this.data )
.enter()
.append('g')
.attr('class', 'row')
.attr('transform', function(d, i) {
var ty = height - y(i) - (sectionHeight/2) + (margin*3);
return 'translate(0, ' + ty + ')';
});
// create row divinding lines
punchcardRow.
selectAll('line').
data([0]).
enter().
append('line').
attr('x1', 0).
attr('x2', width).
attr('y1', (sectionHeight/2)).
attr('y2', (sectionHeight/2)).
style('stroke-width', 1).
style('stroke', '#efefef');
// create row headers
punchcardRow.
selectAll('.textheader').
data( function(d, i ) {
// we only return the first element of each array
// which contains the header text
return [d[0]];
} ).
enter().
append('text').
attr('x', 0).
attr('y', function (d, i) {
return lineHeight;
}).
attr('class', 'textheader').
attr('text-anchor', 'left').
text(function (d, i) {
return d.value;
})
.on('mouseover', handleRowMouseover)
.on('mouseout', handleRowMouseout);
// draw circles for each row
punchcardRow.
selectAll('circle').
data( function(d, i ) {
return d.slice(1);
} ).
enter().
append('circle').
style('fill', '#888').
attr('r', function(d, i) {
return rScale( parseFloat( d.value ) );
}).
attr('transform', function(d, i) {
var tx = paneLeft + x(i);
return 'translate(' + tx + ', 0)';
});
// draw text values for each row
punchcardRow.
selectAll('text.value').
data( function(d, i ) {
return d.slice(1);
} ).
enter().
append('text').
attr('class', 'value').
style('display','none').
text(function (d, i) {
return d.value;
}).
attr('text-anchor', 'middle').
attr('x', function (d, i) {
return paneLeft + x(i);
}).
attr('y', function (d, i) {
return lineHeight;
});
return this;
};
// to be called when closing a view
// in which this object has been created
D3punchcard.prototype.destroy = function () {
// remove event listeners
d3.select(this.element)
.selectAll('text.textheader')
.on('mouseover', null)
.on('mouseout', null);
};
/**
* Expose D3punchcard
*/
return D3punchcard;
// ---------------------------------------------------------------------------
} if (typeof exports === 'object') {
// node export
module.exports = moduleDefinition(require('d3'));
} else if (typeof define === 'function' && define.amd) {
// amd anonymous module registration
define(['d3'], moduleDefinition);
} else {
// browser global
global.D3punchcard = moduleDefinition(global.d3);
}}(this));