Published
Edited
Apr 18, 2021
2 forks
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
emissions_chart = {
//setting up the svg
const margin = { top: 10, bottom: 50, right: 10, left: 80 };
const visHeight = 500 - margin.top - margin.bottom;
const visWidth = width - margin.left - margin.right; //width is basically max-width
const svg = d3.create('svg')
.attr('width', visWidth + margin.left + margin.right)
.attr('height', visHeight + margin.top + margin.bottom);
const g = svg.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`);

//create Scales
const x = d3.scaleLinear()
.domain([1990.00,2019.00])
.range([0, visWidth]);
const y = d3.scaleLinear()
.domain([0, maxEmissions]).nice()
.range([visHeight, 0]);
const xAxis = d3.axisBottom(x);
const yAxis = d3.axisLeft(y);
g.append('g')
.attr('transform', `translate(0,${visHeight})`)
.call(xAxis);

g.append('g')
.call(yAxis)
.call(g => g.selectAll('.domain').remove())
.append('text')
.attr('text-anchor', 'start')
.attr('dominant-baseline', 'middle')
.attr('fill', 'black')
.attr('x', 5)
.text('Emissions - tonnes');

const line = d3.line()
.x(d => x(d.Year))
.y(d => y(d["Annual CO2 emissions"]));

const series = g.append('g')
.selectAll('g')
.data(dataByCountry)
.join('g')
.attr('stroke', d => color(d.country))
.append('path')
.datum(d => d.counts)
.attr('fill', 'none')
.attr('stroke-width', 2)
.attr('d', line);

//footer text
g.append("text")
.attr("x", visWidth / 4 * 3 - 70)
.attr("y", (margin.bottom + 425))
.attr("text-anchor", "right")
.attr("dominant-baseline", "hanging")
.attr("font-family",'Source Serif Pro')
.attr("font-size", "12px")
.text("Source: Our World in Data - CO2 Emissions");

return svg.node()
}
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