Public
Edited
Apr 18, 2023
Insert cell
Insert cell

<svg width="1200" height="1200">
<style>
.guzzlerrects {stroke: white; stroke-width: 2px;}
.header1{font-family:Tahoma, sans serif; font-size:34px; fill:black}
.header2{font-family:Tahoma, sans serif; font-size:18px; fill:black}
.title{font-family: Tahoma, sans serif; font-size:26px; fill:white}
.smalltitle1{font-family: Tahoma, sans serif; font-size:20px; fill:white}
.smalltitle2{font-family: Tahoma, sans serif; font-size:16px; fill:white}
.smalltitle3{font-family: Tahoma, sans serif; font-size:16px; fill:white}
.smalltitle4{font-family: Tahoma, sans serif; font-size:16px; fill:white}
</style>
<text x="0" y="100" class="header1">Land guzzlers</text>
<text x="0" y="130" class="header2">The ecological footprints of our pet can make SUVs look positively eco-friendly</text>
</svg>
Insert cell
Insert cell
Insert cell
landguzzlers = d3.select(svgContainer) // selects the cell called svgContainer and make it a d3 object
Insert cell
Insert cell
Insert cell
data = FileAttachment("land_guzzlers.csv").csv()
Insert cell
Insert cell
basepointX = 0
Insert cell
basepointY = 1200
Insert cell
colors = ["#a5620b", "#c29657", "#cc1f5e", "#a71949", "#f7991d", "#231f20"]
Insert cell
// this is the part that does the drawing
{
landguzzlers.selectAll(".guzzlerrects") //WHERE IS GUZZLERECTS COMING FROM?
.data(data) //HOW DO I SPECIFY ONLY ONE RECTANGLE?
.join("rect")
.attr("x", basepointX)
.attr("y", d => basepointY - Math.sqrt(d["footprint"]) * 1000) //WHY IS THIS SO HIGH UP? CORRECT CALCULATION?
.attr("width", d => Math.sqrt(d["footprint"]) * 1000)
.attr("height", d => Math.sqrt(d["footprint"]) * 1000)
.style("fill", (d,i) => colors[i])
.attr("class", "guzzlerrects"); // <- don't forget the ; at the end of the whole statement
landguzzlers.selectAll(".title")
.data(data)
.join("text")
.attr("x", d => Math.sqrt(d["footprint"]) * 1000)
.attr("text-anchor", d => {if(d["title"]!="HAMSTER") {return "end"}})
.attr("y", d=> basepointY - Math.sqrt(d["footprint"]) * 1000 + 28)
.text(d=> d["title"]) //don't need "text" argument
.attr("class", "title");

landguzzlers.selectAll(".smalltitle1")
.data(data)
.join("text")
.attr("x", d => Math.sqrt(d["footprint"]) * 1000)
.attr("y", d=> basepointY - Math.sqrt(d["footprint"]) * 1000 + 50)
.text(d=> "Eco-footprint: " + d["footprint"] + " hectares") //don't need "text" argument
.attr("transform", d => {if(d["title"]!="HAMSTER") {return("rotate(90 " + (Math.sqrt(d["footprint"]) * 1000 - 4) + "," + (basepointY - Math.sqrt(d["footprint"]) * 1000 + 33) + ")")}})
.attr("class", "smalltitle1");

landguzzlers.selectAll(".smalltitle2")
.data(data)
.join("text")
.attr("x", d => Math.sqrt(d["footprint"]) * 1000 - 30)
.attr("text-anchor", d => {if(d["title"]!="HAMSTER") {return "end"}})
.attr("y", d=> basepointY - Math.sqrt(d["footprint"]) * 1000 + 53)
.text(d=> d["subtitle"]) //don't need "text" argument
.attr("class", "smalltitle2");

landguzzlers.selectAll(".smalltitle3")
.data(data)
.join("text")
.attr("x", d => Math.sqrt(d["footprint"]) * 1000 - 30)
.attr("text-anchor", d => {if(d["title"]!="HAMSTER") {return "end"}})
.attr("y", d=> basepointY - Math.sqrt(d["footprint"]) * 1000 + 73)
.text(d=> d["subtitle2"]) //don't need "text" argument
.attr("class", "smalltitle3");

landguzzlers.selectAll(".smalltitle4")
.data(data)
.join("text")
.attr("x", d => Math.sqrt(d["footprint"]) * 1000 - 30)
.attr("text-anchor", d => {if(d["title"]!="HAMSTER") {return "end"}})
.attr("y", d=> basepointY - Math.sqrt(d["footprint"]) * 1000 + 93)
.text(d=> d["subtitle3"]) //don't need "text" argument
.attr("class", "smalltitle4");
}
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