<script>
import { LayerCake, Svg, Html } from 'layercake';
import { scaleOrdinal } from 'd3-scale';
import { timeParse, timeFormat } from 'd3-time-format';
import { format, precisionFixed } from 'd3-format';
import MultiLine from './components/MultiLine.svelte';
import AxisX from './components/AxisX.svelte';
import AxisY from './components/AxisY.svelte';
import Labels from './components/Labels.svelte';
import SharedTooltip from './components/SharedTooltip.svelte';
import data from './data/fruit.csv';
const xKey = 'month';
const yKey = 'value';
const zKey = 'key';
const seriesNames = Object.keys(data[0]).filter(d => d !== xKey);
const seriesColors = ['#ffe4b8', '#ffb3c0', '#ff7ac7', '#ff00cc'];
const parseDate = timeParse('%Y-%m-%d');
const dataLong = seriesNames.map(key => {
return {
key,
values: data.map(d => {
d[xKey] = typeof d[xKey] === 'string' ? parseDate(d[xKey]) : d[xKey];
return {
key,
[yKey]: +d[key],
[xKey]: d[xKey]
};
})
};
});
const flatten = data => data.reduce((memo, group) => {
return memo.concat(group.values);
}, []);
const formatTickX = timeFormat('%b. %e');
const formatTickY = d => format(`.${precisionFixed(d)}s`)(d);
</script>
<style>
.chart-container {
width: 100%;
height: 100%;
}
</style>
<div class="chart-container">
<LayerCake
padding={{ top: 7, right: 10, bottom: 20, left: 25 }}
x={xKey}
y={yKey}
z={zKey}
yDomain={[0, null]}
zScale={scaleOrdinal()}
zDomain={seriesNames}
zRange={seriesColors}
flatData={flatten(dataLong)}
data={dataLong}
>
<Svg>
<AxisX
gridlines={false}
ticks={data.map(d => d[xKey]).sort((a, b) => a - b)}
formatTick={formatTickX}
snapTicks={true}
/>
<AxisY
ticks={4}
formatTick={formatTickY}
/>
<MultiLine/>
</Svg>
<Html>
<Labels/>
<SharedTooltip
formatTitle={formatTickX}
dataset={data}
/>
</Html>
</LayerCake>
</div>
<script>
import { getContext } from 'svelte';
const { data, xGet, yGet, zGet, xScale, yScale, xRange, yRange, xDomain, yDomain } = getContext('LayerCake');
$: path = values => {
return 'M' + values
.map(d => {
return $xGet(d) + ',' + $yGet(d);
})
.join('L');
};
</script>
<g class="line-group">
{#each $data as group}
<path
class='path-line'
d='{path(group.values)}'
stroke="{$zGet(group)}"
></path>
{/each}
</g>
<style>
.path-line {
fill: none;
stroke-linejoin: round;
stroke-linecap: round;
stroke-width: 3px;
}
</style>
<script>
import { getContext } from 'svelte';
const { width, height, xScale, yScale, yRange } = getContext('LayerCake');
export let gridlines = true;
export let formatTick = d => d;
export let baseline = false;
export let snapTicks = false;
export let ticks = undefined;
export let xTick = undefined;
export let yTick = 16;
export let dxTick = 0;
export let dyTick = 0;
$: isBandwidth = typeof $xScale.bandwidth === 'function';
$: tickVals = Array.isArray(ticks) ? ticks :
isBandwidth ?
$xScale.domain() :
typeof ticks === 'function' ?
ticks($xScale.ticks()) :
$xScale.ticks(ticks);
function textAnchor(i) {
if (snapTicks === true) {
if (i === 0) {
return 'start';
}
if (i === tickVals.length - 1) {
return 'end';
}
}
return 'middle';
}
</script>
<g class='axis x-axis'>
{#each tickVals as tick, i}
<g class='tick tick-{ tick }' transform='translate({$xScale(tick)},{$yRange[0]})'>
{#if gridlines !== false}
<line y1='{$height * -1}' y2='0' x1='0' x2='0'></line>
{/if}
<text
x="{xTick || isBandwidth ? $xScale.bandwidth() / 2 : 0 }"
y='{yTick}'
dx='{dxTick}'
dy='{dyTick}'
text-anchor='{textAnchor(i)}'>{formatTick(tick)}</text>
</g>
{/each}
{#if baseline === true}
<line class="baseline" y1='{$height + 0.5}' y2='{$height + 0.5}' x1='0' x2='{$width}'></line>
{/if}
</g>
<style>
.tick {
font-size: .725em;
font-weight: 200;
}
line,
.tick line {
stroke: #aaa;
stroke-dasharray: 2;
}
.tick text {
fill: #666;
}
.baseline {
stroke-dasharray: 0;
}
</style>
<script>
import { getContext } from 'svelte';
const { padding, xRange, xScale, yScale } = getContext('LayerCake');
export let ticks = 4;
export let gridlines = true;
export let formatTick = d => d;
export let xTick = 0;
export let yTick = 0;
export let dxTick = 0;
export let dyTick = -4;
export let textAnchor = 'start';
$: isBandwidth = typeof $yScale.bandwidth === 'function';
$: tickVals = Array.isArray(ticks) ? ticks :
isBandwidth ?
$yScale.domain() :
typeof ticks === 'function' ?
ticks($yScale.ticks()) :
$yScale.ticks(ticks);
</script>
<g class='axis y-axis' transform='translate({-$padding.left}, 0)'>
{#each tickVals as tick, i}
<g class='tick tick-{tick}' transform='translate({$xRange[0] + (isBandwidth ? $padding.left : 0)}, {$yScale(tick)})'>
{#if gridlines !== false}
<line
x2='100%'
y1={yTick + (isBandwidth ? ($yScale.bandwidth() / 2) : 0)}
y2={yTick + (isBandwidth ? ($yScale.bandwidth() / 2) : 0)}
></line>
{/if}
<text
x='{xTick}'
y='{yTick + (isBandwidth ? $yScale.bandwidth() / 2 : 0)}'
dx='{isBandwidth ? -5 : dxTick}'
dy='{isBandwidth ? 4 : dyTick}'
style="text-anchor:{isBandwidth ? 'end' : textAnchor};"
>{formatTick(tick)}</text>
</g>
{/each}
</g>
<style>
.tick {
font-size: .725em;
font-weight: 200;
}
.tick line {
stroke: #aaa;
stroke-dasharray: 2;
}
.tick text {
fill: #666;
}
.tick.tick-0 line {
stroke-dasharray: 0;
}
</style>
<script>
import { getContext } from 'svelte';
import { max } from 'd3-array';
const { data, x, y, xScale, yScale, xRange, yRange } = getContext('LayerCake');
const cap = val => val.replace(/^\w/, d => d.toUpperCase());
$: left = values => $xScale(max(values, $x)) / Math.max(...$xRange);
$: top = values => $yScale(max(values, $y)) / Math.max(...$yRange);
</script>
{#each $data as group}
<div
class="label"
style="
top:{top(group.values) * 100}%;
left:{left(group.values) * 100}%;
"
>{cap(group.key)}</div>
{/each}
<style>
.label {
position: absolute;
transform: translate(-100%, -100%)translateY(1px);
font-size: 13px;
}
</style>
<script>
import { getContext } from 'svelte';
import { format } from 'd3-format';
import QuadTree from './QuadTree.svelte';
const { data, width, yScale, config } = getContext('LayerCake');
const commas = format(',');
const titleCase = d => d.replace(/^\w/, w => w.toUpperCase());
export let tooltipOffset = 20;
export let dataset = undefined;
export let formatTitle = d => d;
export let formatKey = d => titleCase(d);
export let formatValue = d => isNaN(+d) ? d : commas(d);
const w = 150;
const w2 = w / 2;
let top = 0;
function sortResult(result) {
if (Object.keys(result).length === 0) return [];
const rows = Object.keys(result).filter(d => d !== $config.x).map(key => {
return {
key,
value: result[key]
};
}).sort((a, b) => b.value - a.value);
return rows;
}
</script>
<style>
.tooltip {
position: absolute;
font-size: 13px;
pointer-events: none;
border: 1px solid #ccc;
background: rgba(255, 255, 255, 0.85);
transform: translate(-50%, -100%);
padding: 5px;
z-index: 15;
pointer-events: none;
}
.line {
position: absolute;
top: 0;
bottom: 0;
width: 1px;
border-left: 1px dotted #666;
pointer-events: none;
}
.tooltip,
.line {
transition: left 250ms ease-out, top 250ms ease-out;
}
.title {
font-weight: bold;
}
.key {
color: #999;
}
</style>
<QuadTree
dataset={dataset || $data}
y='x'
let:x
let:y
let:visible
let:found
let:e
>
{#if visible === true}
<div
style="left:{x}px;"
class="line"></div>
<div
class="tooltip"
style="
width:{w}px;
display: { visible ? 'block' : 'none' };
top:{$yScale(sortResult(found)[0].value) - tooltipOffset}px;
left:{Math.min(Math.max(w2, x), $width - w2)}px;"
>
<div class="title">{formatTitle(found[$config.x])}</div>
{#each sortResult(found) as row}
<div class="row"><span class="key">{formatKey(row.key)}:</span> {formatValue(row.value)}</div>
{/each}
</div>
{/if}
</QuadTree>
<script>
import { getContext } from 'svelte';
import { quadtree } from 'd3-quadtree';
const { data, xGet, yGet, width, height } = getContext('LayerCake');
let visible = false;
let found = {};
let e = {};
export let dataset = undefined;
export let x = 'x';
export let y = 'y';
export let searchRadius = undefined;
$: xGetter = x === 'x' ? $xGet : $yGet;
$: yGetter = y === 'y' ? $yGet : $xGet;
function findItem (evt) {
e = evt;
const xLayerKey = `layer${x.toUpperCase()}`;
const yLayerKey = `layer${y.toUpperCase()}`;
found = finder.find(evt[xLayerKey], evt[yLayerKey], searchRadius) || {};
visible = Object.keys(found).length > 0;
}
$: finder = quadtree()
.extent([[-1, -1], [$width + 1, $height + 1]])
.x(xGetter)
.y(yGetter)
.addAll(dataset || $data);
</script>
<style>
.bg {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
</style>
<div
class="bg"
on:mousemove="{findItem}"
on:mouseout="{() => visible = false}"
></div>
<slot
x={xGetter(found) || 0}
y={yGetter(found) || 0}
{found}
{visible}
{e}
></slot>
month,apples,bananas,cherries,dates
2015-04-01,3840,1920,960,400
2015-03-01,1600,1440,960,400
2015-02-01,640,960,640,400
2015-01-01,320,480,640,400