Archived

Documentation for Layer Cake v10, before Svelte 5 runes. Current docs at layercake.graphics

Menu

MapPoints.html.svelte component

Generates HTML dots onto a map using d3-geo.

Param Default Required Description
projection Function None
yes
A D3 projection function. Pass this in as an uncalled function, e.g. projection={geoAlbersUsa}.
r number 3.5
no
The point's radius.
fill string 'yellow'
no
The point's fill color.
stroke string '#000'
no
The point's stroke color.
strokeWidth number 1
no
The point's stroke width in pixels.
opacity number 1
no
The point's opacity.
features (Array<Object> | undefined) None
no
A list of GeoJSON features to plot. If unset, the plotted features will default to those in k.data.features, assuming this field is a list of GeoJSON features.
<!--
  @component
  Generates HTML dots onto a map using [d3-geo](https://github.com/d3/d3-geo).
 -->
<script>
  import { getLayerCakeContext } from 'layercake';

  /** @type {import('layercake').LayerCakeContext<any, { features: Array<any> }>} */
  const k = getLayerCakeContext();

  /**
   * @typedef {Object} Props
   * @property {Function} projection - A D3 projection function. Pass this in as an uncalled function, e.g. `projection={geoAlbersUsa}`.
   * @property {number} [r=3.5] - The point's radius.
   * @property {string} [fill='yellow'] - The point's fill color.
   * @property {string} [stroke='#000'] - The point's stroke color.
   * @property {number} [strokeWidth=1] - The point's stroke width in pixels.
   * @property {number} [opacity=1] - The point's opacity.
   * @property {Array<Object>|undefined} [features] - A list of GeoJSON features to plot. If unset, the plotted features will default to those in `k.data.features`, assuming this field is a list of GeoJSON features.
   */

  /** @type {Props} */
  let {
    projection,
    r = 3.5,
    fill = 'yellow',
    stroke = '#000',
    strokeWidth = 1,
    opacity = 1,
    features
  } = $props();

  let projectionFn = $derived(projection().fitSize([k.width, k.height], k.data));
</script>

<div class="points">
  {#each features || k.data.features as d}
    <!-- To size each circle from an r scale, set width and height from k.rGet(d.properties) -->
    <div
      class="point"
      style="
      top: {projectionFn(d.geometry.coordinates)[1]}px;
      left: {projectionFn(d.geometry.coordinates)[0]}px;
      width: {r * 2}px;
      height: {r * 2}px;
      border-width: {strokeWidth}px;
      border-color: {stroke};
      background-color: {fill};
      opacity: {opacity};
    "
    ></div>
  {/each}
</div>

<style>
  .point {
    position: absolute;
    border-radius: 50%;
    border-style: solid;
  }
</style>