Search

Revealing Hidden Labels

Reducing Clutter

Hide labels by default and reveal them on demand.

Revealing Hidden Labels
View live example →

Hide labels by default to reduce clutter in your map visualizations.

Here, the labels are revealed using the NetworkNodeLabel visible API with a hover interaction.

Here, the labels are revealed using the NetworkNodeLabel visible API with an onHover interaction.

import { MapWeave } from "mapweave/mapbox";
import { NetworkLayer } from "mapweave/layers";
import { networkData } from "./data";

const mapweave = new MapWeave({ container: "mw", options: { accessToken: VITE_MAPBOX_API_KEY } });

const networkLayer = new NetworkLayer({ data: networkData });
mapweave.addLayer(networkLayer);

mapweave.fitBounds();

let lastHovered = null;
let hoverStyleReset = null;

mapweave.on("hover", ({ id }) => {
  if (id !== null) {
    if (id !== lastHovered) {
      lastHovered = id;
      hoverStyleReset?.();
      hoverStyleReset = networkLayer.overrideStyle(id, {
        scale: 1.4,
        label: { visible: true },
        border: { color: "white" },
      });
    }
  } else {
    lastHovered = null;
    hoverStyleReset?.();
  }
});
const cities = [
  ["London", 51.5072, -0.1275],
  ["Birmingham", 52.48, -1.9025],
  ["Portsmouth", 50.8058, -1.0872],
  ["Southampton", 50.9025, -1.4042],
  ["Nottingham", 52.9561, -1.1512],
  ["Bristol", 51.4536, -2.5975],
  ["Manchester", 53.479, -2.2452],
  ["Liverpool", 53.4094, -2.9785],
  ["Leicester", 52.6344, -1.1319],
  ["Worthing", 50.8147, -0.3714],
  ["Coventry", 52.4081, -1.5106],
  ["Belfast", 54.5964, -5.93],
  ["Bradford", 53.8, -1.75],
  ["Derby", 52.9247, -1.478],
  ["Plymouth", 50.3714, -4.1422],
  ["Wolverhampton", 52.5833, -2.1333],
  ["Northampton", 52.2304, -0.8938],
  ["Norwich", 52.628, 1.2928],
  ["Luton", 51.8783, -0.4147],
  ["Aberdeen", 57.15, -2.11],
  ["Croydon", 51.3727, -0.1099],
  ["Bournemouth", 50.72, -1.88],
  ["Basildon", 51.58, 0.49],
  ["Maidstone", 51.272, 0.529],
  ["Ilford", 51.557, 0.0858],
  ["Warrington", 53.39, -2.59],
  ["Oxford", 51.75, -1.25],
  ["Harrow", 51.5836, -0.3464],
  ["Gloucester", 51.8667, -2.25],
  ["York", 53.96, -1.08],
  ["Blackpool", 53.8142, -3.0503],
  ["Stockport", 53.4083, -2.1494],
  ["Glasgow", 55.862, -4.258],
  ["Edinburgh", 55.953, -3.188],
  ["Cambridge", 52.205, 0.1192],
];

function createNetworkNodes(cities) {
  const networkNodes = {};
  for (const [name, lat, lon] of cities) {
    networkNodes[name] = {
      type: "node",
      size: 5,
      latitude: lat,
      longitude: lon,
      color: "#0FAB7C",
      border: { width: 1, color: "#0FAB7C" },
      label: {
        text: name,
        position: "ne",
        backgroundColor: "rgba(31, 31, 31, 0.75)",
        color: "white",
        border: { width: 1, color: "white" },
        visible: false,
      },
    };
  }
  return networkNodes;
}

export const networkData = createNetworkNodes(cities);
<!doctype html>
<html>
  <head>
    <link rel="stylesheet" href="@ci/theme/mw/css/examples.css" />
  </head>
  <body>
    <div id="mw">
      <div class="example__infobox" style="position: absolute; margin: 5px">
        Hover a node to reveal the label.
      </div>
    </div>
    <script type="module" src="./code.js"></script>
  </body>
</html>
import React, { useState } from "react";
import { createRoot } from "react-dom/client";
import { MapWeave } from "mapweave/react/mapbox";
import { NetworkLayer } from "mapweave/react/layers";
import { networkData } from "./data";

const mapWeaveOptions = { accessToken: VITE_MAPBOX_API_KEY };

const overrideStyle = { scale: 1.4, label: { visible: true }, border: { color: "white" } };

