Git Product home page Git Product logo

use-supercluster's Introduction

useSupercluster

A hook for using Supercluster with React.

const { clusters, supercluster } = useSupercluster({
  points: [],
  bounds: [
    -1.2411810957931664,
    52.61208435908725,
    -1.0083656811012531,
    52.64495957533833,
  ],
  zoom: 12,
  options: { radius: 75, maxZoom: 20 },
});

Installation

You will need to install supercluster as a peer dependency of this package.

yarn add supercluster use-supercluster

Examples

This package contains an example along with tests, but full examples with instructions in the most popular mapping libraries for React can be found below.

Mapbox

Full instructions and an example can be found here.

Google Maps

Full instructions and an example can be found here.

Leaflet

Full instructions and an example can be found here.

Configuration

The last (fourth) argument passed to the useSupercluster hook are options that are passed directly to the instance of Supercluster. You can use any of Supercluster's options.

Map & Reduce Options

As an example, you can use map and reduce to keep track of a total value summed up from across the points. In this case we have the total cost, the max severity, plus another count (which is redundant because Supercluster gives us the point_count property).

const options = {
  radius: 75,
  maxZoom: 20,
  map: (props) => ({
    cost: props.cost,
    severity: props.severity,
    count: 1,
  }),
  reduce: (acc, props) => {
    acc.count += 1;
    acc.cost += props.cost;
    acc.severity = Math.max(acc.severity, props.severity);
    return acc;
  },
};

I found map and reduce a little confusing! The value returned from map of the first point is used as the initial value passed as the accumulator to the reduce function. The only props you have available in reduce are the ones returned from map. You technically don't need to return a value from reduce (it's not used), but instead need to mutate the accumulator object.

Then these accumulated properties can be used and are available on each cluster:

<ul>
  {clusters.map((point) => {
    const properties = point.properties || {};
    if (properties.cluster) {
      return (
        <li key={point.id}>
          <h2>Points: {properties.point_count}</h2>
          <p>Cost: {properties.cost.toFixed(2)}</p>
          <p>Severity: {properties.severity}</p>
          <p>Count: {properties.count}</p>
        </li>
      );
    } else {
      return <li key={properties.crimeId}>{properties.category}</li>;
    }
  })}
</ul>

use-supercluster's People

Contributors

dependabot[bot] avatar leighhalliday avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.