-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathplot.js
92 lines (72 loc) · 2.87 KB
/
plot.js
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
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Lib = require('../../lib');
var BADNUM = require('../../constants/numerical').BADNUM;
var getTopojsonFeatures = require('../../lib/topojson_utils').getTopojsonFeatures;
var locationToFeature = require('../../lib/geo_location_utils').locationToFeature;
var geoJsonUtils = require('../../lib/geojson_utils');
var subTypes = require('../scatter/subtypes');
var style = require('./style');
module.exports = function plot(gd, geo, calcData) {
for(var i = 0; i < calcData.length; i++) {
calcGeoJSON(calcData[i], geo.topojson);
}
function removeBADNUM(d, node) {
if(d.lonlat[0] === BADNUM) {
d3.select(node).remove();
}
}
var scatterLayer = geo.layers.frontplot.select('.scatterlayer');
var gTraces = Lib.makeTraceGroups(scatterLayer, calcData, 'trace scattergeo');
// TODO find a way to order the inner nodes on update
gTraces.selectAll('*').remove();
gTraces.each(function(calcTrace) {
var s = calcTrace[0].node3 = d3.select(this);
var trace = calcTrace[0].trace;
if(subTypes.hasLines(trace) || trace.fill !== 'none') {
var lineCoords = geoJsonUtils.calcTraceToLineCoords(calcTrace);
var lineData = (trace.fill !== 'none') ?
geoJsonUtils.makePolygon(lineCoords) :
geoJsonUtils.makeLine(lineCoords);
s.selectAll('path.js-line')
.data([{geojson: lineData, trace: trace}])
.enter().append('path')
.classed('js-line', true)
.style('stroke-miterlimit', 2);
}
if(subTypes.hasMarkers(trace)) {
s.selectAll('path.point')
.data(Lib.identity)
.enter().append('path')
.classed('point', true)
.each(function(calcPt) { removeBADNUM(calcPt, this); });
}
if(subTypes.hasText(trace)) {
s.selectAll('g')
.data(Lib.identity)
.enter().append('g')
.append('text')
.each(function(calcPt) { removeBADNUM(calcPt, this); });
}
// call style here within topojson request callback
style(gd, calcTrace);
});
};
function calcGeoJSON(calcTrace, topojson) {
var trace = calcTrace[0].trace;
if(!Array.isArray(trace.locations)) return;
var features = getTopojsonFeatures(trace, topojson);
var locationmode = trace.locationmode;
for(var i = 0; i < calcTrace.length; i++) {
var calcPt = calcTrace[i];
var feature = locationToFeature(locationmode, calcPt.loc, features);
calcPt.lonlat = feature ? feature.properties.ct : [BADNUM, BADNUM];
}
}