Map (svg)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.

To render server-side, this example uses the fixedAspectRatio property, which the user sets ahead of time.

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

  // For a map example with a tooltip, check out

  import MapSvg from './_components/Map.svg.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
   * `dataJoinKey` is the name of the field in the data
   * `mapJoinKey` is the name of the field in the map file
  const dataJoinKey = 'name';
  const mapJoinKey = 'name';
  const dataLookup = new Map();

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

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

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

    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.
    The height is being set inline with `padding-bottom` using the aspect ratio trick below.
  .chart-container {
    position: relative;
    width: 100%;

<div class="chart-container" style="padding-bottom:{100 / aspectRatio}%">
    z={d => dataLookup.get(d[mapJoinKey])}