-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathplot.js
122 lines (94 loc) · 3.51 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
/**
* Copyright 2012-2016, 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');
function ScatterMapbox(mapbox, uid) {
this.mapbox = mapbox;
this.map = mapbox.map;
this.uid = uid;
this.idSourceFill = uid + '-source-fill';
this.idSourceLine = uid + '-source-line';
this.idSourceCircle = uid + '-source-circle';
this.idSourceSymbol = uid + '-source-symbol';
this.idLayerFill = uid + '-layer-fill';
this.idLayerLine = uid + '-layer-line';
this.idLayerCircle = uid + '-layer-circle';
this.idLayerSymbol = uid + '-layer-symbol';
this.mapbox.initSource(this.idSourceFill);
this.mapbox.initSource(this.idSourceLine);
this.mapbox.initSource(this.idSourceCircle);
this.mapbox.initSource(this.idSourceSymbol);
this.map.addLayer({
id: this.idLayerFill,
source: this.idSourceFill,
type: 'fill'
});
this.map.addLayer({
id: this.idLayerLine,
source: this.idSourceLine,
type: 'line'
});
this.map.addLayer({
id: this.idLayerCircle,
source: this.idSourceCircle,
type: 'circle'
});
this.map.addLayer({
id: this.idLayerSymbol,
source: this.idSourceSymbol,
type: 'symbol'
});
// We could merge the 'fill' source with the 'line' source and
// the 'circle' source with the 'symbol' source if ever having
// for up-to 4 sources per 'scattermapbox' traces becomes a problem.
}
var proto = ScatterMapbox.prototype;
proto.update = function update(calcTrace) {
var mapbox = this.mapbox;
var opts = convert(calcTrace);
mapbox.setOptions(this.idLayerFill, 'setLayoutProperty', opts.fill.layout);
mapbox.setOptions(this.idLayerLine, 'setLayoutProperty', opts.line.layout);
mapbox.setOptions(this.idLayerCircle, 'setLayoutProperty', opts.circle.layout);
mapbox.setOptions(this.idLayerSymbol, 'setLayoutProperty', opts.symbol.layout);
if(isVisible(opts.fill)) {
mapbox.setSourceData(this.idSourceFill, opts.fill.geojson);
mapbox.setOptions(this.idLayerFill, 'setPaintProperty', opts.fill.paint);
}
if(isVisible(opts.line)) {
mapbox.setSourceData(this.idSourceLine, opts.line.geojson);
mapbox.setOptions(this.idLayerLine, 'setPaintProperty', opts.line.paint);
}
if(isVisible(opts.circle)) {
mapbox.setSourceData(this.idSourceCircle, opts.circle.geojson);
mapbox.setOptions(this.idLayerCircle, 'setPaintProperty', opts.circle.paint);
}
if(isVisible(opts.symbol)) {
mapbox.setSourceData(this.idSourceSymbol, opts.symbol.geojson);
mapbox.setOptions(this.idLayerSymbol, 'setPaintProperty', opts.symbol.paint);
}
};
proto.dispose = function dispose() {
var map = this.map;
map.removeLayer(this.idLayerFill);
map.removeLayer(this.idLayerLine);
map.removeLayer(this.idLayerCircle);
map.removeLayer(this.idLayerSymbol);
map.removeSource(this.idSourceFill);
map.removeSource(this.idSourceLine);
map.removeSource(this.idSourceCircle);
map.removeSource(this.idSourceSymbol);
};
function isVisible(layerOpts) {
return layerOpts.layout.visibility === 'visible';
}
module.exports = function createScatterMapbox(mapbox, calcTrace) {
var trace = calcTrace[0].trace;
var scatterMapbox = new ScatterMapbox(mapbox, trace.uid);
scatterMapbox.update(calcTrace);
return scatterMapbox;
};