-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathplot.js
137 lines (104 loc) · 4.03 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Copyright 2012-2017, 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 Drawing = require('../../components/drawing');
var Color = require('../../components/color');
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');
module.exports = function plot(geo, calcData) {
function keyFunc(d) { return d[0].trace.uid; }
function removeBADNUM(d, node) {
if(d.lonlat[0] === BADNUM) {
d3.select(node).remove();
}
}
for(var i = 0; i < calcData.length; i++) {
fillLocationLonLat(calcData[i], geo.topojson);
}
var gScatterGeoTraces = geo.framework.select('.scattergeolayer')
.selectAll('g.trace.scattergeo')
.data(calcData, keyFunc);
gScatterGeoTraces.enter().append('g')
.attr('class', 'trace scattergeo');
gScatterGeoTraces.exit().remove();
// TODO find a way to order the inner nodes on update
gScatterGeoTraces.selectAll('*').remove();
gScatterGeoTraces.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, trace) :
geoJsonUtils.makeLine(lineCoords, trace);
s.selectAll('path.js-line')
.data([lineData])
.enter().append('path')
.classed('js-line', true);
}
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(geo);
};
function fillLocationLonLat(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];
}
}
function style(geo) {
var selection = geo.framework.selectAll('g.trace.scattergeo');
selection.style('opacity', function(calcTrace) {
return calcTrace[0].trace.opacity;
});
selection.each(function(calcTrace) {
var trace = calcTrace[0].trace,
group = d3.select(this);
group.selectAll('path.point')
.call(Drawing.pointStyle, trace);
group.selectAll('text')
.call(Drawing.textPointStyle, trace);
});
// this part is incompatible with Drawing.lineGroupStyle
selection.selectAll('path.js-line')
.style('fill', 'none')
.each(function(d) {
var path = d3.select(this),
trace = d.trace,
line = trace.line || {};
path.call(Color.stroke, line.color)
.call(Drawing.dashLine, line.dash || '', line.width || 0);
if(trace.fill !== 'none') {
path.call(Color.fill, trace.fillcolor);
}
});
}