-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiples.html
More file actions
117 lines (94 loc) · 2.81 KB
/
Copy pathmultiples.html
File metadata and controls
117 lines (94 loc) · 2.81 KB
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
margin: 0;
}
.line {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
.area {
fill: lightsteelblue;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 8, right: 10, bottom: 2, left: 10},
width = 960 - margin.left - margin.right,
height = 69 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var area = d3.svg.area()
.x(function(d) { return x(d.timestamp); })
.y0(height)
.y1(function(d) { return y(d.sleep); });
var line = d3.svg.line()
.x(function(d) { return x(d.timestamp); })
.y(function(d) { return y(d.sleep); });
d3.csv("simple_sleep_output.csv", type, function(error, data) {
// Nest data by the_day.
var the_days = d3.nest()
.key(function(d) { return d.the_day; })
.entries(data);
// Compute the maximum sleep per the_day, needed for the y-domain.
the_days.forEach(function(s) {
s.maxPrice = d3.max(s.values, function(d) { return d.sleep; });
});
// Compute the minimum and maximum timestamp across the_days.
// We assume values are sorted by timestamp.
x.domain([
d3.min(the_days, function(s) { return s.values[0].timestamp; }),
d3.max(the_days, function(s) { return s.values[s.values.length - 1].timestamp; })
]);
// Add an SVG element for each the_day, with the desired dimensions and margin.
var svg = d3.select("body").selectAll("svg")
.data(the_days)
.enter().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 + ")");
// Add the area path elements. Note: the y-domain is set per element.
svg.append("path")
.attr("class", "area")
.attr("d", function(d) { y.domain([0, d.maxPrice]); return area(d.values); });
// Add the line path elements. Note: the y-domain is set per element.
svg.append("path")
.attr("class", "line")
.attr("d", function(d) { y.domain([0, d.maxPrice]); return line(d.values); });
// Add an x axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add a small label for the the_day name.
svg.append("text")
.attr("x", width - 6)
.attr("y", height - 6)
.style("text-anchor", "end")
.text(function(d) { return d.key; });
});
function type(d) {
d.sleep = +d.sleep;
d.timestamp = +d.timestamp;
return d;
}
</script>