forked from jamesrom/jamesrom.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart.js
220 lines (186 loc) · 5.11 KB
/
chart.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
var Chart = (function() {
var self = {
margins: {top: 30, bottom: 70, left: 0, right: 30}
};
self.height = function() {
return $('#chart').height() - self.margins.top - self.margins.bottom;
};
self.width = function() {
return $('#chart').width() - self.margins.left - self.margins.right;
};
var svg = d3.select('#chart')
.append('svg:svg')
.attr({width: '100%', height: '100%'})
.append('g')
.attr('transform', 'translate(' + self.margins.left + ',' + self.margins.top + ')');
var xScale = d3.scale.linear()
.domain([0, 1])
.range([4, self.width()]);
var axisScale = d3.scale.linear()
.domain([0, 1])
.range([8, self.width() + 4]);
var yScale = d3.scale.linear()
.domain([60, 0])
.range([self.height(), 0]);
var xAxis = d3.svg.axis()
.scale(axisScale)
.tickFormat(d3.format("d"))
.orient('bottom');
var yAxis = d3.svg.axis()
.scale(yScale)
.orient('right')
.tickValues([60, 51, 41, 31, 21, 11, 0]);
var tip = d3.tip()
.attr('class', 'tooltip')
.offset([-12, 0])
.html(function(d) {
return '<div>Seconds Left: ' + d.seconds_left + '</div>'
+ '<div>Time Stamp: ' + d.now.format("YYYY-MM-DD HH:mm:ss") + '</div>'
+ '<div>Clicks: ' + d.clicks + '</div>';
});
var zoomLvl = 0;
var scrollLvl = 0;
svg.selectAll("line.grid").data(yAxis.tickValues()).enter()
.append("line")
.attr({
'class': 'grid',
'x1' : 4,
'x2' : self.width(),
'y1' : yScale,
'y2' : yScale,
'shape-rendering' : 'crispEdges',
})
.attr("stroke", function (d) {
switch (d) {
case 60: return "#6ea2d5";
case 0: return "#6ea2d5";
default: return flairColor(d);
}
})
svg.append('g')
.attr('class', 'x axis')
.attr('shape-rendering', 'crispEdges')
.attr('transform', 'translate(-4,' + self.height() + ')');
svg.append('g')
.attr('shape-rendering', 'crispEdges')
.attr('class', 'y axis')
.attr('transform', 'translate(' + self.width() + ',0)')
.append('rect')
.attr({
x: 0,
y: 0,
width: '100%',
height: self.height(),
fill: 'white'
});
svg.selectAll('g.y.axis')
.call(yAxis);
svg.call(tip);
// expose
self.svg = svg;
self.xScale = xScale;
self.yScale = yScale;
self.render = function(data) {
var clicks = _.filter(data, 'is_click');
Stats.resets = clicks.length;
xScale.domain([scrollLvl, clicks.length+1-zoomLvl+scrollLvl]);
axisScale.domain([scrollLvl, clicks.length+1-zoomLvl+scrollLvl]);
svg.selectAll('g.x.axis')
.call(xAxis);
yPixel = _.flow(_.property('seconds_left'), yScale);
flair = _.flow(_.property('seconds_left'), flairColor);
var rect = svg.selectAll('rect.bar').data(clicks);
rect.attr("class", "bar")
.attr('x', function(d, i) {
return xScale(i);
})
.attr('y', yPixel)
.attr('width', function(d, i) {
return xScale(i+1) - xScale(i);
})
.attr('height', function(d, i) {
return yScale(60) - yPixel(d)
})
.attr('fill', flair);
rect.enter()
.append('rect')
.attr("class", "bar")
.attr('shape-rendering', 'crispEdges')
.attr('x', function(d, i) {
return xScale(i);
})
.attr('y', yPixel)
.attr('width', function(d, i) {
return xScale(i+1) - xScale(i);
})
.attr('height', function(d, i) {
return yScale(60) - yPixel(d)
})
.attr('fill', flair)
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
rect.exit()
.remove();
//Put axis in front of bars in case they overlap
svg.select('.y.axis').moveToFront();
}
self.resize = function() {
if ($(window).width() < 400) {
$('#chart').addClass('small');
}
else {
$('#chart').removeClass('small');
}
xScale.range([4, self.width()]);
axisScale.range([8, self.width() + 4]);
yScale.range([Chart.height(), 0]);
var grids = svg.selectAll("line.grid")
.data(yAxis.tickValues());
grids.attr({
'class': 'grid',
'x1' : 4,
'x2' : self.width(),
'y1' : yScale,
'y2' : yScale,
'shape-rendering' : 'crispEdges',
})
grids.enter()
.append("line")
.attr({
'class': 'grid',
'x1' : 4,
'x2' : self.width(),
'y1' : yScale,
'y2' : yScale,
'shape-rendering' : 'crispEdges',
});
grids.exit()
.remove();
svg.selectAll('g.x.axis')
.attr('transform', 'translate(-4,' + Chart.height() + ')')
.call(xAxis);
svg.selectAll('g.y.axis')
.attr('transform', 'translate(' + Chart.width() + ',0)')
.call(yAxis);
svg.selectAll('g.y.axis rect')
.attr('height', self.height());
}
self.zoom = function(delta, pos) {
//Different zoom depending on level (accelerate zoom every time you zoom out by 50)
var v = delta * Math.floor((Stats.resets - zoomLvl)/50) + delta;
zoomLvl += v;
if (zoomLvl < 0) { zoomLvl = 0; }
if (zoomLvl > Stats.resets) { zoomLvl = Stats.resets; }
//Adjust scrolling to center on mouse
scrollLvl += pos / self.width() * v;
if (scrollLvl < 0) { scrollLvl = 0; }
if (scrollLvl > zoomLvl) { scrollLvl = zoomLvl; }
}
self.scroll = function(dist) {
//Adjust scrolling speed according to domain
scrollLvl += dist / self.width() * (xScale.domain()[1] - xScale.domain()[0]);
if (scrollLvl < 0) { scrollLvl = 0; }
if (scrollLvl > zoomLvl) { scrollLvl = zoomLvl; }
}
return self;
}());