Public
Edited
Feb 29, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chosenTempYear = 2019;
Insert cell
chosenTempTerm = "China Virus";
Insert cell
tempData = generateMonthlyCountsDataByYear(chosenTempYear, chosenTempTerm);
Insert cell
height = 500
Insert cell
width = 1400
Insert cell
margin = Object({top: 40, right: 250, bottom: 60, left: 60})
Insert cell
week = Array.from(slursUsed.map(d => d.Week));
Insert cell
monthYearArray = Array.from(new Set(week.map(dateString => dateString.split('-').slice(0, 2).join('-'))));
Insert cell
yearsOnly = Array.from(new Set(week.map(dateString => dateString.split('-')[0])));
Insert cell
monthOnly = Array.from(new Set(week.map(dateString => dateString.split('-')[1])));
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
uniqueMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
Insert cell
x = d3.scaleBand()
.domain(uniqueMonths)
.range([margin.left, width - margin.right])
.padding(0.1);
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
graph = {
const svg = d3.create('svg')
.attr('width', width)
.attr('height', height);

svg.append('g')
.attr('transform', `translate(0, ${height - margin.bottom})`)
.call(d3.axisBottom(x))
.append("text")
.attr("text-anchor", 'end')
.attr('fill', 'black')
.attr("font-size", '12px')
.attr("font-weight", 'bold')
.attr("x", width - margin.right)
.attr('y', '30')
.text("Months");

svg.append('g')
.attr('transform', `translate(${margin.left}, 0)`)
.call(d3.axisLeft(y))
.append("text")
.attr("transform", `translate(-30, ${margin.top}) rotate (-90)`)
.attr("text-anchor", 'end')
.attr('fill', 'black')
.attr("font-size", '12px')
.attr("font-weight", 'bold')
.text("Term Popularity (in %)");

svg.selectAll("rect")
.data(slursUsed)
.join("rect")
.attr("x", (d) => x(d[0]))
.attr("y", (d) => y(d[1]))
.attr("height", d => height - margin.bottom - y(d[1]))
.attr("width", x.bandwidth())
.append('title')
.text(d => `Term: ${termSelector}\nYear: ${parseInt(yearSelector)}`);

const bars = svg
.attr("fill", "steelblue")
.selectAll("rect")
.data(Object.entries(generateMonthlyCountsDataByYear(parseInt(yearSelector), termSelector)))
.join("rect")
.attr("x", (d) => x(d[0]))
.attr("y", (d) => y(d[1]))
.attr("height", d => height - margin.bottom - y(d[1]))
.attr("width", x.bandwidth())
update();
function update() {
const newYearData = generateMonthlyCountsDataByYear(parseInt(yearSelector), termSelector);
bars
.data(Object.entries(newYearData))
.transition()
.duration(1000)
.ease(d3.easeCubic)
.attr('x', d => x(d[0]))
.attr('y', d => y(d[1]))
.attr("height", d => height - margin.bottom - y(d[1]))
.attr("width", x.bandwidth())

}
return Object.assign(svg.node());
}
Insert cell
slursOnly = Object.keys(slursUsed[0]).filter(key => key !== "Week");
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more