Public
Edited
Nov 1, 2023
Insert cell
Insert cell
chart = {
// Specify the chart’s dimensions.
const width = 928;
const height = 600;
const marginTop = 10;
const marginRight = 10;
const marginBottom = 200;
const marginLeft = 150;

// Prepare the scales for positional and color encodings.
// Fx encodes the state.
const fx = d3.scaleBand()
.domain(new Set(data.map(d => d.state)))
.rangeRound([marginLeft, width - marginRight])
.paddingInner(0.1);

// Both x and color encode the age class.
const ages = new Set(data.map(d => d.age));

const x = d3.scaleBand()
.domain(ages)
.rangeRound([0, fx.bandwidth()])
.padding(0.05);
///////////////////////////////////////////////////////////
//const color = d3.scaleOrdinal()
//.domain(ages)
//.range(d3.schemeSpectral[ages.size])
//.unknown("#ccc");
const color = d3.scaleOrdinal()
.domain(ages)
//.range(d3.schemeSpectral[ages.size])
//.range(d3.quantize(d3.interpolateSpectral, 200))
.range(d3.schemeTableau10)
//.range(["#aac"]);
.unknown("#ccc"); ///////////////////////////////////////////////
// Y encodes the height of the bar.
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.population)]).nice()
.rangeRound([height - marginBottom, marginTop]);

// A function to format the value in the tooltip.
const formatValue = x => isNaN(x) ? "N/A" : x.toLocaleString("en")

// Create the SVG container.
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto;");


// Append a group for each state, and a rect for each age.
svg.append("g")
.selectAll()
.data(d3.group(data, d => d.state))
.join("g")
.attr("transform", ([state]) => `translate(${fx(state)},0)`)
.selectAll()
.data(([, d]) => d)
.join("rect")
.attr("x", d => x(d.age))
.attr("y", d => y(d.population))
.attr("width", x.bandwidth())
.attr("height", d => y(0) - y(d.population))
.attr("fill", d => color(d.age));

// Append the horizontal axis.
const xAxis = svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(fx).tickSizeOuter(0))
.call(g => g.selectAll(".domain").remove());

xAxis.selectAll("text")
.style("text-anchor", "end")
.attr("transform", "rotate(-45)")
.attr("dx", "-.8em")
.attr("dy", ".15em");

// Append the vertical axis.
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y).ticks(null, "s"))
.call(g => g.selectAll(".domain").remove());

// Return the chart with the color scale as a property (for the legend).
return Object.assign(svg.node(), {scales: {color}});
//return Object.assign("hola", {scales: {color}});
}
Insert cell
data = {
let data = await FileAttachment("datoshsrp2.txt").csv({typed: true});
const routers = data.columns.slice(1);
data = d3.sort(data, d => -d3.sum(routers, router => d[router])).slice(0, 80);
return routers.flatMap((age) => data.map((d) => ({state: d.Nexus, age, population: d[age]})));
}
Insert cell
//import {legend} from "@d3/color-legend"
import {legend} from "e3e562d107e4b1fa"
Insert cell
Select a data source…
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
Plot.plot({
x: {axis: null},
y: {tickFormat: "s"},
fx: {label: null},
color: {scheme: "spectral"},
marks: [
Plot.barY(data, {fx: "state", x:"age", y: "population", fill: "age", sort: {color: null, x: null, fx: {value: "-y", reduce: "sum"}}}),
Plot.ruleY([0])
]
})
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