-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarChart.html
92 lines (78 loc) · 2.3 KB
/
barChart.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/d3/7.8.5/d3.min.js"></script>
</head>
<body>
<script>
const data = [100, 200, 300, 400, 500, 600, 700, 800]
const xData = ['2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018']
const xAxisScale = d3.scaleBand().domain(xData).range([0, 400])
const yAxisScale = d3.scaleLinear().domain([0, 800]).range([400, 0])
// 创建画布空间
d3.select('body')
.append('svg')
.attr('width', 500)
.attr('height', 500)
.attr('class', 'svg')
// 定义x轴
const xAxis = d3.axisBottom(xAxisScale)
.ticks(8)
.tickSize(6)
.tickPadding(5)
// 绘制x轴
d3.select('.svg')
.append('g')
.attr('transform', 'translate(50,430)')
.attr('class', 'x-axis')
.call(xAxis)
// 定义y轴
const yAxis = d3.axisLeft(yAxisScale)
.ticks(8)
.tickSize(6)
.tickPadding(5)
// 绘制y轴
d3.select('.svg')
.append('g')
.attr('transform', 'translate(50,30)')
.attr('class', 'y-axis')
.call(yAxis)
// 绘制柱形
d3.selectAll('.x-axis .tick')
.append('rect')
.attr('class', 'rect')
.attr('transform', 'translate(-10, 0)')
.attr('x', 0)
.attr('width', 20)
.attr('height', 0)
.attr('y', 0)
.attr('fill', '#2e6be6')
// 定义柱形的过渡动画
.transition()
.duration(2000)
.delay((d,i) => 200 * i)
.ease(d3.easeBounce)
.attr('height', (d,i) => data[i]*0.5)
.attr('y', (d,i) => -data[i]*0.5)
// 绘制柱形上的数据标签
d3.selectAll('.x-axis .tick')
.append('text')
.attr('class', 'text')
.attr('transform', 'translate(0, 0)')
.attr('x', 0)
.attr('y', 0)
.text((d,i) => data[i])
.attr('fill', '#2e6be6')
// 定义数据标签的过渡动画
.transition()
.duration(2000)
.delay((d,i) => 200*i)
.ease(d3.easeBounce)
.attr('y', (d,i) => -data[i] * 0.5 - 5)
</script>
</body>
</html>