Map (svg, tooltip)Edit

A map component using d3-geo for the projection. Adapted from d3-selection, the SVG map component uses Layer Cake's built-in raise function to ensure the hovered-over SVG element is always on top of its siblings. The tooltip uses Svelte's createEventDispatcher.

  import { LayerCake, Svg, Html } from 'layercake';
  import { feature } from 'topojson-client';
  import { geoAlbersUsa } from 'd3-geo';
  import { scaleQuantize } from 'd3-scale';
  import { format } from 'd3-format';

  import MapSvg from './components/Map.svg.svelte';
  import Tooltip from './components/Tooltip.svelte';

  // This example loads json data as json using @rollup/plugin-json
  import usStates from './data/us-states.topojson.json';
  import stateData from './data/us-states-data.json';

  const colorKey = 'myValue';
  /* --------------------------------------------
   * Create lookups to more easily join our data
  const joinKey = 'name';
  const dataLookup = new Map();

  const geojson = feature(usStates, usStates.objects.collection);
  const projection = geoAlbersUsa();

  stateData.forEach(d => {
    dataLookup.set(d[joinKey], d);

  geojson.features.forEach(d => {
    // This will overwrite any existing keys on
    // so watch out for any name collision
    Object.assign(, dataLookup.get([joinKey]));

  let evt;
  let hideTooltip = true;

  // Create a flat array of objects that LayerCake can use to measure
  // extents for the color scale
  const flatData = =>;
  const colors = ['#ffdecc', '#ffc09c', '#ffa06b', '#ff7a33'];

  const addCommas = format(',');

    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: 100%;

<div class="chart-container">
        on:mousemove={event => evt = hideTooltip= event}
        on:mouseout={() => hideTooltip = true}

      {#if hideTooltip !== true}
          {#each Object.entries(detail.props) as [key, value]}
            <div class="row"><span>{key}:</span> {typeof value === 'number' ? addCommas(value) : value}</div>