I think that’s exactly what my notebook is already doing; just change the radio button to “Median Age”.
Here are the relevant lines of code in the definition of map
:
let p = parameter.toLowerCase().replace(" ", "_");
let max = d3.max(country_data.map((o) => +o[p]));
let bubble_group = svg.append("g");
bubble_group
.selectAll("circle")
.data(country_data.filter((o) => centroid_map.has(o.CountryName)))
.join("circle")
.attr("cx", (d) => centroid_map.get(d.CountryName)[0])
.attr("cy", (d) => centroid_map.get(d.CountryName)[1])
.attr("r", (d) => 20 * Math.sqrt(+d[p] / max))
.attr("fill", "lightblue")
.attr("stroke", "black");
Now, if you examine the code defining the radio button labeled “Parameter to show”, you’ll see the following:
viewof parameter = Inputs.radio(["Population", "Airports", "Median Age"], {
value: "Population",
label: "Parameter to show:"
})
What this does is bind the variable parameter
to the value of the radio button list. Thus, anytime that you refer to parameter
in code, you get one of the following:
"Population"
,"Airports"
, or"Median Age"
.
Furthermore, this value is changed dynamically and propagated throughout the notebook when you change the radio button. Thus, when you see
let p = parameter.toLowerCase().replace(" ", "_");
in the map code, the parameter
refers to the radio button and changing the radio button immediately updates the map. The .toLowerCase().replace(" ", "_")
business simply translates the displayed radio value to the object key in your data.
Finally, we use p
to define the radius of a circle displayed on the map in this line:
.attr("r", (d) => 20 * Math.sqrt(+d[p] / max))
In this line, the variable d
arises from your data and, since we’ve already defined p
to be the object key that we want, d[p]
extracts the value that you want.