-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
146 lines (117 loc) · 5.15 KB
/
Copy pathindex.html
File metadata and controls
146 lines (117 loc) · 5.15 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
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
<!-- partially inspired by this tutorial : https://www.youtube.com/watch?v=jtVATktKAtM&list=PL4WVvNtU-vPAnqjTCN5_uVBFz5C3HvDck&index=8 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Pollen concentration with checkboxes</title>
</head>
<body>
<h1 style="margin-left: 2em; ">Concentration pollinique au Luxembourg, 1992-2018 </h1>
<p style="font-size: medium; margin-left: 4em; margin-right: 4em; text-align: justify;">Sélectionnez/déselectionnez les cases sous le graphique pour afficher les pollens (il est peut-être nécessaire de descendre sous le graphique selon l'affichage sur votre écran). <br>Pour des informations concernant l'interprétation des données, voir ce <a href="https://www.meteoswiss.admin.ch/dam/jcr:f3d0942c-b3ab-4de6-882e-5df909faed9c/threshold-values-for-pollen-load-classes-of-allergenic-pollen-types.pdf" target="_blank">tableau de la confédération suisse</a> à propos des valeurs seuils pour les classes de charge pollinique des types de pollens allergisants. La concentration de certains pollens au <var>m<sup>3</sup></var> est considérée comme très forte à partir d'environ 50, par exemple pour l'armoise commune (<i>artemisia</i>), alors que pour d'autres la concentration doit atteindre les 400 pour être considérée comme très forte, par exemple pour le platane (<i>platanus</i>).</p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script>
const margin = {top: 20, right: 80, bottom: 30, left: 50},
width = 1800 - margin. left - margin. right,
height = 700 - margin.top - margin. bottom;
// format date
const parseDate = d3. time.format("%Y-%m-%d").parse;
const x = d3.time.scale()
.range ([0, width]);
const y = d3.scale. linear()
.range ( [height, 0]);
const color = d3. scale. category10();
// x and y axes
const xAxis = d3. svg.axis ()
.scale (x)
.orient ("bottom");
const yAxis = d3. svg.axis ()
.scale (y)
.orient ("left");
const line = d3.svg.line()
.interpolate ("basis")
.x(function(d) { return x(d. date); })
.y(function(d) { return y(d.concentration); });
let 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 + ")");
// load data
d3.csv ("pollen_tri.csv", function(error, data) {
color.domain (d3.keys (data[0]).filter(function (key) { return key !== "date"; }));
data.forEach(function(d) {
d.date = parseDate (d.date);
});
var plants = color.domain().map (function (name) {
return {
name: name,
values: data.map(function (d) {
return {date: d.date, concentration: +d [name]};
})
};
});
x.domain (d3.extent (data, function(d) { return d.date; }));
y.domain([
d3.min(plants, function(c) { return d3.min(c.values, function(v) {return v.concentration;}); }),
d3.max(plants, function(c) { return d3.max(c.values, function(v) { return v.concentration;});})
]);
// call x axe
svg.append("g")
.attr("class" , "x axis")
.attr("transform", "translate(0," + height + ")")
.call (xAxis);
// call y axe
svg.append ("g")
.attr("class", "y axis")
.call (yAxis)
.append ("text")
. attr("transform", "rotate (-90)")
.attr("y", 6)
.attr("dy", " .71em")
.style("text-anchor", "end")
.text ("Concentration (m3)");
let plant = svg.selectAll(".plant")
.data(plants)
.enter()
.append ("g")
.attr("class", "plant");
// Add checkboxes, inspired by the code of @Peter F : https://stackoverflow.com/questions/77336948/how-to-hide-show-specific-lines-with-a-checkbox-on-a-line-chart/77583395#77583395
const mapPlantNameToSvgPath = {};
plant.append("path")
.attr("class", "line")
.attr("d", function (d) { return line(d.values); })
.style("stroke", function (d) { return color(d.name); })
// https://d3js.org/d3-selection/control-flow#selection_each
.each(function (d) {
const svgPath = this;
const plantName = d.name;
mapPlantNameToSvgPath[plantName] = svgPath;
})
color.domain().forEach(function (plantName) {
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label
const label = document.createElement("label");
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox
const checkbox = document.createElement("input");
checkbox.setAttribute("type", "checkbox");
checkbox.setAttribute("checked", "true");
checkbox.addEventListener("change", function () {
const svgPath = mapPlantNameToSvgPath[plantName];
// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/visibility
if (checkbox.checked) {
svgPath.setAttribute("visibility", "visible");
} else {
svgPath.setAttribute("visibility", "hidden");
}
})
label.append(checkbox);
label.append(document.createTextNode(plantName));
document.body.append(document.createElement("br"));
document.body.append(label);
});
});
</script>
</body>
</html>