You could reshape the rows into name, value and group and plot them like a scatter plot. This chart skips that with Layer Cake's accessor shorthand: the x accessor is the list of columns to plot, so k.xGet(row) turns each row into a list of pixel values, one per dot.
This is the server-side rendered version. ssr and percentRange on <LayerCake> put the scales in percentages, so the chart renders before the browser measures it. The axes are HTML components and so are the dots.
<script>import { LayerCake, Html } from'layercake';
import { scaleBand, scaleOrdinal } from'd3-scale';
importClevelandDotPlotfrom'./_components/ClevelandDotPlot.percent-range.html.svelte';
importAxisXfrom'./_components/AxisX.percent-range.html.svelte';
importAxisYfrom'./_components/AxisY.percent-range.html.svelte';
// The CSV rows are parsed, and their numbers typed, by @rollup/plugin-dsv. See vite.config.jsimport data from'./_data/fruitOrdinal.csv';
const yKey = 'year';
// Every column but the year is a dot, so the x accessor is the list of those columnsconst xKey = Object.keys(data[0]).filter(d => d !== yKey);
const seriesColors = ['#ff00cc', '#00bbff', '#00e047', '#ff7a33'];
</script><divclass="chart-container"><!-- xPadding is in percent units here, so it is smaller than the client-side version's 10 pixels --><LayerCakessrpercentRangepadding={{ right: 10, bottom: 20, left: 30 }}x={xKey}y={yKey}yScale={scaleBand().paddingInner(0.05)}yDomainSort={true}xDomain={[0, null]}xPadding={[2, 0]}cScale={scaleOrdinal()}cDomain={xKey}cRange={seriesColors}{data}
><Html><AxisX /><AxisYgridlines={false} /><ClevelandDotPlot /></Html></LayerCake></div><style>/* Give the wrapper a width and height. LayerCake fills it. */.chart-container {
width: 100%;
height: 250px;
}
</style>
<!--
@component
Generates an HTML Cleveland dot plot, also known as a lollipop chart. Positions are percentages, so use it with `percentRange={true}`. The y scale must be a band scale.
--><script>import { getLayerCakeContext } from'layercake';
const k = getLayerCakeContext();
/**
* @typedef {Object} Props
* @property {number} [r=5] - The circle radius in pixels.
*//** @type {Props} */let { r = 5 } = $props();
let halfBand = $derived(k.yScale.bandwidth() / 2);
</script><divclass="dot-plot">{#each k.dataas row}{@const yPos = k.yGet(row)}{@const xPositions = k.xGet(row)}<divclass="dot-row"><divclass="line"style="
left: {Math.min(...xPositions)}%;
top: {yPos + halfBand}%;
right: {100 - Math.max(...xPositions)}%;
"
></div>{#each xPositions as circleX, i}<divclass="circle"style="
left: {circleX}%;
top: {yPos + halfBand}%;
width: {r * 2}px;
height: {r * 2}px;
background: {k.cScale?.(k.config.x[i]) ?? '#ccc'};
"
></div>{/each}</div>{/each}</div><style>.line {
position: absolute;
border-bottom: 1px solid #000;
}
.circle {
position: absolute;
border-radius: 50%;
border: 1px solid #000;
transform: translate(-50%, -50%);
}
</style>
<!--
@component
Generates an HTML x-axis along the bottom of the chart, for server-side rendered charts. If the x scale is a band scale, each tick sits in the middle of its band.
Positions are percentages when `percentRange={true}` and pixels otherwise, so this also works in a client-side chart with no setup. Set the `units` prop to `'%'` or `'px'` to override that.
--><script>import { getLayerCakeContext } from'layercake';
const k = getLayerCakeContext();
/**
* @typedef {Object} Props
* @property {boolean} [tickMarks=false] - Show a vertical mark at 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} [showBaseline=false] - Show a solid line along the bottom of the chart.
* @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] - Formats a tick value for display.
* @property {number|Array<any>|((ticks: Array<any>) => Array<any>)} [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 gap in pixels between the bottom of the chart area and the start of the tick.
* @property {number} [dx=0] - Horizontal offset of the label in pixels.
* @property {number} [dy=0] - Vertical offset of the label in pixels.
* @property {'px'|'%'} [units] - Position with pixels or percentages. Defaults to `'%'` when `percentRange={true}`, otherwise `'px'`.
*//** @type {Props} */let {
tickMarks = false,
gridlines = true,
tickMarkLength = 6,
showBaseline = false,
snapLabels = false,
format = d => d,
ticks = undefined,
tickGutter = 0,
dx = 0,
dy = 0,
units = k.percentRange === true ? '%' : 'px'
} = $props();
let tickLen = $derived(tickMarks === true ? (tickMarkLength ?? 6) : 0);
let isBandwidth = $derived(typeof k.xScale.bandwidth === 'function');
/** @type {Array<any>} */let tickVals = $derived(
Array.isArray(ticks)
? ticks
: isBandwidth
? k.xScale.domain()
: typeof ticks === 'function'
? ticks(k.xScale.ticks())
: k.xScale.ticks(ticks)
);
let halfBand = $derived(isBandwidth ? k.xScale.bandwidth() / 2 : 0);
</script><divclass="axis x-axis"class:snapLabels>{#if showBaseline === true}<divclass="baseline"style="top:100%; width:100%;"></div>{/if}{#each tickVals as tick, i (tick)}{@const tickValUnits = k.xScale(tick)}{#if gridlines === true}<divclass="gridline"style:left="{tickValUnits + halfBand}{units}"style="top:0; bottom:0;"
></div>{/if}{#if tickMarks === true}<divclass="tick-mark"style:left="{tickValUnits + halfBand}{units}"style:height="{tickLen}px"style:bottom="{-tickLen - tickGutter}px"
></div>{/if}<divclass="tick tick-{i}"style:left="{tickValUnits + halfBand}{units}"style="top:calc(100% + {tickGutter}px);"
><divclass="text"style:top="{tickLen}px"style:transform="translate(calc(-50% + {dx}px), {dy}px)"
>{format(tick)}</div></div>{/each}</div><style>.axis,
.tick,
.tick-mark,
.gridline,
.baseline {
position: absolute;
}
.axis {
width: 100%;
height: 100%;
}
.tick {
font-size: 11px;
}
.gridline {
border-left: 1px dashed #aaa;
}
.tick-mark {
border-left: 1px solid #aaa;
}
.baseline {
border-top: 1px solid #aaa;
}
.tick.text {
color: #666;
position: relative;
white-space: nowrap;
transform: translateX(-50%);
}
/* Snapped end labels sit 40% inside their edge instead of centered on it */.axis.snapLabels.tick:last-child {
transform: translateX(-40%);
}
.axis.snapLabels.tick.tick-0 {
transform: translateX(40%);
}
</style>
<!--
@component
Generates an HTML y-axis along the left edge of the chart, for server-side rendered charts. If the y scale is a band scale, each tick sits in the middle of its band.
Positions are percentages when `percentRange={true}` and pixels otherwise, so this also works in a client-side chart with no setup. Set the `units` prop to `'%'` or `'px'` to override that.
--><script>import { getLayerCakeContext } from'layercake';
const k = getLayerCakeContext();
/**
* @typedef {Object} Props
* @property {boolean} [tickMarks=false] - Show a horizontal mark at each tick.
* @property {'even'|'above'} [labelPosition='even'] - Whether the label sits level with its tick ('even') or above it ('above').
* @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] - Length of the tick mark in pixels. Defaults to the width of the widest label when `labelPosition` is 'above', otherwise 6.
* @property {(d: any) => string} [format=d => d] - Formats a tick value for display.
* @property {number|Array<any>|((ticks: Array<any>) => Array<any>)} [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 gap in pixels between the left edge of the chart area and the tick.
* @property {number} [dx=0] - Horizontal offset of the label in pixels.
* @property {number} [dy=-3] - Vertical offset of the label in pixels.
* @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).
* @property {'px'|'%'} [units] - Position with pixels or percentages. Defaults to `'%'` when `percentRange={true}`, otherwise `'px'`.
*//** @type {Props} */let {
tickMarks = false,
labelPosition = 'even',
snapBaselineLabel = false,
gridlines = true,
tickMarkLength = undefined,
format = d => d,
ticks = 4,
tickGutter = 0,
dx = 0,
dy = -3,
charPixelWidth = 7.25,
units = k.percentRange === true ? '%' : 'px'
} = $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 k.yScale.bandwidth === 'function');
/** @type {Array<any>} */let tickVals = $derived(
Array.isArray(ticks)
? ticks
: isBandwidth
? k.yScale.domain()
: typeof ticks === 'function'
? ticks(k.yScale.ticks())
: k.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 halfBand = $derived(isBandwidth ? k.yScale.bandwidth() / 2 : 0);
let maxTickValUnits = $derived(Math.max(...tickVals.map(k.yScale)));
</script><divclass="axis y-axis">{#each tickVals as tick, i (tick)}{@const tickValUnits = k.yScale(tick)}<divclass="tick tick-{i}"style="left:{k.xRange ? k.xRange[0] : 0}{units};top:{tickValUnits + halfBand}{units};"
>{#if gridlines === true}<divclass="gridline"style="top:0;"style:left="{x1}px"style:right="0px"></div>{/if}{#if tickMarks === true}<divclass="tick-mark"style:top="0"style:left="{x1}px"style:width="{tickLen}px"></div>{/if}<divclass="text"style:top="0"style:text-align={labelPosition === 'even' ? 'right' : 'left'}style:width="{widestTickLen}px"style:left="{-widestTickLen - tickGutter - (labelPosition === 'even' ? tickLen : 0)}px"style:transform="translate({dx + (labelPosition === 'even' ? -3 : 0)}px, calc(-50% + {dy +
(labelPosition === 'above' ||
(snapBaselineLabel === true && tickValUnits === maxTickValUnits)
? -3
: 4)}px))"
>{format(tick)}</div></div>{/each}</div><style>.axis,
.tick,
.tick-mark,
.gridline,
.text {
position: absolute;
}
.axis {
width: 100%;
height: 100%;
}
.tick {
font-size: 11px;
width: 100%;
}
.gridline {
border-top: 1px dashed #aaa;
}
.tick-mark {
border-top: 1px solid #aaa;
}
.tick.text {
color: #666;
}
</style>