function Demo() {
  const [hoverInfo, setHoverInfo] = useState(null);

  const overrideStyles =
    hoverInfo?.item?.type === "node" ? [{ ids: hoverInfo.id, overrideStyle }] : [];

  return (
    <>
      <div className="example__infobox" style={{ position: "absolute", margin: 5 }}>
        Hover a node to reveal the label.
      </div>
      <MapWeave options={mapWeaveOptions} onHover={setHoverInfo}>
        <NetworkLayer data={networkData} overrideStyles={overrideStyles} />
      </MapWeave>
    </>
  );
}

const root = createRoot(document.getElementById("mw"));
root.render(<Demo />);
const cities = [
  ["London", 51.5072, -0.1275],
  ["Birmingham", 52.48, -1.9025],
  ["Portsmouth", 50.8058, -1.0872],
  ["Southampton", 50.9025, -1.4042],
  ["Nottingham", 52.9561, -1.1512],
  ["Bristol", 51.4536, -2.5975],
  ["Manchester", 53.479, -2.2452],
  ["Liverpool", 53.4094, -2.9785],
  ["Leicester", 52.6344, -1.1319],
  ["Worthing", 50.8147, -0.3714],
  ["Coventry", 52.4081, -1.5106],
  ["Belfast", 54.5964, -5.93],
  ["Bradford", 53.8, -1.75],
  ["Derby", 52.9247, -1.478],
  ["Plymouth", 50.3714, -4.1422],
  ["Wolverhampton", 52.5833, -2.1333],
  ["Northampton", 52.2304, -0.8938],
  ["Norwich", 52.628, 1.2928],
  ["Luton", 51.8783, -0.4147],
  ["Aberdeen", 57.15, -2.11],
  ["Croydon", 51.3727, -0.1099],
  ["Bournemouth", 50.72, -1.88],
  ["Basildon", 51.58, 0.49],
  ["Maidstone", 51.272, 0.529],
  ["Ilford", 51.557, 0.0858],
  ["Warrington", 53.39, -2.59],
  ["Oxford", 51.75, -1.25],
  ["Harrow", 51.5836, -0.3464],
  ["Gloucester", 51.8667, -2.25],
  ["York", 53.96, -1.08],
  ["Blackpool", 53.8142, -3.0503],
  ["Stockport", 53.4083, -2.1494],
  ["Glasgow", 55.862, -4.258],
  ["Edinburgh", 55.953, -3.188],
  ["Cambridge", 52.205, 0.1192],
];

function createNetworkNodes(cities) {
  const networkNodes = {};
  for (const [name, lat, lon] of cities) {
    networkNodes[name] = {
      type: "node",
      size: 5,
      latitude: lat,
      longitude: lon,
      color: "#0FAB7C",
      border: { width: 1, color: "#0FAB7C" },
      label: {
        text: name,
        position: "ne",
        backgroundColor: "rgba(31, 31, 31, 0.75)",
        color: "white",
        border: { width: 1, color: "white" },
        visible: false,
      },
    };
  }
  return networkNodes;
}

export const networkData = createNetworkNodes(cities);
<!doctype html>
<html>
  <head>
    <link rel="stylesheet" href="@ci/theme/mw/css/examples.css" />
  </head>
  <body>
    <div id="mw"></div>
    <script type="module" src="./code.jsx"></script>
  </body>
</html>

Terms of use

These terms do not alter or supersede any existing agreements between you (or your employer) and us.

By accessing or using any Content you agree to be bound by these Terms of Use. Please review these terms carefully before using the website.

The contents of this website, including but not limited to any text, code samples, API references, schemas, interactive tools, and other materials (collectively, the 'Content'), are made available for informational and internal evaluation purposes only. All intellectual property rights in the Content are reserved. No licence is granted to use the Content for any commercial purpose, or to copy, distribute, modify, reverse-engineer, or incorporate any part of the Content into any product or service, without our prior written consent.

This Content is provided “as is” and “as available,” without any representations, warranties, or guarantees of any kind, whether express or implied, including but not limited to implied warranties of merchantability, fitness for a particular purpose, non-infringement, or accuracy. To the fullest extent permitted by applicable law, we expressly exclude and disclaim all implied warranties, conditions, and other terms that might otherwise be implied.

We disclaim all liability for any loss or damage, whether direct, indirect, incidental, consequential, or otherwise, arising from any reliance placed on the Content or from your use of it, to the fullest extent permitted by applicable law. By continuing to access or use the Content, you acknowledge and agree to these terms.