Public
Edited
Aug 11, 2023
Insert cell
Insert cell
Insert cell
chart = {
// Specify the chart’s dimensions.
const width = 928;
const height = 500;
const marginTop = 10;
const marginRight = 10;
const marginBottom = 20;
const marginLeft = 40;
const outerRadius = (height / 1.5) - marginRight;
const innerRadius = 30;

// Determine the series that need to be stacked.
const series = d3.stack()
.keys(d3.union(data.map(d => d.period))) // distinct series keys, in input order
.value(([, D], key) => D.get(key).value) // get value for each series key and stack
(d3.index(data, d => d.date, d => d.period)); // group by stack then series key

// Prepare the scales for positional and color encodings.
const x = d3.scaleUtc()
.domain(d3.extent(data, d => d.date))
.range([0, 2 * Math.PI]);
console.log(x.domain())
const y = d3.scaleLinear()
.domain([0, d3.max(series, d => d3.max(d, d => d[1]))])
.rangeRound([innerRadius, outerRadius]);

const color = d3.scaleOrdinal()
.domain(series.map(d => d.key))
.range(d3.schemeTableau10);

// Construct an area shape.
const area = d3.areaRadial()
.curve(d3.curveCardinalClosed)
.angle(d => x(d.data[0]))
.innerRadius(d => y(d[0]))
.outerRadius(d => y(d[1]));

// 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;");

// Add the y-axis, remove the domain line, add grid lines and a label.
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y).ticks(height / 80))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").clone()
.attr("x2", width - marginLeft - marginRight)
.attr("stroke-opacity", 0.1))
.call(g => g.append("text")
.attr("x", -marginLeft)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text("↑ Unemployed persons"));

// Append a path for each series.
svg.append("g")
.attr("transform", `translate(${width / 2},${height / 2})`)
.selectAll()
.data(series)
.join("path")
.attr("fill", d => color(d.key))
.attr("d", area)
.append("title")
.text(d => d.key);

// Append the horizontal axis atop the area.
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0));

// Return the chart with the color scale as a property (for the legend).
return Object.assign(svg.node(), {scales: {color}});
}
Insert cell
data = FileAttachment("stocks_viz_test@6.csv").csv({typed: true})
Insert cell
import {Swatches} from "@d3/color-legend"
Insert cell
Insert cell
Plot.plot({
marginLeft: 60,
y: {grid: true},
color: {legend: true, columns: 6},
marks: [
Plot.areaY(data, {x: "date", y: "value", fill: "period"}),
Plot.ruleY([0])
]
})
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