teralytics / Leaflet.D3SvgOverlay

Leaflet Plugin: D3 SVG Overlay
MIT License
184 stars 45 forks source link

Leaflet.D3SvgOverlay

An overlay class for Leaflet, a JS library for interactive maps. Allows drawing overlay using SVG with the help of D3, a JavaScript library for manipulating documents based on data.

Features

Compatible with Leaflet 0.7.x / 1.0.x

Demo

Basic usage

Include the dependency libraries:

<link href='https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.5/leaflet.css'
           rel='stylesheet' type='text/css'/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.5/leaflet-src.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.9/d3.min.js"></script>

Include the D3SvgOverlay library:

<script src="https://github.com/teralytics/Leaflet.D3SvgOverlay/raw/master/L.D3SvgOverlay.min.js"></script>

Create a map:

var map = L.map(...);

Create an overlay:

var d3Overlay = L.d3SvgOverlay(function(selection, projection){

    var updateSelection = selection.selectAll('circle').data(dataset);
    updateSelection.enter()
        .append('circle')
        ...
        .attr("cx", function(d) { return projection.latLngToLayerPoint(d.latLng).x })
        .attr("cy", function(d) { return projection.latLngToLayerPoint(d.latLng).y });

});

Add it to the map:

d3Overlay.addTo(map);

Note: within the drawing callback function you can and should use the normal D3 workflow with update, .enter() and .exit() selections.

API

Factory method

L.d3SvgOverlay(<function> drawCallback, <options> options?)

Drawing callback function

drawCallback(selection, projection)

Overlay options object

available fields:

Projection object

available methods/fields:

License

This code is provided under the MIT License (MIT).

Brought to you by Teralytics AG

Interested in data analysis, big data, mapping and visualizations? Have experience in running big infrastructure? We're hiring!

Find how to apply at http://teralytics.net/