-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathplot.js
152 lines (118 loc) · 3.57 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Copyright 2012-2019, 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 convert = require('./convert').convert;
var convertOnSelect = require('./convert').convertOnSelect;
function ChoroplethMapbox(subplot, uid) {
this.subplot = subplot;
this.uid = uid;
// N.B. fill and line layers share same source
this.sourceId = uid + '-source';
this.layerList = [
['fill', uid + '-layer-fill'],
['line', uid + '-layer-line']
];
// previous 'below' value,
// need this to update it properly
this.below = null;
}
var proto = ChoroplethMapbox.prototype;
proto.update = function(calcTrace) {
this._update(convert(calcTrace));
};
proto.updateOnSelect = function(calcTrace) {
this._update(convertOnSelect(calcTrace));
};
proto._update = function(optsAll) {
var subplot = this.subplot;
var layerList = this.layerList;
subplot.map
.getSource(this.sourceId)
.setData(optsAll.geojson);
if(optsAll.below !== this.below) {
this._removeLayers();
this._addLayers(optsAll);
}
for(var i = 0; i < layerList.length; i++) {
var item = layerList[i];
var k = item[0];
var id = item[1];
var opts = optsAll[k];
subplot.setOptions(id, 'setLayoutProperty', opts.layout);
if(opts.layout.visibility === 'visible') {
subplot.setOptions(id, 'setPaintProperty', opts.paint);
}
}
};
proto._addLayers = function(optsAll) {
var subplot = this.subplot;
var layerList = this.layerList;
var sourceId = this.sourceId;
var below = this.getBelow(optsAll);
for(var i = 0; i < layerList.length; i++) {
var item = layerList[i];
var k = item[0];
var opts = optsAll[k];
subplot.map.addLayer({
type: k,
id: item[1],
source: sourceId,
layout: opts.layout,
paint: opts.paint
}, below);
}
this.below = below;
};
proto._removeLayers = function() {
var map = this.subplot.map;
var layerList = this.layerList;
for(var i = layerList.length - 1; i >= 0; i--) {
map.removeLayer(layerList[i][1]);
}
};
proto.dispose = function() {
var map = this.subplot.map;
this._removeLayers();
map.removeSource(this.sourceId);
};
proto.getBelow = function(optsAll) {
if(optsAll.below !== undefined) {
return optsAll.below;
}
var mapLayers = this.subplot.map.getStyle().layers;
var out = '';
// find layer just above top-most "water" layer
for(var i = 0; i < mapLayers.length; i++) {
var layerId = mapLayers[i].id;
if(typeof layerId === 'string') {
var isWaterLayer = layerId.indexOf('water') === 0;
if(out && !isWaterLayer) {
out = layerId;
break;
}
if(isWaterLayer) {
out = layerId;
}
}
}
return out;
};
module.exports = function createChoroplethMapbox(subplot, calcTrace) {
var trace = calcTrace[0].trace;
var choroplethMapbox = new ChoroplethMapbox(subplot, trace.uid);
var sourceId = choroplethMapbox.sourceId;
var optsAll = convert(calcTrace);
subplot.map.addSource(sourceId, {
type: 'geojson',
data: optsAll.geojson
});
choroplethMapbox._addLayers(optsAll);
// link ref for quick update during selections
calcTrace[0].trace._glTrace = choroplethMapbox;
return choroplethMapbox;
};