Published
Edited
Feb 26, 2021
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
x = d3
.scaleTime()
.domain(d3.extent(data, d => d.YEAR))
.range([margin.left, width - margin.right])
Insert cell
xAxis = g =>
g.attr("transform", `translate(0,${height - margin.bottom})`).call(
d3
.axisBottom(x)
.ticks(tick_num)
.tickFormat(d3.format("d"))
)
Insert cell
y_conc = d3
.scaleLinear()
.domain([0, 1400])
.nice()
.range([height - margin.bottom, margin.top])
Insert cell
y_emission = d3
.scaleLinear()
.domain([0, 140000])
.nice()
.range([height - margin.bottom, margin.top])
Insert cell
yAxis_conc = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y_conc).ticks(tick_num))
.call(g =>
g
.select(".tick:last-of-type text")
.clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y)
)
Insert cell
yAxis_emission = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisRight(y_emission).ticks(tick_num))
.call(g =>
g
.select(".tick:last-of-type text")
.clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y)
)
Insert cell
line_conc = d3
.line()
.defined(d => !isNaN(d.CO2_conc))
.x(d => x(d.YEAR))
.y(d => y_conc(d.CO2_conc))
Insert cell
line_emission = d3
.line()
.defined(d => !isNaN(d.CO2_emission))
.x(d => x(d.YEAR))
.y(d => y_emission(d.CO2_emission))
Insert cell
function tweenDash() {
const l = this.getTotalLength(),
i = d3.interpolateString("0," + l, l + "," + l);
return function(t) {
return i(t);
};
}
Insert cell
function transition(path) {
path
.transition()
.duration(7000)
.ease(d3.easeQuadOut)
.attrTween("stroke-dasharray", tweenDash)
.on("end", () => {
d3.select(this).call(transition);
});
}
Insert cell
data = d3.csv(
"https://raw.githubusercontent.com/isaack8/final-project-cse412/main/data/data_CO2_full.csv",
d3.autoType
)
Insert cell
d3 = require('d3@5')
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