-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbar-chart.html
87 lines (77 loc) · 2.39 KB
/
bar-chart.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bar Chart</title>
</head>
<body>
<div id="canvas"></div>
<script type="text/javascript" src="scatterplot-data-1.js"></script>
<script type="text/javascript" src="applestocks-data.js"></script>
<script type="module">
import * as d3 from "https://cdn.jsdelivr.net/npm/d3@7/+esm";
let width = 900;
let height = 300;
let margin = {
top: 25,
bottom: 35,
left: 40,
right: 20,
};
const x = d3
.scaleUtc()
.domain(d3.extent(apple_stocks_data, (d) => new Date(d.Date)))
.nice()
.range([0, width]);
const bandScale = d3
.scaleBand()
.domain(
Array.from(new Set(apple_stocks_data.map((d) => new Date(d.Date)))),
)
.range([0, width])
.padding(0.1);
const y = d3
.scaleLinear()
.domain(d3.extent(apple_stocks_data, (d) => d.Close))
.nice()
.range([height, 0]);
const line = d3
.line()
.curve(d3.curveBasis)
.x((d) => x(new Date(d.Date)))
.y((d) => y(d.Close));
let svg = d3
.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
const g = svg
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
g.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x).ticks(width / 80));
g.append("g").call(d3.axisLeft(y));
g.append("g")
.attr("fill", "steelblue")
.selectAll("rect")
.data(apple_stocks_data)
.join("rect")
.attr("x", (d) => bandScale(new Date(d.Date)))
.attr("y", (d) => y(d.Close))
.attr("height", (d) => height - y(d.Close))
.attr("width", bandScale.bandwidth());
g.append("g")
.attr("stroke", "red")
.attr("stroke-width", 1.5)
.attr("fill", "none")
.selectAll("circle")
.data(apple_stocks_data)
.join("circle")
.attr("cx", (d) => bandScale(new Date(d.Date)))
.attr("cy", (d) => y(d.Close))
.attr("r", 1);
document.getElementById("canvas").append(svg.node());
</script>
</body>
</html>