all files / candela/components/GeoDots/ index.js

56.52% Statements 52/92
65.08% Branches 41/63
60% Functions 12/20
16.67% Lines 7/42
12 statements, 6 functions, 21 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154                                                                                                                                                                                                                                                                                                        
import d3 from 'd3';
import Geo from '../Geo';
import VisComponent from '../../VisComponent';
import { minmax } from '../../util';
 
export default class GeoDots extends VisComponent {
  static get options () {
    return [
      {
        name: 'data',
        type: 'table',
        format: 'objectlist'
      },
      {
        name: 'latitude',
        type: 'string',
        format: 'text',
        domain: {
          mode: 'field',
          from: 'data',
          fieldTypes: ['number']
        }
      },
      {
        name: 'longitude',
        type: 'string',
        format: 'text',
        domain: {
          mode: 'field',
          from: 'data',
          fieldTypes: ['number']
        }
      },
      {
        name: 'color',
        type: 'string',
        format: 'text',
        optional: true,
        domain: {
          mode: 'field',
          from: 'data',
          fieldTypes: ['string', 'date', 'number', 'integer', 'boolean']
        }
      },
      {
        name: 'size',
        type: 'string',
        format: 'text',
        optional: true,
        domain: {
          mode: 'field',
          from: 'data',
          fieldTypes: ['number', 'integer', 'boolean']
        }
      }
    ];
  }
 
  constructor (el, options) {
    super(el);
 
    let sizeTransform = 5;
    if (options.size) {
      const range = minmax(options.data.map(d => d[options.size]));
      const scale = d3.scale.linear()
        .domain([range.min, range.max])
        .range([3, 19]);
 
      sizeTransform = d => scale(d[options.size]);
    }
 
    let fillTransform = 'red';
    let strokeTransform = 'darkred';
    if (options.color && options.data.length > 0) {
      let fillScale, strokeScale;
 
      const type = typeof options.data[0][options.color];
      if (type === undefined || type === 'string') {
        fillScale = d3.scale.category10();
        strokeScale = 'black';
      } else {
        const range = minmax(options.data.map(d => d[options.color]));
 
        const red = d3.rgb('#ef6a62');
        const blue = d3.rgb('#67a9cf');
        const darkred = red.darker();
        const darkblue = blue.darker();
 
        fillScale = d3.scale.linear()
          .domain([range.min, range.max])
          .range([red, blue]);
 
        strokeScale = d3.scale.linear()
          .domain([range.min, range.max])
          .range([darkred, darkblue]);
      }
 
      fillTransform = d => fillScale(d[options.color]);
      if (strokeScale === 'black') {
        strokeTransform = 'black';
      } else {
        strokeTransform = d => strokeScale(d[options.color]);
      }
    }
 
    // TODO(choudhury): don't mutate the options object directly.
    options.layers = [];
    if (options.tileUrl !== null) {
      options.layers.push({
        type: 'osm',
        url: options.tileUrl
      });
    }
 
    options.layers.push({
      type: 'feature',
      features: [
        {
          name: 'feature1',
          type: 'point',
          x: options.longitude,
          y: options.latitude,
          style: {
            radius: sizeTransform,
            fillColor: fillTransform,
            strokeColor: strokeTransform
          },
          data: options.data
        }
      ]
    });
 
    options.center = options.center || {};
    const center = {
      x: options.center.longitude || 0.0,
      y: options.center.latitude || 0.0
    };
 
    const map_options = Object.assign({
      map: {
        zoom: options.zoom,
        center: center
      }
    }, options);
 
    this.geojs = new Geo(this.el, map_options);
  }
 
  render () {
    this.geojs.render();
  }
}