Published
Edited
May 11, 2022
Insert cell
Insert cell
dataName = race + gender + value
Insert cell
function getData(name){
if(name == "WhiteMaleMean") {return WhiteMaleMean}
if(name == "WhiteMaleMedian") {return WhiteMaleMedian}
if(name == "WhiteFemaleMean") {return WhiteFemaleMean}
if(name == "WhiteFemaleMedian") {return WhiteFemaleMedian}
if(name == "AsianMaleMean") {return AsianMaleMean}
if(name == "AsianMaleMedian") {return AsianMaleMedian}
if(name == "AsianFemaleMean") {return AsianFemaleMean}
if(name == "AsianFemaleMedian") {return AsianFemaleMedian}
if(name == "BlackMaleMean") {return BlackMaleMean}
if(name == "BlackMaleMedian") {return BlackMaleMedian}
if(name == "BlackFemaleMean") {return BlackFemaleMean}
if(name == "BlackFemaleMedian") {return BlackFemaleMedian}
if(name == "HispanicMaleMean") {return HispanticMaleMean}
if(name == "HispanicMaleMedian") {return HispanticMaleMedian}
if(name == "HispanicFemaleMean") {return HispanicFemaleMean}
if(name == "HispanicFemaleMedian") {return HispanicFemaleMedian}
}
Insert cell
function getIndex(){
if(attribute == "Age") {return 0}
if(attribute == "Region") {return 1}
if(attribute == "Household Status") {return 2}
if(attribute == "Education") {return 3}
}
Insert cell
Insert cell
[selected, buttonIndex()]
Insert cell
MeanOrMedian()
Insert cell
function buttonIndex(){
if(selected == "Inside metropolitan statistical areas"){return 1}
if(selected == "Inside principal cities"){return 2}
if(selected == "Outside principal cities"){return 3}
if(selected == "Outside metropolitan statistical areas"){return 4}
if(selected == "Northeast"){return 5}
if(selected == "Midwest"){return 6}
if(selected == "South"){return 7}
if(selected == "West"){return 8}
if(selected == "15-24"){return 9}
if(selected == "25-29"){return 10}
if(selected == "30-34"){return 11}
if(selected == "35-39"){return 12}
if(selected == "40-44"){return 13}
if(selected == "45-49"){return 14}
if(selected == "50-54"){return 15}
if(selected == "55-59"){return 16}
if(selected == "60-64"){return 17}
if(selected == "65-69"){return 18}
if(selected == "70-74"){return 19}
if(selected == "75+"){return 20}

if(selected == "Householder"){return 21}
if(selected == "Spouse of householder"){return 22}
if(selected == "Child of householder"){return 23}
if(selected == "Other relative of householder"){return 24}

if(selected == "Less than 9th grade"){return 25}
if(selected == "9th to 12th grade no diploma"){return 26}
if(selected == "High school graduate (includes equivalency)"){return 27}
if(selected == "Some college no degree"){return 28}
if(selected == "Associate degree"){return 29}
if(selected == "Bachelor's degree or more"){return 30}
if(selected == "Bachelor's degree"){return 31}
if(selected == "Master's degree"){return 32}
if(selected == "Professional degree"){return 33}
if(selected == "Doctorate degree"){return 34}

}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
<div id="container">
<svg id="dataContainer" width="1000 " height="500">
<text id="attributesText" x="500" y="200" font-size="14"></text>
<text id="mytext" x="500" y="220"></text>
<text id="outlineHeader" x="250"></text>
<style>
#mytext {font-family: Arial,Helvetica,sans-serif;}
#attributesText {font-family: Arial,Helvetica,sans-serif;}
#outlineHeader {font-family: Arial,Helvetica,sans-serif;}
</style>
</svg>
</div>
Insert cell
MeanOrMedian(value)
Insert cell
meanMedianIncome = [53996, 35805]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
rFromArea = (area) => Math.sqrt((area)/Math.PI);
Insert cell
function MeanOrMedian(value) {
if(value === "Mean"){
return 131.10095581032184;
}
else {
return 106.75713313315708;}
}
Insert cell
viz.select("#outlineHeader").text("The " + value + " U.S. income: $" + Math.round(Math.PI*MeanOrMedian(value)*MeanOrMedian(value)))
.attr("font-size", 14)
.attr("y", MeanOrMedian(value)+270)
Insert cell
viz = d3.select(container).select("#dataContainer")
Insert cell
circles = viz.selectAll(".circles")
.data(getData(dataName))
.join("circle")
.attr("class","circles")
.attr("cx", d => 250)
.attr("cy", d => 250)
.attr("r", d => rFromArea(getData(dataName)[buttonIndex()]))
.attr("fill", "#889b73")
.style("opacity", 0.05);
Insert cell
mean_median_circle = viz.selectAll("circle")
.data(meanMedianIncome)
.join("circle")
.attr("cx", 250)
.attr("cy", 250)
.attr("r", MeanOrMedian(value))
.attr("fill", "#FFFFFF")
.attr("stroke", "#000000")
.attr("stroke-width", 3)
.style("opacity", 1);
Insert cell
viz.select("#mytext").text("$" + String(getData(dataName)[buttonIndex()]));
Insert cell
viz.select("#attributesText").text("The " + value + " annual income for the group you have selected is:" )
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more