Public
Edited
Apr 26, 2023
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
simple = require("simple-statistics@7.0.7/dist/simple-statistics.min.js")
Insert cell
format = d => `${d}%`
Insert cell
topojson = require("topojson-client@3")
Insert cell
US = FileAttachment("Alc_Con.json").json()
Insert cell
states = topojson.feature(US, US.objects.Alc_Con)
Insert cell
csv_data = d3.csvParse(await FileAttachment("Alcohol_Consumption_US_2016.csv").text(),({State, Sixteen_Total_alc_Per_capita}) => [State, [Sixteen_Total_alc_Per_capita]])
Insert cell
data = Object.assign(new Map(csv_data), {title: "Alcohol Consumption by State"})
Insert cell
alc_con = Array.from(csv_data.values(), d => d[1][0])
Insert cell
YlGnBu = [d3.color("#ffffd4"), d3.color("#fee391"), d3.color("#fec44f"), d3.color("#fe9929"),d3.color("#d95f0e"),d3.color("#993404")]
Insert cell
naturalbreaks = simple.ckmeans(alc_con, YlGnBu.length).map(v => v.pop())
Insert cell
//more information on sequential scales: https://observablehq.com/@d3/sequential-scales
// color = d3.scaleSequentialQuantile([...data.values()], d3.interpolateBlues)

// color = d3.scaleQuantile()
// .domain(med_age)
// .range()

color = d3.scaleThreshold()
.domain(naturalbreaks)
.range(YlGnBu)
Insert cell
width = 975
Insert cell
height = 610
Insert cell
margin = 100
Insert cell
//StateName = "State"
Insert cell
//Rotate the map sets the longitude of origin for our UTM Zone 15N projection.
//projection = d3.geoTransverseMercator().rotate([94,0]).fitExtent([[80, 80], [width, height]], states);
projection = d3.geoAlbersUsa();
//d3 reference for projections: https://github.com/d3/d3-geo/blob/master/README.md

//use the following url for specific projection settings: https://github.com/veltman/d3-stateplane
//Use this code to set up the map projection (if different than geographic projection)

//projection = d3.geoAlbers().fitExtent([[margin, margin], [width - margin, height - margin]], counties)

//projection = d3.geoMercator().fitExtent([[margin, margin], [width - margin, height - margin]], counties)
Insert cell
//Using a path generator to project geometry onto the map
path = d3.geoPath().projection(projection);
Insert cell
viewof gain = Inputs.range([0, 11], {label: "Gain", step: 0.1, placeholder: "0–11"})

viewof yearSelect = Inputs.range(d3.extent(csv_data, d => d.year),
{label: "Year:", step: 1, value: d3.max(us_broadband, d => d.year)})
Insert cell
choropleth = {
const svg = d3.create("svg")
.attr("viewBox", [-10, -50, width, height]);

svg.append("g")
.attr("transform", "translate(500,0)")
.append(() =>
legend({
color: color,
title: data.title,
width: 260,
tickFormat: ".1f"
})
);

svg.append("g")
.selectAll("path")
.data(states.features)
.join("path")
.attr("stroke", "black")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 1)
// .attr("fill", function(d){
// console.log(color(data.get(d.properties.FIPS)[0]))
// return color(data.get(d.properties.FIPS)[0]);
// })
.attr("fill", d => color(data.get(d.properties.NAME)))
.attr("d", path)
.append("title")
.text(d => "Gallon per Capita: " + data.get(d.properties.NAME));
//.text(d => `${d.properties[StateName]} : ${format(d.properties.Name)}`);

return svg.node();
}
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