-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathEDAV7_3_obj_constancy.html
executable file
·159 lines (120 loc) · 3.87 KB
/
EDAV7_3_obj_constancy.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>EDAV7_3_obj_constancy</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<h2>Object constancy</h2>
<p id="add">Add an element</p>
<p id="remove_left">Remove bar (left)</p>
<p id="remove_right">Remove bar (right)</p>
<script>
// Create svg and initial bars
var w = 500;
var h = 400;
var margin = {top: 25, right: 0, bottom: 25,
left: 25};
var innerWidth = w - margin.left - margin.right;
var innerHeight = h - margin.top - margin.bottom;
var dur = 500;
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", w)
.attr("height", h)
.attr("fill", "lightblue");
var bardata = [300, 100, 150, 220, 70, 270]
.map((value, key) => ({key, value})); // NEW
var xScale = d3.scaleBand()
.domain(d3.range(bardata.length))
.range([0, innerWidth])
.paddingInner(.1);
var yScale = d3.scaleLinear()
.domain([0, d3.max(bardata, d => d.value)]) // CHANGE
.range([innerHeight, 0]);
var xAxis = d3.axisBottom()
.scale(xScale);
var yAxis = d3.axisLeft()
.scale(yScale);
var bars = svg.append("g")
.attr("id", "plot")
.attr("transform", `translate (${margin.left}, ${margin.top})`)
.selectAll("rect")
.data(bardata, d => d.key); // CHANGE
bars.enter().append("rect")
.attr("class", "bar")
.attr("x", (d, i) => xScale(i))
.attr("y", d => yScale(d.value)) // CHANGE
.attr("width", xScale.bandwidth())
.attr("height", d => innerHeight - yScale(d.value)) // CHANGE
.attr("fill","blue");
svg.append("g")
.attr("class", "xAxis")
.attr("transform", `translate (${margin.left}, ${h - margin.bottom})`)
.call(xAxis);
svg.append("g")
.attr("class", "yAxis")
.attr("transform", `translate (${margin.left}, ${margin.top})`)
.call(yAxis);
// General Update Pattern
function update(data, exitpos) {
xScale.domain(d3.range(data.length));
yScale.domain([0, d3.max(data, d => d.value)]);
var bars = svg.select("#plot")
.selectAll("rect")
.data(data, d => d.key); // CHANGE
bars.enter().append("rect")
.attr("x", w)
.attr("y", d => yScale(d.value)) // CHANGE
.attr("width", xScale.bandwidth())
.attr("height", d => innerHeight - yScale(d.value))
.attr("fill", "blue")
.merge(bars)
.transition()
.duration(dur)
.ease(d3.easeLinear)
.attr("x", (d, i) => xScale(i))
.attr("y", d => yScale(d.value)) // CHANGE
.attr("width", xScale.bandwidth())
.attr("height", d => innerHeight - yScale(d.value)) // CHANGE
bars.exit()
.transition() // NEW
.duration(dur)
.attr("x", exitpos)
.remove();
svg.select(".xAxis")
.transition()
.duration(dur)
.ease(d3.easeLinear)
.call(xAxis);
svg.select(".yAxis")
.transition()
.duration(dur)
.ease(d3.easeLinear)
.call(yAxis);
}
d3.selectAll("p")
.on("click", function () {
var paraID = d3.select(this).attr("id");
if (paraID == "add") {
var newvalue = Math.floor(Math.random()*400);
var newkey = bardata[bardata.length-1].key + 1;
bardata.push({key: newkey, value: newvalue } ); // NEW
} else if (paraID == "remove_left") {
bardata.shift();
var exitpos = -xScale.bandwidth();
} else {
bardata.pop();
var exitpos = w;
};
update(bardata, exitpos);
});
</script>
</body>
</html>