-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.html
48 lines (42 loc) · 1.4 KB
/
06.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>06 - D3 SVG Bar</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.0.0/d3.js"></script>
<script>
const data = [4, 8, 15, 16, 23, 42];
const container = d3.create('svg')
.attr('width', 420)
.attr('height', 120)
.attr('font-family', 'sans-serif')
.attr('font-size', 10)
.attr('text-anchor', 'end');
document.body.appendChild(container.node());
const x = d3.scaleLinear()
.domain([0, d3.max(data)])
.range([0, container.attr('width')]);
const y = d3.scaleBand()
.domain(d3.range(data.length))
.range([0, container.attr('height')]);
const bar = container
.selectAll('g')
.data(data)
.join('g')
.attr('transform', (d, i) => `translate(0, ${y(i)})`);
bar.append('rect')
.attr('fill', 'steelblue')
.attr('width', d => x(d))
.attr('height', y.bandwidth() - 1);
bar.append('text')
.attr('fill', 'white')
.attr('x', d => x(d) - 3)
.attr('y', (y.bandwidth() - 1) / 2)
.attr('dy', '.35em')
.text(d => d);
</script>
</body>
</html>