<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';
import AxisX from './_components/AxisX.percent-range.html.svelte';
import AxisY from './_components/AxisY.percent-range.html.svelte';
import AreaStacked from './_components/AreaStacked.svelte';
import 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];
seriesNames.forEach(name => {
d[name] = +d[name];
});
});
const stackData = stack()
.keys(seriesNames);
const series = stackData(data);
const formatLabelX = timeFormat('%b. %-d')
const formatLabelY = d => format(`~s`)(d);
</script>
<style>
.chart-container {
width: 100%;
height: 250px;
}
</style>
<div class="chart-container">
<LayerCake
ssr
percentRange
padding={{ 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>
<AxisX
format={formatLabelX}
tickMarks
/>
<AxisY
format={formatLabelY}
/>
</Html>
<ScaledSvg>
<AreaStacked/>
</ScaledSvg>
</LayerCake>
</div>
<script>
import { getContext } from 'svelte';
const { width, height, xScale, yRange } = getContext('LayerCake');
export let tickMarks = false;
export let gridlines = true;
export let tickMarkLength = 6;
export let baseline = false;
export let snapLabels = false;
export let format = d => d;
export let ticks = undefined;
export let tickGutter = 0;
export let dx = 0;
export let dy = 1;
$: tickLen = tickMarks === true
? tickMarkLength ?? 6
: 0;
$: isBandwidth = typeof $xScale.bandwidth === 'function';
$: tickVals = Array.isArray(ticks) ? ticks :
isBandwidth ?
$xScale.domain() :
typeof ticks === 'function' ?
ticks($xScale.ticks()) :
$xScale.ticks(ticks);
$: halfBand = isBandwidth ? $xScale.bandwidth() / 2 : 0
</script>
<div class='axis x-axis' class:snapLabels>
{#each tickVals as tick, i (tick)}
{@const tickValPx = $xScale(tick)}
{#if baseline === true}
<div class="baseline" style='top:100%; width:100%;'></div>
{/if}
{#if gridlines === true}
<div
class="gridline"
style:left='{tickValPx}%'
style='top:0; bottom:0;'></div>
{/if}
{#if tickMarks === true}
<div
class="tick-mark"
style:left='{tickValPx + halfBand}%'
style:height='{tickLen}px'
style:bottom='{-tickLen - tickGutter}px'
></div>
{/if}
<div
class='tick tick-{i}'
style:left='{tickValPx + halfBand}%'
style='top:calc(100% + {tickGutter}px);'>
<div
class="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%);
}
.axis.snapLabels .tick:last-child {
transform: translateX(-40%);
}
.axis.snapLabels .tick.tick-0 {
transform: translateX(40%);
}
</style>
<script>
import { getContext } from 'svelte';
const { xRange, yScale } = getContext('LayerCake');
export let tickMarks = false;
export let labelPosition = 'even';
export let snapBaselineLabel = false;
export let gridlines = true;
export let tickMarkLength = undefined;
export let format = d => d ;
export let ticks = 4;
export let tickGutter = 0;
export let dx = 0;
export let dy = -3;
export let charPixelWidth = 7.25;
$: isBandwidth = typeof $yScale.bandwidth === 'function';
$: tickVals = Array.isArray(ticks) ? ticks :
isBandwidth ?
$yScale.domain() :
typeof ticks === 'function' ?
ticks($yScale.ticks()) :
$yScale.ticks(ticks);
function calcStringLength(sum, val) {
if (val === ',' || val === '.') return sum + charPixelWidth * 0.5;
return sum + charPixelWidth;
}
$: tickLen = tickMarks === true
? labelPosition === 'above'
? tickMarkLength ?? widestTickLen
: tickMarkLength ?? 6
: 0;
$: widestTickLen = Math.max(10, Math.max(...tickVals.map(d => format(d).toString().split('').reduce(calcStringLength, 0))));
$: x1 = -tickGutter - (labelPosition === 'above' ? widestTickLen : tickLen);
$: halfBand = isBandwidth ? $yScale.bandwidth() / 2 : 0;
$: maxTickValPerc = Math.max(...tickVals.map($yScale));
</script>
<div class='axis y-axis'>
{#each tickVals as tick, i (tick)}
{@const tickValPerc = $yScale(tick)}
<div class='tick tick-{i}' style='left:{$xRange[0]}%;top:{tickValPerc + halfBand}%;'>
{#if gridlines === true}
<div
class="gridline"
style="top:0;"
style:left='{x1}px'
style:right='0px'
></div>
{/if}
{#if tickMarks === true}
<div
class="tick-mark"
style:top='0'
style:left='{x1}px'
style:width='{tickLen}px'
></div>
{/if}
<div
class="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 && tickValPerc === maxTickValPerc) ? -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>
<script>
import { getContext } from 'svelte';
import { area } from 'd3-shape';
const { data, xGet, yScale, zGet } = getContext('LayerCake');
$: areaGen = area()
.x(d => $xGet(d))
.y0(d => $yScale(d[0]))
.y1(d => $yScale(d[1]));
</script>
<g class="area-group">
{#each $data as d}
<path
class='path-area'
d='{areaGen(d)}'
fill='{$zGet(d)}'
></path>
{/each}
</g>
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