<script>importSyncedBrushWrapperfrom'./_components/SyncedBrushWrapper.svelte';
// This example loads csv data as json using @rollup/plugin-dsvimport pointsOne from'./_data/points.csv';
import pointsTwo from'./_data/pointsTwo.csv';
import pointsThree from'./_data/pointsThree.csv';
import pointsFour from'./_data/pointsFour.csv';
let brushExtents = $state([null, null]);
const xKey = 'myX';
const yKey = 'myY';
const datasets = [pointsOne, pointsTwo, pointsThree, pointsFour];
datasets.forEach(dataset => {
dataset.forEach(d => {
d[yKey] = +d[yKey];
});
});
const colors = ['#00e047', '#00bbff', '#ff00cc', '#ffcc00'];
</script><divclass="chart-container">{#each datasets as dataset, i}<SyncedBrushWrapperdata={dataset}{xKey}{yKey}bind:min={brushExtents[0]}bind:max={brushExtents[1]}stroke={colors[i]}
/>{/each}</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;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-content: space-between;
}
</style>
<script>import { LayerCake, Svg, Html } from'layercake';
importLinefrom'./Line.svelte';
importAreafrom'./Area.svelte';
importAxisXfrom'./AxisX.svelte';
importAxisYfrom'./AxisY.svelte';
importBrushfrom'./Brush.html.svelte';
/**
* @typedef {Object} Props
* @property {any} [min]
* @property {any} [max]
* @property {string} [xKey]
* @property {string} [yKey]
* @property {any} [data]
* @property {string} [stroke]
*//** @type {Props} */let {
min = $bindable(null),
max = $bindable(null),
xKey = 'x',
yKey = 'y',
data = [],
stroke = '#00e047'
} = $props();
let brushedData = $derived.by(() => {
const start = Math.max(0, Math.floor((min ?? 0) * data.length));
const end = Math.min(data.length, Math.ceil((max ?? 1) * data.length));
let brushed = data.slice(start, end);
if (brushed.length < 2 && data.length >= 2) {
return data.slice(start, start + 2);
}
return brushed;
});
</script><divclass="chart-wrapper"><divclass="chart-container"><LayerCakepadding={{ bottom: 20, left: 25 }}x={xKey}y={yKey}yDomain={[0, null]}data={brushedData}
><Svg><AxisXticks={ticks => {
const filtered = ticks.filter(t => t % 1 === 0);
if (filtered.length > 7) {
return filtered.filter((t, i) => i % 2 === 0);
}
return filtered;
}}
/><AxisYticks={2} /><Line{stroke} /><Areafill={`${stroke}10`} /></Svg></LayerCake></div><divclass="brush-container"><LayerCakepadding={{ top: 5 }}x={xKey}y={yKey}yDomain={[0, null]}{data}><Svg><Line{stroke} /><Areafill={`${stroke}10`} /></Svg><Html><Brushbind:minbind:max /></Html></LayerCake></div></div><style>.chart-wrapper {
width: 48%;
height: 40%;
}
/*
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: 80%;
}
.brush-container {
width: 100%;
height: 20%;
}
</style>
<!--
@component
Generates an SVG line shape.
--><script>import { getContext } from'svelte';
const { data, xGet, yGet } = getContext('LayerCake');
/**
* @typedef {Object} Props
* @property {string} [stroke='#ab00d6'] - The shape's fill color. This is technically optional because it comes with a default value but you'll likely want to replace it with your own color.
*//** @type {Props} */let { stroke = '#ab00d6' } = $props();
let path = $derived(
'M' +
$data
.map(d => {
return $xGet(d) + ',' + $yGet(d);
})
.join('L')
);
</script><pathclass="path-line"d={path}{stroke}></path><style>.path-line {
fill: none;
stroke-linejoin: round;
stroke-linecap: round;
stroke-width: 2;
}
</style>
<!--
@component
Generates an SVG area shape.
--><script>import { getContext } from'svelte';
const { data, xGet, yGet, xScale, yScale, extents } = getContext('LayerCake');
/**
* @typedef {Object} Props
* @property {string} [fill='#ab00d610'] - The shape's fill color. This is technically optional because it comes with a default value but you'll likely want to replace it with your own color.
*//** @type {Props} */let { fill = '#ab00d610' } = $props();
let path = $derived(
'M' +
$data
.map((/** @type {object} */ d) => {
return $xGet(d) + ',' + $yGet(d);
})
.join('L')
);
/** @type {string} **/let area = $derived.by(() => {
const yRange = $yScale.range();
return (
path +
('L' +
$xScale($extents.x ? $extents.x[1] : 0) +
',' +
yRange[0] +
'L' +
$xScale($extents.x ? $extents.x[0] : 0) +
',' +
yRange[0] +
'Z')
);
});
</script><pathclass="path-area"d={area}{fill}></path>
<!--
@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>
<!--
@component
Adds a brush component to create a range between 0 and 1. Bind to the `min` and `max` props to use them in other components. See the [brushable example](https://layercake.graphics/example/Brush) for use.
--><script>import { clamp } from'yootils';
/**
* @typedef {Object} Props
* @property {number|null} min - The brush's min value. Useful to bind to.
* @property {number|null} max - The brush's max value. Useful to bind to.
*//** @type {Props} */let { min = $bindable(), max = $bindable() } = $props();
let brush = $state();
constp = x => {
const { left, right } = brush.getBoundingClientRect();
returnclamp((x - left) / (right - left), 0, 1);
};
consthandler = fn => {
returne => {
e.stopPropagation();
e.preventDefault(); // Prevent default drag behaviorif (e.type === 'touchstart') {
if (e.touches.length !== 1) return;
e = e.touches[0];
}
const id = e.identifier;
const start = { min, max, p: p(e.clientX) };
consthandle_move = e => {
e.preventDefault(); // Prevent default drag behavior during moveif (e.type === 'touchmove') {
if (e.changedTouches.length !== 1) return;
e = e.changedTouches[0];
if (e.identifier !== id) return;
}
fn(start, p(e.clientX));
};
consthandle_end = e => {
if (e.type === 'touchend') {
if (e.changedTouches.length !== 1) return;
if (e.changedTouches[0].identifier !== id) return;
} elseif (e.target === brush) {
clear();
}
window.removeEventListener('mousemove', handle_move);
window.removeEventListener('mouseup', handle_end);
window.removeEventListener('touchmove', handle_move);
window.removeEventListener('touchend', handle_end);
};
window.addEventListener('mousemove', handle_move);
window.addEventListener('mouseup', handle_end);
window.addEventListener('touchmove', handle_move);
window.addEventListener('touchend', handle_end);
};
};
constclear = () => {
min = null;
max = null;
};
const reset = handler((start, p) => {
min = clamp(Math.min(start.p, p), 0, 1);
max = clamp(Math.max(start.p, p), 0, 1);
});
const move = handler((start, p) => {
const d = clamp(p - start.p, -start.min, 1 - start.max);
min = start.min + d;
max = start.max + d;
});
const adjust_min = handler((start, p) => {
min = p > start.max ? start.max : p;
max = p > start.max ? p : start.max;
});
const adjust_max = handler((start, p) => {
min = p < start.min ? p : start.min;
max = p < start.min ? start.min : p;
});
let left = $derived(min !== null ? 100 * min : null);
let right = $derived(max !== null ? 100 * (1 - max) : null);
</script><!-- TODO Add keyboard accessibility. See https://github.com/mhkeller/layercake/pull/258 --><!-- svelte-ignore a11y_no_static_element_interactions --><divbind:this={brush}class="brush-outer"onmousedown={reset}ontouchstart={reset}>{#if min !== null}<divclass="brush-inner"draggable="false"onmousedown={move}ontouchstart={move}style="left: {left}%; right: {right}%"
></div><divclass="brush-handle"draggable="false"onmousedown={adjust_min}ontouchstart={adjust_min}style="left: {left}%"
></div><divclass="brush-handle"draggable="false"onmousedown={adjust_max}ontouchstart={adjust_max}style="right: {right}%"
></div>{/if}</div><style>.brush-outer {
position: relative;
width: 100%;
height: calc(100% + 5px);
top: -5px;
}
.brush-inner {
position: absolute;
height: 100%;
cursor: move;
/* mix-blend-mode: difference; */background-color: #cccccc90;
/* border: 1px solid #000; */
}
.brush-handle {
position: absolute;
width: 0;
height: 100%;
cursor: ew-resize;
}
.brush-handle::before {
position: absolute;
content: '';
width: 8px;
left: -4px;
height: 100%;
background: transparent;
}
</style>