Public
Edited
Apr 8, 2022
Fork of Line graphs
Insert cell
Insert cell
<svg id="base" width="1000" height="800">
<style>
.area {fill:steelblue; stroke: #000000}
</style>
<g id="layer1" transform="translate(50 10)"></g>
<g id="layer2" transform="translate(50 400)"></g>
</svg>
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sortedData = world_gdp_growth_pivot.sort((a,b)=>d3.ascending(toNum(a["Year"]),toNum(b["Year"])));
// sort the data so it's ordered left to right. The data may already be, but let's make sure.
Insert cell
Insert cell
scaleX = d3.scaleLinear().domain([1960,2012]).range([10,920])
Insert cell
scaleY = d3.scaleLinear().domain([-20,20]).range([300,0])
Insert cell
Insert cell
Insert cell
graph1Elem = d3.select(svgContainer).select("#layer1") // this goes in <g id="layer1"></g>
Insert cell
myline1 = areaGraph(graph1Elem,sortedData,"Year","USA",0,scaleX,scaleY)
Insert cell
Insert cell
graph1Elem.append("g").attr("transform","translate(10 300)").call(xAxis).style("color","#ccc");
Insert cell
graph1Elem.append("g").call(yAxis).style("color","#ccc");
Insert cell
Insert cell
Insert cell
graph2Elem = d3.select(svgContainer).select("#layer2") // this goes in <g id="layer2"></g>
Insert cell
myLine2 = areaGraph(graph2Elem,sortedData,"Year","USA","CHN",scaleX,scaleY);
Insert cell
Insert cell
graph2Elem.append("g").attr("transform","translate(10 300)").call(xAxis).style("color","#ccc");
Insert cell
graph2Elem.append("g").call(yAxis).style("color","#ccc");
Insert cell
Insert cell
Insert cell
xAxis = d3.axisBottom().scale(scaleX).tickFormat(d3.format("d"))
Insert cell
yAxis = d3.axisLeft().scale(scaleY)
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