-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.html
More file actions
160 lines (136 loc) · 4.89 KB
/
test.html
File metadata and controls
160 lines (136 loc) · 4.89 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<html>
<meta charset="utf-8">
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v6.js"></script>
<!-- Initialize a select button -->
<select id="selectButton"></select>
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
<script>
// set the dimensions and margins of the graph
const margin = {top: 10, right: 30, bottom: 50, left: 60},
width = 600 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
const svg = d3.select("#my_dataviz")
.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})`);
d3.csv("https://raw.githubusercontent.com/alfaevc/covidexpense/main/data/cpidata.csv").then(function(data) {
// List of groups (here I have one group per column)
const allGroup = new Set(data.map(d => d.item_name))
// add the options to the button
d3.select("#selectButton")
.selectAll('myOptions')
.data(allGroup)
.enter()
.append('option')
.text(d => d) // text showed in the menu
.attr("value", d => d) // corresponding value returned by the button
const myColor = d3.scaleOrdinal()
.domain(allGroup)
.range(d3.schemeSet2);
// Add X axis --> it is a date format
const x = d3.scaleBand()
.range([ width, 0])
.domain(data.map(d => d.date))
.padding(0.8);
svg.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(x))
.selectAll("text")
.attr("transform", "translate(-10,0)rotate(-65)")
.style("text-anchor", "end")
.style( "font","8px times");
// Add Y axis
const y = d3.scaleLinear()
.domain([50, 250])
.range([ height, 0 ]);
svg.append("g")
.call(d3.axisLeft(y));
// Initialize line with first group of the list
const line = svg.append("path")
.datum(data.filter(function(d){return d.item_name=="All items"}))
.attr("fill", "none")
.attr("stroke", function(d){ return myColor("valueA") })
.attr("stroke-width", 1.5)
.attr("d", d3.line()
.curve(d3.curveBasis) // Just add that to have a curve instead of segments
.x(d => x(d.date))
.y(d => y(d.value))
)
const Tooltip = d3.select("#my_dataviz")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
.style("background-color", "white")
.style("border", "solid")
.style("border-width", "2px")
.style("border-radius", "5px")
.style("padding", "5px")
const mouseover = function(event,d) {
Tooltip
.style("opacity", 1)
}
const mousemove = function(event,d) {
Tooltip
.html("Exact value: " + d.value)
.style("left", `${event.layerX+10}px`)
.style("top", `${event.layerY}px`)
}
const mouseleave = function(event,d) {
Tooltip
.style("opacity", 0)
}
const dot = svg
.selectAll('circle')
.data(data.filter(function(d){return d.item_name=="All items"}))
.join('circle')
.attr("cx", d => x(d.date))
.attr("cy", d => y(d.value))
.attr("r", 2)
.style("fill", "white")
.attr("stroke", function(d){ return myColor("valueA") })
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
// A function that update the chart
function update(selectedGroup) {
// Create new data with the selection?
const dataFilter = data.filter(function(d){return d.item_name==selectedGroup})
// Give these new data to update line
line
.datum(dataFilter)
.transition()
.duration(1000)
.attr("d", d3.line()
.curve(d3.curveBasis) // Just add that to have a curve instead of segments
.x(d => x(d.date))
.y(d => y(d.value))
)
.attr("stroke", function(d){ return myColor(selectedGroup) })
dot
.data(dataFilter)
.transition()
.duration(1000)
.attr("cx", d => x(d.date))
.attr("cy", d => y(d.value))
.attr("r", 2)
.attr("stroke", function(d){ return myColor(selectedGroup) })
.attr("stroke-width", 1)
.attr("fill", "white")
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
}
// When the button is changed, run the updateChart function
d3.select("#selectButton").on("change", function(event,d) {
// recover the option that has been chosen
const selectedOption = d3.select(this).property("value")
// run the updateChart function with this selected option
update(selectedOption)
})
})
</script>