-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart.html
181 lines (151 loc) · 5.52 KB
/
chart.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
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
<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */
body { font: 12px Arial;}
.axis path,
.axis line {
fill: none;
stroke: grey; /* axes */
stroke-width: 1; /* axes */
shape-rendering: crispEdges;
}
.tooltip {
position: absolute;
font-size: 12px;
width: auto;
height: auto;
background-color: white;
}
</style>
<body>
<!-- load the d3.js library -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var url_string = window.location.href;
var url = new URL(url_string);
var csv_file = url.searchParams.get("csv");
// Set the dimensions of the canvas / graph
var margin = {top: 130, right: 20, bottom: 30, left: 150},
width = 1200 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;
// Set the ranges
var x = d3.scale.linear().range([0, width/2]);
var y = d3.scale.linear().range([height/2, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(15);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(15);
// Adds the svg canvas
var svg = d3.select("body")
.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 + ")");
// Get the data
d3.csv(csv_file, function(error, data) {
var mappedArray = d3.entries(data[0]);
// console.log(mappedArray);
data.forEach(function(d) {
d.minutes = +d.minutes / 60;
for (var l = 0; l < 10; l++)
{ d[mappedArray[l + 1].key] = d[mappedArray[l + 1].key];
// console.log(d[mappedArray[l + 1].key])
}
});
// Scale the range of the data
maxx = +d3.max(data, function(d) { return d.minutes; })
maxx = 250
x.domain([0, maxx]);
y.domain([0, 100]);
tic = maxx/24 + 1
xAxis.ticks(tic);
var colors = ["orange", "orange", "lightblue", "lightblue", "blue", "blue", "limegreen", "limegreen"];
var interpolation = ["step-after", "step-after", "linear", "linear", "linear", "linear", "linear", "linear"]
svg.append("path")
.attr("stroke", colors[0])
.attr("stroke-width", 2)
.attr("fill", colors[0])
.attr("d", d3.svg.line()
.x(function(d) { return x(d.minutes); })
.interpolate(interpolation[0])
.y(function(d) { return y(d[mappedArray[1].key]); })(data));
for (var l = 1; l < 8; l++)
{ if (l % 2 == 0)
{ svg.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("r", 2.5)
.attr("fill", colors[l])
.attr("stroke", colors[l])
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", 1);
div.html(d.datetime + "<br/><a target=_blank href=http://natural-interaction.s3-website-eu-west-1.amazonaws.com/" + d.image + ">hires image</a><br/>" + parseInt(d.minutes) + " hours<br/><font color=" + colors[6] + ">" + mappedArray[6 + 2].key + " " + parseInt(d[mappedArray[6 + 1].key]) + "%<br/><font color=" + colors[4] + ">" + mappedArray[4 + 2].key + " " + parseInt(d[mappedArray[4 + 1].key]) + "%<br/><font color=" + colors[2] + ">" + mappedArray[2 + 2].key + " " + parseInt(d[mappedArray[2 + 1].key]) + "%<br/><font color=" + colors[0] + ">" + mappedArray[0 + 2].key + " " + parseInt(d[mappedArray[0 + 1].key]) + "%")
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY + 10) + "px");
})
.on("DISABLEDmouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0)})
.attr("cx", function(d) { return x(d.minutes); })
.attr("cy", function(d) { return y(d[mappedArray[l + 1].key]); });
}
}
for (var l = 2; l < 8; l++)
{ if (l % 2 == 1)
{ svg.append("path")
// .attr("class", "line" + (l + 1))
.attr("stroke", colors[l - 1])
.attr("stroke-width", 2)
.attr("fill", "none")
.attr("d", d3.svg.line()
.x(function(d) { return x(d.minutes); })
.interpolate(interpolation[l])
.y(function(d) { return y(d[mappedArray[l + 1].key]); })(data));
}
}
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height/2 + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("g")
.append("text")
.attr("class", "chart title")
.attr("text-anchor", "middle")
.attr("x", width/4)
.attr("y", -30)
.text(mappedArray[12].key + '/' + mappedArray[13].key + '-' + mappedArray[14].key + ' (started ' + mappedArray[11].key + ')');
svg.append("g")
.append("text")
.attr("class", "x label")
.attr("text-anchor", "middle")
.attr("x", width/4)
.attr("y", height/2 + 50)
.text("age (hours)");
for (var l = 1; l < 9; l+=2)
{
svg.append("g")
.append("text")
.attr("class", "y label")
.attr("text-anchor", "middle")
.attr("x", -100)
.attr("y", height/4 + 30 - 10 * l)
.text(mappedArray[l + 1].key + " (%)")
.style('fill', colors[l])
}
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
});
</script>
</body>