Public
Edited
Jan 20
Fork of Calendar
63 forks
28 stars
Global temperature trendsBeeswarm, Log ScaleContoursDensity contoursBubble chart componentCircle packing componentExploring Data with Vega-LiteStreaming data into VegaVega-Lite Line ChartBrushable scatterplotVega-Lite ScatterplotNight Skies — Lights and Light Pollution WebGL GlobePhysics based t-SNEGraphvizRidgeline plotZoomable sunburstHierarchical edge bundlingMethods of Comparison, ComparedChord diagramA Guide to Guides: Axes & Legends in VegaPSR B1919+21HexbinStacked-to-grouped barsTree of LifeHeat indexZoomable circle packingMarimekkoVizsla and Vega-LiteDirectly labelling linesParallel coordinatesCollapsible treeTangled tree visualizationMarey’s TrainsWorld History TimelineSmall multiple chart cartogramThe Real MVP in the NBAAnimated treemapDensity Contour Matrix with BrushingStars and constellationsHertzsprung–Russell diagramThe Coronavirus landscapeGitHub BurndownCandlestick ChartConcentration values vs. TimeA few days of CO2 levels in my homeIrregular bins histogramELD ViewerDistributions and summary statistics - a collection of Plot examplesMermaidBivariate Bubble MapD3 gallery
Calendar
Stacked bar chartDot plotConnected scatterplotCandlestick chartHistogramForce-directed graphDisjoint force-directed graphIndex chartSankey diagramLine chart, percent changeStacked area chartBubble chartArea chartHorizontal bar chartRadial tidy treeCircle packingIcicleStreamgraphTidy treeCluster treeSunburstHorizon chartBox plotScatterplotDifference chartBand chartBar chart transitionsTreemapLine chart, multiple seriesScatterplot matrixBrushable scatterplot matrixPlayfair's Wheat and Wages
Insert cell
Insert cell
Insert cell
chart = {
const cellSize = 16; // height of a day
const height = cellSize * 7; // height of a week (5 days + padding)
const width = (cellSize + 1.5) * 53; // width of the chart

// Define formatting functions for the axes and tooltips.
const formatValue = d3.format("+.2%");
const formatClose = d3.format("$,.2f");
const formatDate = d3.utcFormat("%x");
const formatDay = i => "SMTWTFS"[i];
const formatMonth = d3.utcFormat("%b");

// Helpers to compute a day’s position in the week.
const timeWeek = d3.utcMonday;
const countDay = i => (i + 6) % 7;

// Compute the values used to color the cells: percent change is the difference between the day’s
// closing value and the previous day’s, as a fraction of the latter.
const data = d3.pairs(dji, ({Close: Previous}, {Date, Close}) => ({
date: Date,
value: (Close - Previous) / Previous,
close: Close
}));

// Compute the extent of the value, ignore the outliers
// and define a diverging and symmetric color scale.
const max = d3.quantile(data, 0.9975, d => Math.abs(d.value));
const color = d3.scaleSequential(d3.interpolatePiYG).domain([-max, +max]);

// Group data by year, in reverse input order. (Since the dataset is chronological,
// this will show years in reverse chronological order.)
const years = d3.groups(data, d => d.date.getUTCFullYear()).reverse();

// A function that draws a thin white line to the left of each month.
function pathMonth(t) {
const d = Math.max(0, Math.min(5, countDay(t.getUTCDay())));
const w = timeWeek.count(d3.utcYear(t), t);
return `${d === 0 ? `M${w * cellSize},0`
: d === 5 ? `M${(w + 1) * cellSize},0`
: `M${(w + 1) * cellSize},0V${d * cellSize}H${w * cellSize}`}V${5 * cellSize}`;
}

const svg = d3.create("svg")
.attr("width", width)
.attr("height", height * years.length)
.attr("viewBox", [0, 0, width, height * years.length])
.attr("style", "max-width: 100%; height: auto; font: 10px sans-serif;");

const year = svg.selectAll("g")
.data(years)
.join("g")
.attr("transform", (d, i) => `translate(40.5,${height * i + cellSize * 1.5})`);

year.append("text")
.attr("x", -5)
.attr("y", -5)
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.text(([key]) => key);

year.append("g")
.attr("text-anchor", "end")
.selectAll()
.data(d3.range(1, 6))
.join("text")
.attr("x", -5)
.attr("y", i => (countDay(i) + 0.5) * cellSize)
.attr("dy", "0.31em")
.text(formatDay);

year.append("g")
.selectAll()
.data(([, values]) => values.filter(d => ![0, 6].includes(d.date.getUTCDay())))
.join("rect")
.attr("width", cellSize - 1)
.attr("height", cellSize - 1)
.attr("x", d => timeWeek.count(d3.utcYear(d.date), d.date) * cellSize + 0.5)
.attr("y", d => countDay(d.date.getUTCDay()) * cellSize + 0.5)
.attr("fill", d => color(d.value))
.append("title")
.text(d => `${formatDate(d.date)}
${formatValue(d.value)}${d.close === undefined ? "" : `
${formatClose(d.close)}`}`);

const month = year.append("g")
.selectAll()
.data(([, values]) => d3.utcMonths(d3.utcMonth(values[0].date), values.at(-1).date))
.join("g");

month.filter((d, i) => i).append("path")
.attr("fill", "none")
.attr("stroke", "#fff")
.attr("stroke-width", 3)
.attr("d", pathMonth);

month.append("text")
.attr("x", d => timeWeek.count(d3.utcYear(d), timeWeek.ceil(d)) * cellSize + 2)
.attr("y", -5)
.text(formatMonth);

return Object.assign(svg.node(), {scales: {color}});
}
Insert cell
dji = FileAttachment("^DJI.csv").csv({typed: true})
Insert cell
import {Legend} from "@d3/color-legend"
Insert cell
Insert cell
Plot.plot({
padding: 0,
x: {axis: null},
y: {tickFormat: Plot.formatWeekday("en", "narrow"), tickSize: 0},
fy: {tickFormat: "", reverse: true},
color: {scheme: "PiYG", legend: true, label: "Daily change", tickFormat: "+%", domain: [-0.06, 0.06]},
marks: [
Plot.cell(dji, {
x: (d) => d3.utcWeek.count(d3.utcYear(d.Date), d.Date),
y: (d) => d.Date.getUTCDay(),
fy: (d) => d.Date.getUTCFullYear(),
fill: (d, i) => i > 0 ? (d.Close - dji[i - 1].Close) / dji[i - 1].Close : NaN,
title: (d, i) => i > 0 ? ((d.Close - dji[i - 1].Close) / dji[i - 1].Close * 100).toFixed(1) : NaN,
inset: 0.5
})
]
})
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