forked from dc-js/dc.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacked-bar.html
81 lines (68 loc) · 2.37 KB
/
stacked-bar.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>dc.js - Stacked Bar Example</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/dc.css"/>
<style>
.dc-chart g.chart-body {
clip-path: none;
}
</style>
</head>
<body>
<div class="container">
<script type="text/javascript" src="header.js"></script>
<div id="test"></div>
<script type="text/javascript" src="../js/d3.js"></script>
<script type="text/javascript" src="../js/crossfilter.js"></script>
<script type="text/javascript" src="../js/dc.js"></script>
<script type="text/javascript">
var chart = dc.barChart("#test");
d3.csv("morley.csv", function(error, experiments) {
experiments.forEach(function(x) {
x.Speed = +x.Speed;
});
var ndx = crossfilter(experiments),
runDimension = ndx.dimension(function(d) {return +d.Run;}),
speedSumGroup = runDimension.group().reduce(function(p, v) {
p[v.Expt] = (p[v.Expt] || 0) + v.Speed;
return p;
}, function(p, v) {
p[v.Expt] = (p[v.Expt] || 0) - v.Speed;
return p;
}, function() {
return {};
});
function sel_stack(i) {
return function(d) {
return d.value[i];
};
}
chart
.width(768)
.height(480)
.x(d3.scale.linear().domain([1,21]))
.margins({left: 80, top: 20, right: 10, bottom: 20})
.brushOn(false)
.clipPadding(10)
.title(function(d) {
return d.key + '[' + this.layer + ']: ' + d.value[this.layer];
})
.dimension(runDimension)
.group(speedSumGroup, "1", sel_stack('1'))
.renderLabel(true);
chart.legend(dc.legend());
dc.override(chart, 'legendables', function() {
var items = chart._legendables();
return items.reverse();
});
for(var i = 2; i<6; ++i)
chart.stack(speedSumGroup, ''+i, sel_stack(i));
chart.render();
});
</script>
</div>
</body>
</html>