forked from jtr13/D3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDAV5_1.html
executable file
·59 lines (43 loc) · 1.2 KB
/
EDAV5_1.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>EDAV5_1</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script id="s1">
// Create svg and initial bars
var svg = d3.select("body")
.append("svg")
.attr("width", "500")
.attr("height", "400");
var bardata = [300, 100, 150, 225, 75, 275];
var bars = svg.selectAll("rect")
.data(bardata);
bars.enter().append("rect")
.attr("x", "30")
.attr("y", (d, i) => i*50)
.attr("width", d => d)
.attr("height", "35")
.attr("fill", "lightgreen");
// General Update Pattern
function update(data) {
var bars = svg.selectAll("rect") // data join
.data(data);
bars.enter().append("rect") // add new elements
.attr("x", "30")
.attr("y", (d, i) => i*50)
.attr("width", d => d)
.attr("height", "35")
.attr("fill", "yellow")
.merge(bars) // merge
.transition()
.duration(2000)
.attr("width", d => d)
.attr("fill", "orange");
bars.exit().remove(); // remove extra elements
}
</script>
</body>
</html>