-
Notifications
You must be signed in to change notification settings - Fork 0
/
D3_4.html
105 lines (90 loc) · 3.19 KB
/
D3_4.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="./D3.js" charset="utf-8"></script>
</head>
<body>
<button onclick="updateData()">更新</button>
<button onclick="addCircle()">新增</button>
<button onclick="removeCircle()">删除</button>
</body>
<script charset="utf-8">
var width = 400;
var height = 400;
var center = [[ 0.5,0.5],[ 0.7,0.8],[ 0.4,0.9], [0.11,0.32],[ 0.88,0.25],[ 0.75,0.12], [0.5,0.1],[ 0.2,0.3],[ 0.4,0.1],[ 0.6,0.7]];
var xAxisWidth = 300;
var yAxisWidth = 300;
var padding = { top: 30 , right: 30, bottom: 30, left: 30 };
var xScale = d3.scaleLinear().domain([0,1.2*d3.max(center,function(d){
return d[0];
})]).range([0,xAxisWidth]);
var yScale = d3.scaleLinear().domain([0,1.2*d3.max(center,function(d){
return d[1];
})]).range([0,yAxisWidth]);
var xAxis = d3.axisBottom(xScale);
yScale.range([yAxisWidth,0]);
var yAxis = d3.axisLeft(yScale);
var svg = d3.select('body').append('svg').attr('width',width).attr('height',height);
var circle = svg.selectAll('circle').data(center).enter()
.append('circle')
.attr('cx',padding.left)
.attr('cy',height - padding.bottom)
.transition().duration(1000)
.attr('fill','orange')
.attr('cx',function(d){
return padding.left + xScale(d[0]);
})
.attr('cy',function(d){
return height - padding.bottom - yScale(d[1]);
})
.attr('r',5);
svg.append('g').attr('transform', 'translate(' + padding.left + ',' + (height - padding.bottom) + ')').call(xAxis);
svg.append('g').attr('transform', 'translate(' + padding.left + ',' + (height - yAxisWidth - padding.bottom) + ')').call(yAxis);
function circleDraw(){
var circleUpdate = svg.selectAll('circle').data(center);
var circleEnter = circleUpdate.enter();
var circleExit = circleUpdate.exit();
circleUpdate.transition().duration(1000)
.attr('fill','orange')
.attr('cx',function(d){
return padding.left + xScale(d[0]);
})
.attr('cy',function(d){
return height - padding.bottom - yScale(d[1]);
})
.attr('r',5);
circleEnter.append('circle')
.attr('cx',padding.left)
.attr('cy',height - padding.bottom)
.transition().duration(1000)
.attr('fill','orange')
.attr('cx',function(d){
return padding.left + xScale(d[0]);
})
.attr('cy',function(d){
return height - padding.bottom - yScale(d[1]);
})
.attr('r',5);
circleExit.transition().duration(1000).attr('fill','white').remove();
}
function updateData(){
for(var i=0;i<center.length;i++){
center[i][0] = Math.random();
center[i][1] = Math.random();4
}
circleDraw();
}
function addCircle(){
center.push([Math.random(),Math.random()]);
circleDraw();
}
function removeCircle(){
center.pop();
circleDraw();
}
</script>
</html>