This repository has been archived by the owner on Dec 22, 2024. It is now read-only.
generated from code4policy/dataviz-with-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
52 lines (47 loc) · 1.59 KB
/
script.js
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
// Load the CSV data
d3.csv("311_boston_data.csv", d => {
return {
reason: d.reason,
count: +d.Count // convert string to number
};
}).then(data => {
// Sort and slice the top 10 reasons
let topTenData = data.sort((a, b) => d3.descending(a.count, b.count)).slice(0, 10);
// Set up the dimensions and margins of the graph
const margin = { top: 30, right: 30, bottom: 70, left: 60 },
width = 460 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// Append the svg object to the body of the page
const svg = d3.select("#chart")
.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})`);
// X axis
const x = d3.scaleBand()
.range([0, width])
.domain(topTenData.map(d => d.reason))
.padding(0.2);
svg.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x))
.selectAll("text")
.attr("transform", "translate(-10,0)rotate(-45)")
.style("text-anchor", "end");
// Add Y axis
const y = d3.scaleLinear()
.domain([0, d3.max(topTenData, d => d.count)])
.range([height, 0]);
svg.append("g")
.call(d3.axisLeft(y));
// Bars
svg.selectAll("mybar")
.data(topTenData)
.join("rect")
.attr("class", "bar")
.attr("x", d => x(d.reason))
.attr("y", d => y(d.count))
.attr("width", x.bandwidth())
.attr("height", d => height - y(d.count));
});