Public
Edited
May 9
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
examplePieGenerator = d3.pie()
.value(d => d.cost)
Insert cell
exampleAngles = examplePieGenerator(exampleData)
Insert cell
Insert cell
exampleArcGenerator = d3.arc()
.innerRadius(0)
.outerRadius(100)
Insert cell
exampleArcGenerator(exampleAngles[0])
Insert cell
Insert cell
exampleColor = d3.scaleOrdinal()
.domain(exampleData.map(d => d.utility))
.range(d3.schemeCategory10);
Insert cell
swatches({ color: exampleColor })
Insert cell
{
const svg = d3.create('svg')
.attr('width', 200)
.attr('height', 200);

const g = svg.append('g')
.attr('transform', 'translate(100,100)');
g.selectAll('path')
.data(exampleAngles)
.join('path')
.attr('d', exampleArcGenerator)
.attr('fill', d => exampleColor(d.data.utility));
return svg.node();
}
Insert cell
Insert cell
Insert cell
rawData = FileAttachment("age_distribution_by_state_2019.csv").csv({ typed: true })
Insert cell
Insert cell
maxPopulation = d3.max(rawData, d => d.total)
Insert cell
Insert cell
dataset = Object.fromEntries(rawData
.map(d => {
const adults = d['19-25'] + d['26-34'] + d['35-54'] + d['55-64'];
const key = d.state;
const value = [
{ age: '0-18', count: d['0-18'], stateTotalPopulation: d.total },
{ age: '19-64', count: adults, stateTotalPopulation: d.total },
{ age: '65+', count: d['65+'], stateTotalPopulation: d.total },
];
return [key, value];
}))
Insert cell
También se cuenta con datos GeoJSON para los estados. Como la proyección no incluye a Puerto Rico, se filtrará del conjunto de datos.
Insert cell
usaGeo = FileAttachment('gz_2010_us_040_00_20m.json').json()
Insert cell
states = usaGeo.features.filter(d => d.properties.NAME !== 'Puerto Rico')
Insert cell
Insert cell
visWidth = width
Insert cell
visHeight = width * 0.625
Insert cell
Insert cell
projection = d3.geoAlbersUsa()
.fitSize([visWidth, visHeight], usaGeo)
Insert cell
Insert cell
path = d3.geoPath().projection(projection)
Insert cell
Insert cell
pie = d3.pie()
.value(d => d.count)
.sort((a, b) => d3.ascending(a.age, b.age))
Insert cell
Insert cell
radius = d3.scaleSqrt()
.domain([0, maxPopulation])
.range([0, 30])
Insert cell
Insert cell
arc = d3.arc()
.innerRadius(0)
.outerRadius(d => radius(d.data.stateTotalPopulation))
Insert cell
Insert cell
color = d3.scaleOrdinal()
.domain(['0-18', '19-64', '65+'])
.range(d3.schemeCategory10)
Insert cell
Insert cell
swatches({ color })
Insert cell
{
const svg = d3.create("svg")
.attr("width", 1152)
.attr("height", 720);

svg.append("g")
.selectAll("path")
.data(states)
.join("path")
.attr("d", path)
.attr("fill", "#ccc")
.attr("stroke", "#fff");

svg.append("g")
.selectAll("g")
.data(states)
.join("g")
.attr("transform", d => {
const [x, y] = path.centroid(d);
return `translate(${x},${y})`;
})

.each(function(d) {
const stateName = d.properties.NAME;
const data = dataset[stateName];
if (data) {
const g = d3.select(this);
g.selectAll("path")
.data(pie(data))
.join("path")
.attr("d", arc)
.attr("fill", d => color(d.data.age));
}
});

return svg.node();
}

Insert cell
{
const svg = d3.create('svg')
.attr('width', visWidth)
.attr('height', visHeight);

// T 1

// T 2


// task 3



return svg.node();
}
Insert cell
Insert cell
d3 = require('d3@7')
Insert cell
import { swatches } from "@d3/color-legend"
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