1.1.0 • Published 6 years ago

ymaps-polygonmap v1.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

Yandex Maps API Polygonmap Module

Yandex.Maps API module for data visualization.

Polygonmap is a graphical representation of some spatial data, where depending on the number of entered points polygons are painted in different colors. Polygonmap class allows to construct and display such representations over geographical maps.

Loading

  1. Put module source code (polygonmap.min.js) on your CDN.

  2. Load both Yandex Maps JS API 2.1 and module source code by adding following code into <head> section of your page:

    <script src="http://api-maps.yandex.ru/2.1/?lang=ru_RU" type="text/javascript"></script>
    <!-- Change my.cdn.tld to your CDN host name -->
    <script src="http://my.cdn.tld/polygonmap.min.js" type="text/javascript"></script>

    If you use GeoJSON data:

    <script src="http://api-maps.yandex.ru/2.1/?lang=ru_RU&coordOrder=longlat" type="text/javascript"></script>
    <!-- Change my.cdn.tld to your CDN host name -->
    <script src="http://my.cdn.tld/polygonmap.min.js" type="text/javascript"></script>

    If you use npm:

    <script src="http://api-maps.yandex.ru/2.1/?lang=ru_RU" type="text/javascript"></script>
    npm i --save ymaps-polygonmap
    require('ymaps-polygonmap');
    
    // Or with babel
    import 'ymaps-polygonmap';
  3. Get access to module functions by using ymaps.modules.require method:

    ymaps.modules.require(['Polygonmap'], function (Polygonmap) {
         var polygonmap = new Polygonmap();
    });

Polygonmap

Polygonmap module.

Requires: module:option.Manager, module:ObjectManager

Polygonmap ⏏

Kind: Exported class

new Polygonmap(data, options)

ParamTypeDefaultDescription
dataObjectPolygons and points.
data.polygonsObjectGeoJSON FeatureCollections.
data.pointsObjectGeoJSON FeatureCollections.
optionsObjectOptions for customization.
options.mapperfunctiondefaultMapperFunction of iterative transformation of features.
options.fillBystring"points"Calculate the color by pointsweight.
options.fillByWeightPropstring"weight"Prop name in data object, for weight value. If fillBy is "weight".
options.fillByWeightTypestring"middle"Type of calculate color by weight. Can be middlemaximum
options.colorRangesnumber | array4Count of ranges or array of custom ranges.
options.colorSchemestring | array"'#e66a54', '#ce4356', '#ab2960', '#571756'"Preset for colorize or array of custom colors.
options.fillOpacitynumber0.8Opacity of polygon.
options.fillColorEmptyPolygonstring"#f4f6f8"Color of polygon where points count equal 0.
options.strokeColorstring"#fff"Color of polygon stroke.
options.strokeWidthnumber1Width of polygon stroke.
options.showLegendbooleantrueFlag to show color legend.
options.legendTemplatefunctioncolorLegend.defaultTemplateReceives object {color: value} returns html legend template.
options.legendPositionobjecttop: 10, right: 10Position of legend, you can only change the top or bottom and right or left.
options.filterfunctionFunction for custom filter polygons with points.
options.filterEmptyPolygonsbooleanfalseFlag for show polygon with count of points equal 0.
options.onMouseEnterfunctiondefaultOnMouseEnterHandler for mouseEnter event.
options.onMouseLeavefunctiondefaultOnMouseLeaveHandler for mouseLeave event.
options.onClickfunctiondefaultOnClickHandler for click event.
options.balloonContentfunctiondefaultBalloonContentFunction for render content of baloon. Recieves object with properties of polygon.
options.fillColorHoverstringColor of polygon on polygon hover.
options.fillOpacityHovernumber0.9Number of opacity on polygon hover.
options.strokeColorHoverstringColor of polygon stroke on polygon hover.
options.strokeWidthHovernumber2Number of stroke width on polygon hover.
options.fillColorActivestringColor of polygon on polygon active.
options.fillOpacityActivenumber0.9Number of opacity on polygon active.
options.strokeColorActivestringColor of polygon stroke on polygon active.
options.strokeWidthActivenumber2Number of stroke width on polygon active.
options.interactivitybooleantrueFlag for enable interactivity.

polygonmap.getData() ⇒ Object

Get the data, polygons and points.

Kind: instance method of Polygonmap
Returns: Object - Polygons and points.
Access: public

polygonmap.setData(data) ⇒ Polygonmap

Set the data, polygons and points.

Kind: instance method of Polygonmap
Returns: Polygonmap - Self-reference.
Access: public

ParamTypeDescription
dataObjectPolygons and points.
data.polygonsObjectGeoJSON FeatureCollections.
data.pointsObjectGeoJSON FeatureCollections.

polygonmap.getMap() ⇒ Map

Get the Map instance.

Kind: instance method of Polygonmap
Returns: Map - Reference to Map instance.
Access: public

polygonmap.setMap(map) ⇒ Polygonmap

Set Map instance to render Polygonmap object.

Kind: instance method of Polygonmap
Returns: Polygonmap - Self-reference.
Access: public

ParamTypeDescription
mapMapMap instance.

polygonmap.destroy()

Destructs Polygonmap instance.

Kind: instance method of Polygonmap
Access: public

Examples

Displaying polygonmap over geographical map

ymaps.modules.require(['Polygonmap'], function (Polygonmap) {
    const dataPolygons = {
            type: 'FeatureCollection',
            features: [{
                id: 'id3',
                type: 'Feature',
                geometry: {
                    type: 'Polygon',
                    coordinates: [
                        [37.782051, -122.445068]
                        [37.782051, -122.445568]
                        [37.782951, -122.445068]
                        [37.782951, -122.445568]
                    ]
                }
            }]
        };
    const dataPoints = {
            type: 'FeatureCollection',
            features: [{
                id: 'id1',
                type: 'Feature',
                geometry: {
                    type: 'Point',
                    coordinates: [37.782551, -122.445368]
                }
            }, {
                id: 'id2',
                type: 'Feature',
                geometry: {
                    type: 'Point',
                    coordinates: [37.782745, -122.444586]
                }
            }]
        };
    const data = {polygons: dataPolygons, points: dataPoints};
    const polygonmap = new Polygonmap(data);

    polygonmap.setMap(myMap);
});

Updating polygonmap data

ymaps.modules.require(['Polygonmap'], function (Polygonmap) {
    const data = {polygons: dataPolygons, points: dataPoints};
    const polygonmap = new Polygonmap(data);

    polygonmap.setMap(myMap);

    const newData = {polygons: newDataPolygons, points: newDataPoints};

    polygonmap.setData(newData);
});

Changing polygonmap representation options

ymaps.modules.require(['Polygonmap'], function (Polygonmap) {
    const data = {polygons: dataPolygons, points: dataPoints};
    const polygonmap = new Polygonmap(data);

    polygonmap.options.set('mapper', (feature) => {
        const {pointsCount, pointsCountMaximum} = feature.properties;
        const k = pointsCount / pointsCountMaximum;

        feature.options = {fillColor: `rgba(0, 0, 0, ${k})`};

        return feature;
    });
    polygonmap.setMap(myMap);
});

Demo