1.3.9 • Published 2 years ago

mapboxgl-legend-nhvr v1.3.9

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

mapboxgl-legend

NPM NPM NPM

Add a legend to a mapbox-gl map by parsing layer layout & paint expressions.

image

Properties currently supported:

  • fill-color
  • circle-color
  • circle-radius
  • icon-image

Expressions currently supported:

  • match
  • interpolate
  • step
  • literals

Get started

Install npm package

npm install mapboxgl-legend

Add legend control to map

import LegendControl from 'mapboxgl-legend';

const legend = new LegendControl();
map.addControl(legend, 'bottom-left');

Add layers as usual, and the legend will be autogenerated

map.addLayer({
	id: 'density',
	type: 'circle',
	source: 'demographic',
	paint: {
		'circle-radius': [
			'interpolate', ['linear'],
			['to-number', ['get', 'density']],
			100, 10,
			200, 17,
			500, 30,
			1000, 50,
			2000, 75
		],
		'circle-color': [
			'match',
			['get', 'ethnicity'],
			'White', '#f1e8c8',
			'Black', '#443722',
			'Hispanic', '#aa9761',
			'Asian', '#e8e59d',
			/* other */ '#ccc'
		]
	},
});

Options

A few options can be passed on legend initialization.

optiontypedefaultdescription
collapsedBooleanfalseSet legend panels collapsed on load
togglerBooleanfalseAdd button to show and hide layers
layersArrayregexundefinedList of layers to be added. If undefined all layers will be added

There are also a few options that be defined as a per-layer basis using the style metadata object.

optiontypedescription
nameStringSet the panel title name
unitStringAdd a unit to all labels
labelsObjectMap a value to a text that replaces it as a label
map.addLayer({
	id: 'density',
	type: 'circle',
	source: 'demographic',
	paint: { /* ... */ },
	metadata: {
		name: 'Population Density',
		unit: `k/km²`
	}
});

Styles

Legend defaults to a simple design inspired by standard mapbox-gl controls, but can be tunned by changing CSS variables. Check default values in /src/_variables.scss

1.3.9

2 years ago

1.3.8

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago