npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mapbox-choropleth

v0.16.0

Published

Generate choropleths in Mapbox-GL-JS maps from a CSV file and a boundary geometry source.

Downloads

35

Readme

Mapbox-Choropleth

Creates choropleth layers for Mapbox-GL-JS maps. A choropleth (sometimes called a heatmap) is a type of data visualisation where polygons (typically administrative boundaries such as states) are coloured to represent a numeric quantity (such as population).

This library combines a CSV file and a boundary source (GeoJSON or Mapbox vector tiles) to create a fill layer in your Mapbox-GL-JS map.

It uses:

  • D3-csv to fetch and parse the CSV file
  • SimpleStatistics.ckmean to generate colour "bins"
  • Chroma.JS to assign a colour palette

Usage with NPM

Install:

npm install mapbox-choropleth
const MapboxChoropleth = require('mapbox-choropleth');

let c = new MapboxChoropleth({ options }).addTo(map);

Usage in <script> tag

<script src="https://unpkg.com/mapbox-choropleth"></script>
...
<script>
    let c = new MapboxChoropleth({ options }).addTo(map);
    ...
</script>

Required options

    // ONE of tableUrl OR tableRows is required.
    // URL of a CSV file that contains your table data.
    tableUrl:   'http://example.com/table.csv',

    // CSV rows in the format D3 produces.
    //tableRows: [{ id: 'VIC', pop: 5000000}, { id: 'SA', pop: 3000000 }, ...],

    // Name of the column in the CSV file that contains the numeric quantity to be visualised.
    // Values will be coerced to numbers, so may contain numeric strings ("3.5").
    tableNumericField: 'pop',

    // Name of the column in the CSV file that contains boundary identifiers
    tableIdField: 'id',

    // ONE of geometryUrl OR geometryTiles is required.
    // Either a mapbox:// vector tile URL, or a URL of a GeoJSON file containing the boundary geometry.
    geometryUrl: 'https://example.com/state-boundaries.geojson',

    // Array of vector tile endpoints, if you have non-mapbox-hosted vector tiles.
    //geometryTiles: [ 'https://example.com/tiles/states/{z}/{x}/{y}.pbf' ],

    // Name of the attribute in the CSV file that contains the same boundary identifiers as tableIdField
    geometryIdField: 'state_id',

    // alternatively, you can use each feature ID to match on. Only works for numeric fields.
    // useFeatureId: true

    // We can use `setFeatureState` to set values rather than a long style. Requires `useFeatureId`
    // useFeatureState: true

    // If using a vector tile source, the source layer to use.
    //sourceLayer: 'states'

Optional options


    // Number of distinct colour bins to use.
    binCount: 7

    // Any color scheme identifier accepted by [chroma.scale()](https://gka.github.io/chroma.js/#chroma-scale), including
    // [Color Brewer](http://colorbrewer2.org/) names ("BuGn", "Spectral") and arrays (['blue', 'white', 'red']).
    colorScheme: 'BuGn',

    // A DOM element or selector ("#legend") which will be populated with a legend.
    legendElement: '#legend',

    // Object, Mapbox style "paint" options (for "fill" layer)
    paint: { 'fill-opacity': 0.5 },

    // String to use as the Mapbox source ID, instead of 'choropleth'
    sourceId: 'mychoropleth',

    // String to use as the Mapbox layer ID, instead of 'choropleth'
    layerId: 'mychoropleth',

    // Insert the choropleth layer before this layer ID
    before: 'labels',

    // Function to format bin values for legend
    numberFormatFunc: x => x.toFixed(2),

    // additional source properties to set, as defined in the Mapbox source
    source: {
        maxzoom: 13
    },

    immediate: true, // Adds layer immediately, rather than trying to wait till the map is ready. Defaults to false.

    // Cast values in CSV to string or number to make them
    // geometryIdFieldType: String, // or Number, or undefined.

Example

const MapboxChoropleth = require('mapbox-choropleth');
let c = new MapboxChoropleth({
    tableUrl: 'https://gist.githubusercontent.com/stevage/088fd8edab66157e1a307f521e38ecca/raw/46d01d54a7d95cac1ad88347aa910b5de3946b3e/elb.csv',
    tableNumericField: 'Australian Labor Party Percentage',
    tableIdField: 'DivisionNm',
    geometryUrl: 'mapbox://stevage.7ux6xzbz',
    geometryIdField: 'ELECT_DIV',
    sourceLayer: 'ELB',
    binCount: 20,
    colorScheme: 'Spectral',
    legendElement: '#legend'
 }).addTo(map);

Using the legend

The generated legend is contains fairly basic styling. CSS classes are added before any other styles, to make them easy to override. You may want to add styles for these classes:

  • .choropleth-legend: the whole legend container (white, with a grey border)
  • .choropleth-legend-box: a color box (square, colored appropriately, no border)
  • .choropleth-legend-label: the caption of each color (sans-serif)

You'll need to add your own positioning styles for the legend element itself. For instance:

#legend {
    position: absolute;
    top: 1em;
    left: 1em;
}

You can obtain the legend HTML directly by calling choropleth.getLegend().

Credits

Author: Steve Bennett