# all files / candela/components/Geo/ index.js

67.16% Statements 45/67
73.33% Branches 33/45
61.54% Functions 8/13
23.81% Lines 5/21
11 statements, 2 functions, 18 branches Ignored

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    571×
    1×

    1×
    1×

     import geojs from 'geojs/geo.js';
    import VisComponent from '../../VisComponent';

    export default class Geo extends VisComponent {
      constructor (el, {map = {}, layers = [], width, height}) {
        super(el);

        width = width || map.width || 600;
        height = height || map.height || 600;

        el.style.width = width + 'px';
        el.style.height = height + 'px';

        // Construct a GeoJS map object based on the requested options.
        this.plot = geojs.map(Object.assign({
          node: el
        }, map));

        // Process the requested layers.
        layers.forEach(layer => {
          switch (layer.type) {
            case 'osm':
              this.plot.createLayer('osm', layer);
              break;

            case 'feature':
              layer.features.forEach(spec => {
                let feature = this.plot.createLayer('feature', {
                  renderer: 'd3'
                })
                  .createFeature(spec.type)
                  .data(spec.data)
                  .position(d => ({
                    x: d[spec.x],
                    y: d[spec.y]
                  }));

                const style = Object.assign({
                  fillColor: 'red',
                  strokeColor: 'darkred'
                }, spec.style);

                feature.style(style);
              });
              break;
          }
        });

        this.render();
      }

      render () {
        this.plot.draw();
      }
    }