Git Product home page Git Product logo

leaflet-choropleth's Introduction

Leaflet Choropleth Build Status js-standard-style

NPM

Choropleth plugin for Leaflet (color scale based on value) - Demo

screenshot

This plugin extends L.geoJson, giving each feature a style.fillColor that corresponds to a specified value in its properties object. For information on how to use L.geoJson, see the Leaflet tutorial and documentation.

While Leaflet provides a choropleth tutorial, that approach requires you to specify exact breakpoints and colors. This plugin uses chroma.js to abstract that for you. Just tell it which property in the GeoJSON to use and some idea of the color scale you want.

Usage

L.choropleth(geojsonData, {
	valueProperty: 'incidents', // which property in the features to use
	scale: ['white', 'red'], // chroma.js scale - include as many as you like
	steps: 5, // number of breaks or steps in range
	mode: 'q', // q for quantile, e for equidistant, k for k-means
	style: {
		color: '#fff', // border color
		weight: 2,
		fillOpacity: 0.8
	},
	onEachFeature: function(feature, layer) {
		layer.bindPopup(feature.properties.value)
	}
}).addTo(map)

Advanced

  • colors: If you prefer to specify your own exact colors, use colors: ['#fff', '#777', ...] instead of scale. Just make sure the number of colors is the same as the number of steps specified.
  • valueProperty: To use computed values (such as standardizing), you can use a function for valueProperty that is passed (feature) and returns a number (example).

Installation

  • via NPM: npm install leaflet-choropleth
  • via Bower: bower install leaflet-choropleth

Include dist/choropleth.js from this repository on your page after Leaflet:

<script src="path/to/leaflet.js"></script>
<script src="path/to/choropleth.js"></script>

Or, if using via CommonJS (Browserify, Webpack, etc.):

var L = require('leaflet')
require('leaflet-choropleth')

Examples

Development

This project uses webpack to build the JavaScript and standard for code style linting.

  • While developing, use npm run watch to automatically rebuild when files are saved
  • Use npm test to run unit tests and code style linter
  • Before committing dist/, use npm run build to optimize and minify for production use

leaflet-choropleth's People

Contributors

abrin avatar boris-arkenaar avatar ericpalakovichcarr avatar kernc avatar kirkdotcam avatar timwis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leaflet-choropleth's Issues

Set Scale

Is it possible to set the scale/breaks manually? something like scale: ["50", "80", "100"]?

Change style dynamically

Feature request: is it possible to change the style after creating a layer? I'd like to show a map and then have a drop down of feature names, and then update the layer to colour by that feature, possibly with a different colour scale.

At present I don't think it can be easily done because the colour mapping info is stored in opts which has the scope of the L.choropleth constructor - changing mylayer.options and forcing a style update with mylayer.setStyle(mylayer.options.style) doesn't work. I can't actually see how a style function can get anything from the layer options, but my JS skills are poor, especially understanding scope in JS...

Quantile

They way quantile should work is like this: An equal number if items should be assigned to each bucket.
However, when I use the q in the PlugIn, not the same number of items are assigned to each class. Sometimes, there six or seven items per class while in others there are only two or three. But the classes should have the same size?!

Rewrote the plugin

I'm not creating a PR but I would like you to take a look at my fork

https://github.com/bhaskarvk/leaflet-choropleth

Here's what I've done to it

  • Added support for more chroma-js options like bezier interpolate, fix lightness, channel mode
  • Rewrote it so that it can work along with omnivore. For now though it requires my fork of omnivore. This allows the plugin to be used not only for GeoJSON but every file that omnivore supports so TopoJSON/KML/GFX/CSV/WKT
  • Added ability to customize and add a legend automatically if so desired.

Please let me know what you think, and whether you would be interested in merging this.

Change legend scale without minima/maxima?

Hi, is it possible to modify the legend for a density map? For example, if I select colors White, Red, steps 5, and have a feature value between 0 to 100,000, the map and legend is created with 0 as White and 100,000 as red. But, I rather want it to categorize based on ranges of values. For example. <20,000, 20,000-70,000, >100,000. How is it possible to modify the legend this way?

Possible to utilise chroma's domain setting?

Firstly, I love your choropleth plugin. It's working great, except the color scale setting isn't quite precise enough for what I need.

I've tried using chroma's domain setting (https://gka.github.io/chroma.js/#scale-domain) to be more specific about how it sets colors, but it doesn't seem to pass through your plugin.

Is it possible to enhance the plugin to utilize this setting?

Thanks in advance

style bug

in line 61 you have: return _.extend(userStyle(), style) . Must be: return _.extend(userStyle(feature), style)

Drop bower support and remove compiled code from source control

Including dist makes it inconvenient to submit pull requests -- the submitter isn't sure whether to include it in the PR, and the author has no insight into whether the new compiled file accurately reflects the latest source code. I think instead we'd run the build command during npm publish. The gh-pages branch also needs it for the demos, so we could use travis to compile and push from the master branch to gh-pages.

This would probably mean bower no longer works, though, as there's no compiled version sitting on github. Do people still use bower? Any other advantages to having the built file in source control?

var L = require('leaflet') is not defined

Trying to use the leaflet plugin but I get errors at" choropleth.js:1" Uncaught ReferenceError: require choropleth.js:1 is not defined. Error shows up here "var L = require('leaflet')".

I have no clue why.
I am using chrome with the developer tools
printscreen 2018-01-01 kl 22 01 08

Thanks

Got Legends?

How can I leverage the package to generate legends for my map?

allow users to specify a custom color scale algorithm

this is a useful plugin, but there are definitely times where a user may need to have their own custom color-scale algorithm separate from chroma.js. The linked pull request allows a user to specify a color function: ( #9 ).

change valueProperty dynamically

Hi, I was wondering if it was possible to define the valueProperty of a layer dynamically.

Let's say that i have a geojson with different properties, is it possible to set the valueProperty to a variable that changes according to what the user select using an input box or slider?

at the moment i am recreating the whole layer every time.

thanks!

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.