-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
229 lines (203 loc) · 6.49 KB
/
index.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
margin: 15px;
background-color: #F1F3F3
}
.bar {
fill: #6F257F;
}
.axis path,
.axis line {
fill: none;
stroke: #D4D8DA;
stroke-width: 1px;
shape-rendering: crispEdges;
}
.x path {
display: none;
}
.toolTip {
position: absolute;
display: none;
min-width: 80px;
height: auto;
background: none repeat scroll 0 0 #ffffff;
border: 1px solid #6F257F;
padding: 14px;
text-align: center;
}
</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 100, bottom: 30, left: 120},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var x = d3.scaleLinear().range([0, width]);
var y = d3.scaleBand().range([height, 0]);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var xAxis,bars,labels,numLabels,steps = 0;
var format = d3.format(",d");
var duration=2000;
// var yAxis;
var globalData;
d3.json("data.json", function(error, data) {
if (error) throw error;
xAxis = g.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")");
// yAxis = g.append("g")
// .attr("class", "y axis");
bars = g.append("g").attr("id","bars");
labels = g.append("g").attr("id","labels");
numLabels = g.append("g").attr("id","numLabels");
//绘制bar图
// draw(data[0].data)
globalData=data;
requestAnimationFrame(function() {
draw(globalData[steps].data);
});
});
var draw=function(data){
data = data.map(d=>{return {name:d.name,value:Number(d.value)}}).sort(function(a, b) { return b.value - a.value; })
.slice(0,10).reverse();
x.domain([0, d3.max(data, function(d) { return d.value; })]);
y.domain(data.map(function(d) { return d.name; })).padding(0.3);
console.log(data);
xAxis
.transition()
.ease(d3.easeLinear)
.duration(duration)
.call(d3.axisBottom(x).ticks(5).tickFormat(function(d) { return parseInt(d / 1000); }).tickSizeInner([-height]));
console.log(data);
//坐标轴上绘制名称虽然方便但是却不利于进行动画绘制
// yAxis
// .transition()
// .call(d3.axisLeft(y));
var labelsData = labels.selectAll(".label")
.data(data, function(d) { return d ? d.name : this.id; });
var barData = bars.selectAll(".bar")
.data(data, function(d) { return d ? d.name : this.id; });
var numLabelsData = numLabels.selectAll(".numLabel")
.data(data, function(d) { return d ? d.name : this.id; });
//enter
barData.enter().append("rect")
.attr("class", "bar")
.attr('y',height)
.attr("width", 0)
.transition()
.ease(d3.easeLinear)
.duration(duration)
.attr("x", 0)
.attr("height", y.bandwidth())
.attr("y", function(d) { return y(d.name); })
.attr("width", function(d) { return x(d.value); });
//update
barData.transition()
.ease(d3.easeLinear)
.duration(duration)
.attr("x", 0)
.attr("height", y.bandwidth())
.attr("y", function(d) { return y(d.name); })
.attr("width", function(d) { return x(d.value); });
//exit
barData.exit()
.transition()
.ease(d3.easeLinear)
.duration(duration)
.attr('y',height+20)
.attr("width", function(d) { return x(d.value); })
.remove();
//--------------- 类别标签
labelsData.enter().append("text")
.attr("class","label")
.attr("y",height)
.attr('dx','-5')
.attr("dy",y.bandwidth()/2)
.attr("text-anchor",'end')
.style("alignment-baseline",'central')
.text(d=>d.name)
.transition()
.ease(d3.easeLinear)
.duration(duration)
.attr("y", function(d) { return y(d.name); })
.on("end", function(){
steps++;
if(steps < globalData.length){
draw(globalData[steps].data);
}
});
labelsData.transition()
.ease(d3.easeLinear)
.duration(duration)
.attr("y", function(d) { return y(d.name); });
labelsData.exit()
.transition()
.ease(d3.easeLinear)
.duration(duration)
.attr('y',height+20)
.remove();
//--------------- 数字标签
numLabelsData.enter().append("text")
.attr("class","numLabel")
.attr("y",height)
.attr("x",0)
.attr('dx',0)
.attr("dy", y.bandwidth()/2)
.attr("text-anchor",'start')
.style("alignment-baseline",'central')
// .text(d=>d.value)
.text(0)
.transition()
.ease(d3.easeLinear)
.duration(duration)
.attr("x", function(d) { return x(d.value); })
.attr("y", function(d) { return y(d.name); })
.on("start", function (d) {
d3.active(this)
.tween("text", function() {
var that = d3.select(this),
i = d3.interpolateNumber(that.text().replace(/,/g, ""), d.value);
return function(t) { that.text(format(i(t))); };
});
});
numLabelsData.transition()
.ease(d3.easeLinear)
.duration(duration)
.attr("x", function(d) { return x(d.value); })
.attr("y", function(d) { return y(d.name); })
.on("start", function (d) {
d3.active(this)
.tween("text", function() {
var that = d3.select(this),
i = d3.interpolateNumber(that.text().replace(/,/g, ""), d.value);
return function(t) { that.text(format(i(t))); };
});
});
numLabelsData.exit()
.transition()
.ease(d3.easeLinear)
.duration(duration)
.attr('y',height+20)
.attr("x", function(d) { return x(d.value); })
.on("start", function (d) {
d3.active(this)
.tween("text", function() {
var that = d3.select(this),
i = d3.interpolateNumber(that.text().replace(/,/g, ""), d.value);
return function(t) { that.text(format(i(t))); };
});
})
.remove();
// if(steps < globalData.length){
// requestAnimationFrame(function() {
// draw(globalData[steps].data);
// console.log("update");
// });
// }
}
</script>