-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
93 lines (75 loc) · 2.43 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
<!DOCTYPE html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
</head>
<body>
<nav class="navbar bg-light">
<div class="container-fluid">
<span class="navbar-brand mb-0 h1">d3-barchart</span>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col">
<svg width="1000" height="600"></svg>
</div>
</div>
</div>
</body>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
var margin = { top: 20, right: 20, bottom: 30, left: 40 },
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom
const svg = d3
.select('svg')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
var y = d3.scaleBand()
.range([height, 0])
.padding(0.4);
var x = d3.scaleLinear()
.range([0, width - 50]);
var colorScale = d3.scaleLinear()
.domain([50, 67])
.range(['#D4F0F0', '#CCE2CB', '#B6CFB6', '#97C1A9', '8FCACA']);
function findBarCorners(x, y, width, height, radius) {
return "M" + x + "," + y
+ "h" + (width - radius)
+ "a" + radius + "," + radius + " 0 0 1 " + radius + "," + radius
+ "v" + (height - 2 * radius)
+ "a" + radius + "," + radius + " 0 0 1 " + -radius + "," + radius
+ "h" + (radius - width)
+ "z";
}
d3.csv('cok_tutan_lastn.csv').then(data => {
// sort data ascending
data.sort((a, b) => a.count - b.count);
x.domain([0, d3.max(data, function (d) { return d.count; })])
y.domain(data.map(function (d) { return d.lastn; }));
var bar_height = Math.min(Math.ceil((height / data.length) - data.length * 1), 45);
var bars = svg.selectAll(".bar")
.data(data)
.enter()
bars.append("path")
.attr("class", "bar")
//.attr("x", function(d) { return x(d.value); })
.attr("width", function (d) { return x(d.count); })
.attr("y", function (d) { return y(d.lastn); })
.attr("height", y.bandwidth())
.attr("fill", (d) => {
return colorScale(d.count)
})
.attr("d", function (d) {
return findBarCorners(0, Math.floor(y(d.lastn)), x(d.count), y.bandwidth(), 10);
});
svg.append("g")
.call(d3.axisLeft(y).tickSize(5));
svg.append("g")
.call(d3.axisTop(x))
})
</script>