<script>
import { LayerCake, Svg, Html, groupLonger, flatten } 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/GroupLabels.html.svelte';
import SharedTooltip from './_components/SharedTooltip.html.svelte';
import data from './_data/fruit.csv';
const xKey = 'month';
const yKey = 'value';
const zKey = 'fruit';
const xKeyCast = timeParse('%Y-%m-%d');
const seriesNames = Object.keys(data[0]).filter(d => d !== xKey);
const seriesColors = ['#ffe4b8', '#ffb3c0', '#ff7ac7', '#ff00cc'];
data.forEach(d => {
d[xKey] = typeof d[xKey] === 'string'
? xKeyCast(d[xKey])
: d[xKey];
seriesNames.forEach(name => {
d[name] = +d[name];
});
});
const formatTickX = timeFormat('%b. %e');
const formatTickY = d => format(`.${precisionFixed(d)}s`)(d);
const groupedData = groupLonger(data, seriesNames, {
groupTo: zKey,
valueTo: yKey
});
</script>
<style>
.chart-container {
width: 100%;
height: 250px;
}
</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()}
zRange={seriesColors}
flatData={flatten(groupedData, 'values')}
data={groupedData}
>
<Svg>
<AxisX
gridlines={false}
ticks={data.map(d => d[xKey]).sort((a, b) => a - b)}
formatTick={formatTickX}
snapTicks={true}
tickMarks={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 } = 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, yRange } = getContext('LayerCake');
export let gridlines = true;
export let tickMarks = false;
export let baseline = false;
export let snapTicks = false;
export let formatTick = d => d;
export let ticks = undefined;
export let xTick = 0;
export let yTick = 16;
$: 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" class:snapTicks>
{#each tickVals as tick, i (tick)}
<g class="tick tick-{i}" transform="translate({$xScale(tick)},{Math.max(...$yRange)})">
{#if gridlines !== false}
<line class="gridline" y1={$height * -1} y2="0" x1="0" x2="0" />
{/if}
{#if tickMarks === true}
<line
class="tick-mark"
y1={0}
y2={6}
x1={isBandwidth ? $xScale.bandwidth() / 2 : 0}
x2={isBandwidth ? $xScale.bandwidth() / 2 : 0}
/>
{/if}
<text
x={isBandwidth ? ($xScale.bandwidth() / 2 + xTick) : xTick}
y={yTick}
dx=""
dy=""
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} />
{/if}
</g>
<style>
.tick {
font-size: 0.725em;
font-weight: 200;
}
line,
.tick line {
stroke: #aaa;
stroke-dasharray: 2;
}
.tick text {
fill: #666;
}
.tick .tick-mark,
.baseline {
stroke-dasharray: 0;
}
.axis.snapTicks .tick:last-child text {
transform: translateX(3px);
}
.axis.snapTicks .tick.tick-0 text {
transform: translateX(-3px);
}
</style>
<script>
import { getContext } from 'svelte';
const { padding, xRange, yScale } = getContext('LayerCake');
export let gridlines = true;
export let tickMarks = false;
export let formatTick = d => d;
export let ticks = 4;
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 (tick)}
<g class='tick tick-{tick}' transform='translate({$xRange[0] + (isBandwidth ? $padding.left : 0)}, {$yScale(tick)})'>
{#if gridlines !== false}
<line
class="gridline"
x2='100%'
y1={(isBandwidth ? ($yScale.bandwidth() / 2) : 0)}
y2={(isBandwidth ? ($yScale.bandwidth() / 2) : 0)}
></line>
{/if}
{#if tickMarks === true}
<line
class='tick-mark'
x1='0'
x2='{isBandwidth ? -6 : 6}'
y1={(isBandwidth ? ($yScale.bandwidth() / 2) : 0)}
y2={(isBandwidth ? ($yScale.bandwidth() / 2) : 0)}
></line>
{/if}
<text
x='{xTick}'
y='{(isBandwidth ? ($yScale.bandwidth() / 2) + yTick : yTick)}'
dx='{isBandwidth ? -9 : 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;
}
.tick .gridline {
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, z } = 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($z(group))}</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.html.svelte';
const { data, width, yScale, config } = getContext('LayerCake');
const commas = format(',');
const titleCase = d => d.replace(/^\w/, w => w.toUpperCase());
export let formatTitle = d => d;
export let formatValue = d => isNaN(+d) ? d : commas(d);
export let formatKey = d => titleCase(d);
export let offset = -20;
export let dataset = undefined;
const w = 150;
const w2 = w / 2;
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
>
{@const foundSorted = sortResult(found)}
{#if visible === true}
<div
style="left:{x}px;"
class="line"></div>
<div
class="tooltip"
style="
width:{w}px;
display: { visible ? 'block' : 'none' };
top:{$yScale(foundSorted[0].value) + offset}px;
left:{Math.min(Math.max(w2, x), $width - w2)}px;"
>
<div class="title">{formatTitle(found[$config.x])}</div>
{#each foundSorted 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 x = 'x';
export let y = 'y';
export let searchRadius = undefined;
export let dataset = 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}"
on:blur="{() => 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