Public
Edited
Apr 17, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart2 = {
const duration = 250;
const margin = {top: 40, right: 50, bottom: 30, left: 40};
const width = 800 - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;

const svg = d3.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);

const xAxis = svg.append("g")
.attr("transform", `translate(${margin.left},${height + margin.top})`);
const yAxis = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);

const title = svg.append("text")
.attr("x", width / 2 + margin.left)
.attr("y", margin.top / 2)
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "16px")
.attr("font-weight", "bold")
.text("Number Of en.wikipedia.org Editors With 5-99 Edits By Country, Per Capita");

const yearMonth = svg.append("text")
.attr("x", width / 2 + margin.left)
.attr("y", margin.top)
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "16px")
.text("");

do {
for (let year of Object.keys(output)) {
for (let [monthIdx, month] of Object.entries(output[year])) {
yearMonth.text(`${year} - ${monthIdx}`);
month = Object.entries(month).sort((a, b) => b[1] - a[1]).slice(0, 10);

month = month.map(country => [country[0], country[1] / loc[country[0]]]).sort((a, b) => b[1] - a[1])

const x = d3.scaleLinear()
.domain([0, d3.max(month, d => d[1])]).nice()
.range([0, width]);
const y = d3.scaleBand()
.domain(month.map(d => d[0]))
.range([0, height])
.padding(0.1);

const xAxisTransition = xAxis.transition()
.duration(duration)
.call(d3.axisBottom(x))
.end();
const yAxisTransition = yAxis.transition()
.duration(duration)
.call(d3.axisLeft(y))
.end();

const barsTransition = svg.selectAll(".bar")
.data(month)
.join("rect")
.attr("class", "bar")
.attr("y", d => y(d[0]) + margin.top)
.attr("height", y.bandwidth())
.transition()
.duration(duration)
.attr("x", d => margin.left)
.attr("width", d => x(d[1]))
.end();

await Promise.all([xAxisTransition, yAxisTransition, barsTransition]);
yield svg.node();
}
}
} while (loop02[0] === "Loop")
}

Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more