-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathplot.js
123 lines (100 loc) · 3.9 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
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
'use strict';
var d3 = require('@plotly/d3');
var Lib = require('../../lib');
var getTopojsonFeatures = require('../../lib/topojson_utils').getTopojsonFeatures;
var geoJsonUtils = require('../../lib/geojson_utils');
var geoUtils = require('../../lib/geo_location_utils');
var findExtremes = require('../../plots/cartesian/autorange').findExtremes;
var BADNUM = require('../../constants/numerical').BADNUM;
var calcMarkerSize = require('../scatter/calc').calcMarkerSize;
var subTypes = require('../scatter/subtypes');
var style = require('./style');
function plot(gd, geo, calcData) {
var scatterLayer = geo.layers.frontplot.select('.scatterlayer');
var gTraces = Lib.makeTraceGroups(scatterLayer, calcData, 'trace scattergeo');
function removeBADNUM(d, node) {
if(
d.lonlat &&
d.lonlat[0] === BADNUM
) {
d3.select(node).remove();
}
}
// TODO find a way to order the inner nodes on update
gTraces.selectAll('*').remove();
gTraces.each(function(calcTrace) {
var s = 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, fullLayout) {
var trace = calcTrace[0].trace;
var geoLayout = fullLayout[trace.geo];
var geo = geoLayout._subplot;
var len = trace._length;
var i, calcPt;
if(Lib.isArrayOrTypedArray(trace.locations)) {
var locationmode = trace.locationmode;
var features = locationmode === 'geojson-id' ?
geoUtils.extractTraceFeature(calcTrace) :
getTopojsonFeatures(trace, geo.topojson);
for(i = 0; i < len; i++) {
calcPt = calcTrace[i];
var feature = locationmode === 'geojson-id' ?
calcPt.fOut :
geoUtils.locationToFeature(locationmode, calcPt.loc, features);
calcPt.lonlat = feature ? feature.properties.ct : [BADNUM, BADNUM];
}
}
var opts = {padded: true};
var lonArray;
var latArray;
if(geoLayout.fitbounds === 'geojson' && trace.locationmode === 'geojson-id') {
var bboxGeojson = geoUtils.computeBbox(geoUtils.getTraceGeojson(trace));
lonArray = [bboxGeojson[0], bboxGeojson[2]];
latArray = [bboxGeojson[1], bboxGeojson[3]];
} else {
lonArray = [];
latArray = [];
for(i = 0; i < len; i++) {
calcPt = calcTrace[i];
if(!calcPt.lonlat) continue;
lonArray.push(calcPt.lonlat[0]);
latArray.push(calcPt.lonlat[1]);
}
opts.ppad = calcMarkerSize(trace, len);
}
trace._extremes.lon = findExtremes(geoLayout.lonaxis._ax, lonArray, opts);
trace._extremes.lat = findExtremes(geoLayout.lataxis._ax, latArray, opts);
}
module.exports = {
calcGeoJSON: calcGeoJSON,
plot: plot
};