Stack area charts using D3's stack function. Because this will create a nested data structure, we use LayerCake's flatten function and the flatData option from which we measure the extents.
+page.svelte
./_components/AxisX.percent-range.html.svelte
./_components/AxisY.percent-range.html.svelte
./_components/AreaStacked.svelte
./_data/fruit.csv
<script>import { LayerCake, ScaledSvg, Html, flatten } from'layercake';
import { stack } from'd3-shape';
import { scaleOrdinal } from'd3-scale';
import { format } from'd3-format';
import { timeParse, timeFormat } from'd3-time-format';
importAxisXfrom'./_components/AxisX.percent-range.html.svelte';
importAxisYfrom'./_components/AxisY.percent-range.html.svelte';
importAreaStackedfrom'./_components/AreaStacked.svelte';
// This example loads csv data as json and converts numeric columns to numbers using @rollup/plugin-dsv. See vite.config.js for detailsimport data from'./_data/fruit.csv';
const xKey = 'month';
const yKey = [0, 1];
const zKey = 'key';
const parseDate = timeParse('%Y-%m-%d');
const seriesNames = Object.keys(data[0]).filter(d => d !== xKey);
const seriesColors = ['#ff00cc', '#ff7ac7', '#ffb3c0', '#ffe4b8'];
data.forEach(d => {
d[xKey] = typeof d[xKey] === 'string' ? parseDate(d[xKey]) : d[xKey];
});
/* --------------------------------------------
* Create a stacked data structure
*/const stackData = stack().keys(seriesNames);
const series = stackData(data);
const formatLabelX = timeFormat('%b. %-d');
constformatLabelY = d => format(`~s`)(d);
</script><divclass="chart-container"><LayerCakessrpercentRangepadding={{ top: 0, right: 0, bottom: 20, left: 17 }}x={d => d.data[xKey]}y={yKey}z={zKey}zScale={scaleOrdinal()}zDomain={seriesNames}zRange={seriesColors}flatData={flatten(series)}data={series}
><Html><AxisXformat={formatLabelX}tickMarks /><AxisYformat={formatLabelY} /></Html><ScaledSvg><AreaStacked /></ScaledSvg></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 HTML x-axis, useful for server-side rendered charts. 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.
Although this is marked as a percent-range component, you can also use it with a normal scale with no configuration needed. By default, if you have `percentRange={true}` it will use percentages, otherwise it will use pixels. This makes this component compatible with server-side and client-side rendered charts. Set the `units` prop to either `'%'` or `'px'` to override the default behavior.
--><script>import { getContext } from'svelte';
const { xScale, percentRange } = 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=0] - Any optional value passed to the `dy` attribute on the text label.
* @property {'px'|'%'} [units] - If `percentRange={true}` it defaults to `'%'`, otherwise, the default is `'px'`. Options: `'%'` or `'px'`
*//** @type {Props} */let {
tickMarks = false,
gridlines = true,
tickMarkLength = 6,
baseline = false,
snapLabels = false,
format = d => d,
ticks = undefined,
tickGutter = 0,
dx = 0,
dy = 0,
units = $percentRange === true ? '%' : 'px'
} = $props();
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><divclass="axis x-axis"class:snapLabels>{#each tickVals as tick, i (tick)}{@const tickValUnits = $xScale(tick)}{#if baseline === true}<divclass="baseline"style="top:100%; width:100%;"></div>{/if}{#if gridlines === true}<divclass="gridline"style:left="{tickValUnits}{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%);
}
/* This looks a little better at 40 percent than 50 */.axis.snapLabels.tick:last-child {
transform: translateX(-40%);
}
.axis.snapLabels.tick.tick-0 {
transform: translateX(40%);
}
</style>
<!--
@component
Generates an HTML 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.
Although this is marked as a percent-range component, you can also use it with a normal scale with no configuration needed. By default, if you have `percentRange={true}` it will use percentages, otherwise it will use pixels. This makes this component compatible with server-side and client-side rendered charts. Set the `units` prop to either `'%'` or `'px'` to override the default behavior.
--><script>import { getContext } from'svelte';
const { xRange, yScale, percentRange } = 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=-3] - 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).
* @property {'px'|'%'} [units] - If `percentRange={true}` it defaults to `'%'`, otherwise, the default is `'px'`. Options: `'%'` or `'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 = $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 $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 halfBand = $derived(isBandwidth ? $yScale.bandwidth() / 2 : 0);
let maxTickValUnits = $derived(Math.max(...tickVals.map($yScale)));
</script><divclass="axis y-axis">{#each tickVals as tick, i (tick)}{@const tickValUnits = $yScale(tick)}<divclass="tick tick-{i}"style="left:{$xRange[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,
.baseline,
.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;
}
.baseline.gridline {
border-top-style: solid;
}
.tick.text {
color: #666;
}
</style>
<!--
@component
Generates an SVG area shape using the `area` function from [d3-shape](https://github.com/d3/d3-shape) and sets the color via an ordinal scale in `zScale`. It assumes your data is in a [D3 stack format](https://github.com/d3/d3-shape#stack).
--><script>import { getContext } from'svelte';
import { area } from'd3-shape';
const { data, xGet, yScale, zGet } = getContext('LayerCake');
let areaGen = $derived(
area()
.x(d => $xGet(d))
.y0(d => $yScale(d[0]))
.y1(d => $yScale(d[1]))
);
</script><gclass="area-group">{#each $data as d}<pathclass="path-area"d={areaGen(d)}fill={$zGet(d)}></path>{/each}</g>