-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD3.js-ntu.html
More file actions
97 lines (97 loc) · 2.42 KB
/
D3.js-ntu.html
File metadata and controls
97 lines (97 loc) · 2.42 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
<!DOCTYPE html>
<!-- https://www.cc.ntu.edu.tw/chinese/epaper/0040/20170320_4004.html -->
<html>
<head>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<title>D3.js Demo </title>
<style>
.wrap{
position: relative;
overflow: hidden;
margin-bottom: 1em;
}
.bar{
background-color: navy;
width: 2em;
height: auto;
margin-right: 5px;
float: left;
position: relative;
color: #fff;
text-align: center;
padding-top: 5px;
}
button{
font-size: 1.5em; float: left;
margin-right: 10px;
}
</style>
</head>
<body>
<script>
var data = [1, 2, 3, 4, 5];
var height = 250, width = 300;
// body 與 容器
var body = d3.select('body');
var wrap = body.append('div')
.style({
'height': height + 'px'
})
.classed('wrap', true);
// render, & update
var render = function () {
wrap.selectAll('.bar')
.data(data)
.enter()
.append('div')
.classed('bar', true)
.text(function (d) {
return d;
})
.style({
'height': function (d) {
return d * 25 + 'px';
},
'top': function (d) {
return (height - d * 25) + 'px';
}
});
};
// remove
var remove = function () {
wrap.selectAll('.bar')
.data(data)
.text(function (d) {
return d;
})
.style({
'height': function (d) {
return d * 25 + 'px';
},
'top': function (d) {
return (height - d * 25) + 'px';
}
})
.exit()
.remove();
};
// 繪製原始資料
render();
// 兩顆按鈕
body.append('button')
.classed('add', true)
.text('add');
body.append('button')
.classed('remove', true)
.text('remove');
d3.select('.add').on('click', function () {
data.push(Math.floor(Math.random() * 10 + 1));
render();
});
d3.select('.remove').on('click', function () {
data.pop();
remove();
});
</script>
</body>
</html>