Menu

Stacked areaEdit

Jan. 11
Jan. 23
Feb. 3
Feb. 15
Feb. 26
Mar. 10
Mar. 22
0
2k
4k
6k

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.

<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';

  // This example loads csv data as json using @rollup/plugin-dsv
  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];
    });
  });

  /* --------------------------------------------
   * Create a stacked data structure
   */
  const stackData = stack().keys(seriesNames);

  const series = stackData(data);

  const formatLabelX = timeFormat('%b. %-d');
  const formatLabelY = d => format(`~s`)(d);
</script>

<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>

<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>