A scatter plot with an scaleBand for the y-scale to bucket them by day and a scaleTime for the x-scale. The only real fancy part of this plot is modifying the input data to be in "seconds since start of day" and generating the yDomain as every day between the min and max values, not just days for which we have values. This lets us see days in between that have no data.
+page.svelte
./_components/Scatter.svg.svelte
./_components/AxisX.svelte
./_components/AxisY.svelte
./_data/days.csv
<script>import { LayerCake, Svg, calcExtents } from'layercake';
import { timeDay } from'd3-time';
import { scaleBand, scaleTime } from'd3-scale';
importScatterSvgfrom'./_components/Scatter.svg.svelte';
importAxisXfrom'./_components/AxisX.svelte';
importAxisYfrom'./_components/AxisY.svelte';
// This example loads csv data as json using @rollup/plugin-dsvimport data from'./_data/days.csv';
const xKey = 'seconds';
const yKey = 'day';
const r = 4;
const daysTransformed = data.map(d => {
const parts = d.timestring.split('T');
const time = parts[1]
.replace('Z', '')
.split(':')
.map(q => +q);
d[xKey] = time[0] * 60 * 60 + time[1] * 60 + time[2];
d[yKey] = parts[0];
return d;
});
/* --------------------------------------------
* Generate a range of days in between the min and max
* in case we are missing any in our data so we can show empty days for them
*/const extents = calcExtents(daysTransformed, {
x: d => d.timestring
});
// Convert to string even though it is one to make Typescript happyconst minDate = extents.x[0]
.toString()
.split('T')[0]
.split('-')
.map(d => +d);
const maxDate = extents.x[1]
.toString()
.split('T')[0]
.split('-')
.map(d => +d);
const allDays = timeDay
.range(
newDate(Date.UTC(minDate[0], minDate[1] - 1, minDate[2])),
newDate(Date.UTC(maxDate[0], maxDate[1] - 1, maxDate[2] + 1))
)
.map(d => d.toISOString().split('T')[0])
.sort();
</script><divclass="chart-container"><LayerCakepadding={{ top: 0, right: 15, bottom: 20, left: 75 }}x={xKey}y={yKey}xDomain={[0, 24 * 60 * 60]}yDomain={allDays}xScale={scaleTime()}yScale={scaleBand().paddingInner(0.05).round(true)}data={daysTransformed}
><Svg><AxisXticks={[0, 4, 8, 12, 16, 20, 24].map(d => d * 60 * 60)}format={d =>`${Math.floor(d / 60 / 60)}:00`}
/><AxisY /><ScatterSvg{r}fill="rgba(255, 204, 0, 0.75)" /></Svg></LayerCake></div><style>/*
The wrapper div needs to have an explicit width and height in CSS.
It can also be a flexbox child or CSS grid element.
The point being it needs dimensions since the <LayerCake> element will
expand to fill it.
*/.chart-container {
width: 100%;
height: 250px;
}
</style>
<!--
@component
Generates an SVG scatter plot. This component can also work if the x- or y-scale is ordinal, i.e. it has a `.bandwidth` method. See the [timeplot chart](https://layercake.graphics/example/Timeplot) for an example.
--><script>import { getContext } from'svelte';
const { data, xGet, yGet, xScale, yScale } = getContext('LayerCake');
/**
* @typedef {Object} Props
* @property {number} [r=5] - The circle's radius.
* @property {string} [fill='#0cf'] - The circle's fill color.
* @property {string} [stroke='#000'] - The circle's stroke color.
* @property {number} [strokeWidth=0] - The circle's stroke width.
*//** @type {Props} */let { r = 5, fill = '#0cf', stroke = '#000', strokeWidth = 0 } = $props();
</script><gclass="scatter-group">{#each $data as d}<circlecx={$xGet(d) + ($xScale.bandwidth ? $xScale.bandwidth() / 2 : 0)}cy={$yGet(d) + ($yScale.bandwidth ? $yScale.bandwidth() / 2 : 0)}{r}{fill}{stroke}stroke-width={strokeWidth}
/>{/each}</g>
<!--
@component
Generates an SVG x-axis. This component is also configured to detect if your x-scale is an ordinal scale. If so, it will place the markers in the middle of the bandwidth.
--><script>import { getContext } from'svelte';
const { width, height, xScale, yRange } = getContext('LayerCake');
/**
* @typedef {Object} Props
* @property {boolean} [tickMarks=false] - Show a vertical mark for each tick.
* @property {boolean} [gridlines=true] - Show gridlines extending into the chart area.
* @property {number} [tickMarkLength=6] - The length of the tick mark.
* @property {boolean} [baseline=false] - Show a solid line at the bottom.
* @property {boolean} [snapLabels=false] - Instead of centering the text labels on the first and the last items, align them to the edges of the chart.
* @property {(d: any) => string} [format=d => d] - A function that passes the current tick value and expects a nicely formatted value in return.
* @property {number|Array<any>|Function} [ticks] - If this is a number, it passes that along to the [d3Scale.ticks](https://github.com/d3/d3-scale) function. If this is an array, hardcodes the ticks to those values. If it's a function, passes along the default tick values and expects an array of tick values in return. If nothing, it uses the default ticks supplied by the D3 function.
* @property {number} [tickGutter=0] - The amount of whitespace between the start of the tick and the chart drawing area (the yRange min).
* @property {number} [dx=0] - Any optional value passed to the `dx` attribute on the text label.
* @property {number} [dy=12] - Any optional value passed to the `dy` attribute on the text label.
*//** @type {Props} */let {
tickMarks = false,
gridlines = true,
tickMarkLength = 6,
baseline = false,
snapLabels = false,
format = d => d,
ticks = undefined,
tickGutter = 0,
dx = 0,
dy = 12
} = $props();
/** @param {number} i
* @param {boolean} sl */functiontextAnchor(i, sl) {
if (sl === true) {
if (i === 0) {
return'start';
}
if (i === tickVals.length - 1) {
return'end';
}
}
return'middle';
}
let tickLen = $derived(tickMarks === true ? (tickMarkLength ?? 6) : 0);
let isBandwidth = $derived(typeof $xScale.bandwidth === 'function');
/** @type {Array<any>} */let tickVals = $derived(
Array.isArray(ticks)
? ticks
: isBandwidth
? $xScale.domain()
: typeof ticks === 'function'
? ticks($xScale.ticks())
: $xScale.ticks(ticks)
);
let halfBand = $derived(isBandwidth ? $xScale.bandwidth() / 2 : 0);
</script><gclass="axis x-axis"class:snapLabels>{#each tickVals as tick, i (tick)}{#if baseline === true}<lineclass="baseline"y1={$height}y2={$height}x1="0"x2={$width} />{/if}<gclass="tick tick-{i}"transform="translate({$xScale(tick)},{Math.max(...$yRange)})">{#if gridlines === true}<lineclass="gridline"x1={halfBand}x2={halfBand}y1={-$height}y2="0" />{/if}{#if tickMarks === true}<lineclass="tick-mark"x1={halfBand}x2={halfBand}y1={tickGutter}y2={tickGutter + tickLen}
/>{/if}<textx={halfBand}y={tickGutter + tickLen}{dx}{dy}text-anchor={textAnchor(i, snapLabels)}
>{format(tick)}</text
>
</g>{/each}</g><style>.tick {
font-size: 11px;
}
line,
.tickline {
stroke: #aaa;
stroke-dasharray: 2;
}
.ticktext {
fill: #666;
}
.tick.tick-mark,
.baseline {
stroke-dasharray: 0;
}
/* This looks slightly better */.axis.snapLabels.tick:last-childtext {
transform: translateX(3px);
}
.axis.snapLabels.tick.tick-0text {
transform: translateX(-3px);
}
</style>
<!--
@component
Generates an SVG y-axis. This component is also configured to detect if your y-scale is an ordinal scale. If so, it will place the tickMarks in the middle of the bandwidth.
--><script>import { getContext } from'svelte';
const { xRange, yScale, width } = getContext('LayerCake');
/**
* @typedef {Object} Props
* @property {boolean} [tickMarks=false] - Show marks next to the tick label.
* @property {string} [labelPosition='even'] - Whether the label sits even with its value ('even') or sits on top ('above') the tick mark. Default is 'even'.
* @property {boolean} [snapBaselineLabel=false] - When labelPosition='even', adjust the lowest label so that it sits above the tick mark.
* @property {boolean} [gridlines=true] - Show gridlines extending into the chart area.
* @property {number} [tickMarkLength] - The length of the tick mark. If not set, becomes the length of the widest tick.
* @property {(d: any) => string} [format=d => d] - A function that passes the current tick value and expects a nicely formatted value in return.
* @property {number|Array<any>|Function} [ticks=4] - If this is a number, it passes that along to the [d3Scale.ticks](https://github.com/d3/d3-scale) function. If this is an array, hardcodes the ticks to those values. If it's a function, passes along the default tick values and expects an array of tick values in return.
* @property {number} [tickGutter=0] - The amount of whitespace between the start of the tick and the chart drawing area (the xRange min).
* @property {number} [dx=0] - Any optional value passed to the `dx` attribute on the text label.
* @property {number} [dy=0] - Any optional value passed to the `dy` attribute on the text label.
* @property {number} [charPixelWidth=7.25] - Used to calculate the widest label length to offset labels. Adjust if the automatic tick length doesn't look right because you have a bigger font (or just set `tickMarkLength` to a pixel value).
*//** @type {Props} */let {
tickMarks = false,
labelPosition = 'even',
snapBaselineLabel = false,
gridlines = true,
tickMarkLength = undefined,
format = d => d,
ticks = 4,
tickGutter = 0,
dx = 0,
dy = 0,
charPixelWidth = 7.25
} = $props();
/** @param {number} sum
* @param {string} val */functioncalcStringLength(sum, val) {
if (val === ',' || val === '.') return sum + charPixelWidth * 0.5;
return sum + charPixelWidth;
}
let isBandwidth = $derived(typeof $yScale.bandwidth === 'function');
/** @type {Array<any>} */let tickVals = $derived(
Array.isArray(ticks)
? ticks
: isBandwidth
? $yScale.domain()
: typeof ticks === 'function'
? ticks($yScale.ticks())
: $yScale.ticks(ticks)
);
let widestTickLen = $derived(
Math.max(
10,
Math.max(...tickVals.map(d =>format(d).toString().split('').reduce(calcStringLength, 0)))
)
);
let tickLen = $derived(
tickMarks === true
? labelPosition === 'above'
? (tickMarkLength ?? widestTickLen)
: (tickMarkLength ?? 6)
: 0
);
let x1 = $derived(-tickGutter - (labelPosition === 'above' ? widestTickLen : tickLen));
let y = $derived(isBandwidth ? $yScale.bandwidth() / 2 : 0);
let maxTickValPx = $derived(Math.max(...tickVals.map($yScale)));
</script><gclass="axis y-axis">{#each tickVals astick (tick)}{@const tickValPx = $yScale(tick)}<gclass="tick tick-{tick}"transform="translate({$xRange[0]}, {tickValPx})">{#if gridlines === true}<lineclass="gridline"{x1}x2={$width}y1={y}y2={y}></line>{/if}{#if tickMarks === true}<lineclass="tick-mark"{x1}x2={x1 + tickLen}y1={y}y2={y}></line>{/if}<textx={x1}{y}dx={dx + (labelPosition === 'even' ? -3 : 0)}text-anchor={labelPosition === 'above' ? 'start' : 'end'}dy={dy +
(labelPosition === 'above' || (snapBaselineLabel === true && tickValPx === maxTickValPx)
? -3
: 4)}>{format(tick)}</text
>
</g>{/each}</g><style>.tick {
font-size: 11px;
}
.tickline {
stroke: #aaa;
}
.tick.gridline {
stroke-dasharray: 2;
}
.ticktext {
fill: #666;
}
.tick.tick-0line {
stroke-dasharray: 0;
}
</style>