This repository has been archived by the owner on Dec 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show.eco
317 lines (276 loc) · 9.3 KB
/
show.eco
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
<div class="split-test">
<h1><%= @title %></h1>
<p class="meta">This test was started <%= Date.create(@created).relative() %></p>
<div id="alternatives" class="alternatives">
<% for i, alternative of @alternatives: %>
<div class="alternative" id="alt-<%= i %>">
<h2><%= alternative.title %></h2>
<big></big>
<small></small>
<div class="chart days"><div class="title">Day of week</div></div>
<div class="chart hours"><div class="title">Time of day</div></div>
</div>
<% end %>
</div>
</div>
<script>
$(function() {
var DAYS = ["S", "M", "T", "W", "T", "F", "S"];
var testId = document.location.pathname.split("/").last();
$.getJSON("/v1/split/" + testId, function(test) {
data = test.alternatives.map(function(alternative) { return alternative.data });
console.log(data)
d3.select("#alternatives")
.selectAll(".alternative").data(data)
.each(renderAlternative);
});
function renderAlternative(data) {
// The alternative container element
var el = d3.select(this);
// The reduce functions. These are called for each data point, and we need
// to tally the participants and completed so we can calculate conversion.
function reduceAdd(last, d) {
last.participants += d.participants;
last.converted += d.converted;
last.conversion = last.converted / last.participants;
return last;
}
function reduceRemove(last, d) {
last.participants -= d.participants;
last.converted -= d.converted;
last.conversion = last.converted / last.participants;
return last;
}
function reduceInitial() {
return { participants: 0, converted: 0 }
}
// The value function: we're interested in converted.
function value(d) {
return d.conversion || 0;
}
var xfilter = crossfilter(data),
day = xfilter.dimension(function(d) { return Date.create(d.time).getDay(); }),
days = day.group().reduce(reduceAdd, reduceRemove, reduceInitial).order(value),
hour = xfilter.dimension(function(d) { return Date.create(d.time).getHours(); }),
hours = hour.group().reduce(reduceAdd, reduceRemove, reduceInitial).order(value),
all = xfilter.groupAll().reduce(reduceAdd, reduceRemove, reduceInitial);
// Now let's do some charts.
var charts = [
barChart(0) // Day
.dimension(day).group(days).value(value)
.x(d3.scale.linear().domain([0, 7]).rangeRound([0, 10 * 8]))
.reset(resetChart),
barChart(1) // Hour
.dimension(hour).group(hours).value(value)
.x(d3.scale.linear().domain([0, 24]).rangeRound([0, 10 * 24]))
.reset(resetChart)
];
// Renders the specified chart or list.
function render(method) {
var value = all.value(),
conversion = value.conversion;
d3.select(this).call(method);
el.select("small").text(value.participants.format(0));
if (isNaN(conversion))
el.select("big").text("N/A");
else
el.select("big").text((conversion * 100).format(2) + "%");
}
var chart = el.selectAll(".chart").data(charts);
// Whenever the brush moves, re-rendering everything.
function renderAll() {
chart.each(render);
}
function resetChart(i) {
charts[i].filter(null);
renderAll();
}
chart.each(function(chart) { chart.on("brush", renderAll).on("brushend", renderAll); });
renderAll();
}
function barChart(id) {
if (id == undefined) {
if (!barChart.id)
barChart.id = 0;
id = barChart.id++;
}
var margin = { top: 10, right: 10, bottom: 20, left:10 },
x,
y = d3.scale.linear().range([100, 0]),
axis = d3.svg.axis().orient("bottom"),
brush = d3.svg.brush(),
value = function(d) { return d },
brushDirty,
dimension,
group,
round,
reset;
function chart(div) {
var width = d3.last(x.range()),
height = y.range()[0];
y.domain([0, value(group.top(1)[0].value)]);
div.each(function() {
var div = d3.select(this),
g = div.select("g");
// Create the skeletal chart.
if (g.empty()) {
div.select(".title").append("a")
.attr("class", "reset")
.text("reset")
.style("display", "none");
div.select(".title a").on("click", function() {
d3.event.preventDefault();
if (reset)
reset(id);
});
g = div.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
g.append("clipPath")
.attr("id", "clip-" + id)
.append("rect")
.attr("width", width)
.attr("height", height);
g.selectAll(".bar")
.data(["background", "foreground"])
.enter().append("path")
.attr("class", function(d) { return d + " bar"; })
.datum(group.all());
g.selectAll(".foreground.bar")
.attr("clip-path", "url(#clip-" + id + ")");
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(axis);
// Initialize the brush component with pretty resize handles.
var gBrush = g.append("g").attr("class", "brush").call(brush);
gBrush.selectAll("rect").attr("height", height);
gBrush.selectAll(".resize").append("path").attr("d", resizePath);
}
// Only redraw the brush if set externally.
if (brushDirty) {
brushDirty = false;
g.selectAll(".brush").call(brush);
div.select(".title a").style("display", brush.empty() ? "none" : null);
if (brush.empty()) {
g.selectAll("#clip-" + id + " rect")
.attr("x", 0)
.attr("width", width);
} else {
var extent = brush.extent();
g.selectAll("#clip-" + id + " rect")
.attr("x", x(extent[0]))
.attr("width", x(extent[1]) - x(extent[0]));
}
}
g.selectAll(".bar").attr("d", barPath);
});
function barPath(groups) {
var path = [],
i = -1,
n = groups.length,
d;
while (++i < n) {
d = groups[i];
path.push("M", x(d.key), ",", height, "V", y(value(d.value)), "h9V", height);
}
return path.join("");
}
function resizePath(d) {
var e = +(d == "e"),
x = e ? 1 : -1,
y = height / 3;
return "M" + (.5 * x) + "," + y
+ "A6,6 0 0 " + e + " " + (6.5 * x) + "," + (y + 6)
+ "V" + (2 * y - 6)
+ "A6,6 0 0 " + e + " " + (.5 * x) + "," + (2 * y)
+ "Z"
+ "M" + (2.5 * x) + "," + (y + 8)
+ "V" + (2 * y - 8)
+ "M" + (4.5 * x) + "," + (y + 8)
+ "V" + (2 * y - 8);
}
}
brush.on("brushstart.chart", function() {
var div = d3.select(this.parentNode.parentNode.parentNode);
div.select(".title a").style("display", null);
});
brush.on("brush.chart", function() {
var g = d3.select(this.parentNode),
extent = brush.extent();
if (round) g.select(".brush")
.call(brush.extent(extent = extent.map(round)))
.selectAll(".resize")
.style("display", null);
g.select("#clip-" + id + " rect")
.attr("x", x(extent[0]))
.attr("width", x(extent[1]) - x(extent[0]));
dimension.filterRange(extent);
});
brush.on("brushend.chart", function() {
if (brush.empty()) {
var div = d3.select(this.parentNode.parentNode.parentNode);
div.select(".title a").style("display", "none");
div.select("#clip-" + id + " rect").attr("x", null).attr("width", "100%");
dimension.filterAll();
}
});
chart.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
return chart;
};
chart.x = function(_) {
if (!arguments.length) return x;
x = _;
axis.scale(x);
brush.x(x);
return chart;
};
chart.y = function(_) {
if (!arguments.length) return y;
y = _;
return chart;
};
chart.dimension = function(_) {
if (!arguments.length) return dimension;
dimension = _;
return chart;
};
chart.filter = function(_) {
if (_) {
brush.extent(_);
dimension.filterRange(_);
} else {
brush.clear();
dimension.filterAll();
}
brushDirty = true;
return chart;
};
chart.group = function(_) {
if (!arguments.length) return group;
group = _;
return chart;
};
chart.round = function(_) {
if (!arguments.length) return round;
round = _;
return chart;
};
chart.value = function(_) {
if (!arguments.length) return value;
value = _;
return chart;
};
chart.reset = function(_) {
if (!arguments.length) return reset;
reset = _;
return chart;
};
return d3.rebind(chart, brush, "on");
}
})
</script>