-
Notifications
You must be signed in to change notification settings - Fork 13
/
demo05-coxcomb.html
77 lines (68 loc) · 2.1 KB
/
demo05-coxcomb.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
width: 100vw;
height: 100vh;
display: flex;
}
.pie-chart {
margin: auto;
}
</style>
</head>
<body>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src='https://d3js.org/d3-scale-chromatic.v1.min.js'></script>
<script>
const data = [
22, 45, 12, 54, 29, 77
]
const width = 600
const height = 600
const colors = d3.schemeSet3
const radiusScale = d3.scaleLinear()
.range([0, 50])
.domain([d3.min(data), d3.max(data)])
// 通过arc函数生成原型或圆环
const arc = d3.arc()
.innerRadius(0)
.outerRadius(200)
.cornerRadius(5)
.padAngle(Math.PI / 140)
const svg = d3.select('body')
.append('svg')
.attr('class', 'pie-chart')
.attr('width', width)
.attr('height', height)
// 通过pie函数生成饼图或环形图所需要数据
const pieData = d3.pie()(data)
const arcs = svg.selectAll('g')
.data(pieData)
.enter()
.append('g')
.attr('transform', `translate(${width / 2}, ${height / 2})`)
arcs.append('path')
.attr('fill', (_, i) => colors[i])
.attr('d', function (d) {
// 动态设定外半径
arc.outerRadius(100 + radiusScale(d.value))
// 注册path data
return arc(d)
})
arcs.append('text')
.attr('transform', d => {
// 找到图形中间点
return `translate(${ arc.centroid(d) })`
})
.attr('fill', 'white')
.attr('text-anchor', 'middle')
.text(d => d.value)
</script>
</body>
</html>