-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.html
75 lines (66 loc) · 1.92 KB
/
basic.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
<!DOCTYPE html>
<html lang="zh-cn">
<meta charset="utf-8">
<style>
</style>
<body>
<svg width="1400" height="1000"></svg>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
let data = new Array(10).fill(0).map(() => {
return (1 + Math.random() * 99).toFixed(0)
})
let dataName = ['a','b','c','d','e','f','g','h','i','j']
let barWidth = 50
// 绘制x轴
let x = d3.scaleLinear() // 线性比例尺
.domain([0,100]) // 定义域
.range([20,1020]) // 值域
let xAxis = g => g.call(d3.axisBottom(x).ticks(10))
let y = d3.scaleBand()
.domain(dataName)
.range([20, 520])
let yAxis = g => g.call(d3.axisRight(y).tickSizeOuter(0))
let svg = d3.select("svg")
svg.append('g').call(xAxis)
svg.append('g').call(yAxis)
// 绘制柱形
let barGroup = svg.append('g').selectAll('rect')
.data(data).enter()
.append('rect')
.attr('x', 20)
.attr('y', (d,i) => {
return i * (500 / 10) + 20
})
.attr('width', 0)
.transition().duration(1000)
.attr('width', (d) => {
return d * 10
})
.attr('height', barWidth - 10)
.attr("fill","steelblue")
// 绘制为文本
let textGroup = svg.append('g').selectAll('text')
.data(data).enter()
.append('text')
.attr('x', (d) => {
return (d * 10) + 25
})
.attr('dx', (d) => {
return -10
})
.attr('y', (d,i) => {
return i * (500 / 10) + 50
})
.text((d) => {
return d
})
.attr('font-size', '0px')
.transition().delay(1000)
.attr('font-size', '30px')
.attr("fill","#fff")
.attr("stroke","steelblue")
.attr('text-anchor', 'end')
</script>
</body>
</html>