-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathplot.js
132 lines (100 loc) · 3.96 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
/**
* 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 Color = require('../../components/color');
var Drawing = require('../../components/drawing');
var Colorscale = require('../../components/colorscale');
var getTopojsonFeatures = require('../../lib/topojson_utils').getTopojsonFeatures;
var locationToFeature = require('../../lib/geo_location_utils').locationToFeature;
var arrayToCalcItem = require('../../lib/array_to_calc_item');
var constants = require('../../plots/geo/constants');
module.exports = function plot(geo, calcData, geoLayout) {
function keyFunc(d) { return d[0].trace.uid; }
var framework = geo.framework,
gChoropleth = framework.select('g.choroplethlayer'),
gBaseLayer = framework.select('g.baselayer'),
gBaseLayerOverChoropleth = framework.select('g.baselayeroverchoropleth'),
baseLayersOverChoropleth = constants.baseLayersOverChoropleth,
layerName;
var gChoroplethTraces = gChoropleth
.selectAll('g.trace.choropleth')
.data(calcData, keyFunc);
gChoroplethTraces.enter().append('g')
.attr('class', 'trace choropleth');
gChoroplethTraces.exit().remove();
gChoroplethTraces.each(function(calcTrace) {
var trace = calcTrace[0].trace,
cdi = calcGeoJSON(trace, geo.topojson);
var paths = d3.select(this)
.selectAll('path.choroplethlocation')
.data(cdi);
paths.enter().append('path')
.classed('choroplethlocation', true)
.on('mouseover', function(pt) {
geo.choroplethHoverPt = pt;
})
.on('mouseout', function() {
geo.choroplethHoverPt = null;
});
paths.exit().remove();
});
// some baselayers are drawn over choropleth
gBaseLayerOverChoropleth.selectAll('*').remove();
for(var i = 0; i < baseLayersOverChoropleth.length; i++) {
layerName = baseLayersOverChoropleth[i];
gBaseLayer.select('g.' + layerName).remove();
geo.drawTopo(gBaseLayerOverChoropleth, layerName, geoLayout);
geo.styleLayer(gBaseLayerOverChoropleth, layerName, geoLayout);
}
style(geo);
};
function calcGeoJSON(trace, topojson) {
var cdi = [],
locations = trace.locations,
len = locations.length,
features = getTopojsonFeatures(trace, topojson),
markerLine = (trace.marker || {}).line || {};
var feature;
for(var i = 0; i < len; i++) {
feature = locationToFeature(trace.locationmode, locations[i], features);
if(!feature) continue; // filter the blank features here
// 'data_array' attributes
feature.z = trace.z[i];
if(trace.text !== undefined) feature.tx = trace.text[i];
// 'arrayOk' attributes
arrayToCalcItem(markerLine.color, feature, 'mlc', i);
arrayToCalcItem(markerLine.width, feature, 'mlw', i);
// for event data
feature.index = i;
cdi.push(feature);
}
if(cdi.length > 0) cdi[0].trace = trace;
return cdi;
}
function style(geo) {
geo.framework.selectAll('g.trace.choropleth').each(function(calcTrace) {
var trace = calcTrace[0].trace,
s = d3.select(this),
marker = trace.marker || {},
markerLine = marker.line || {};
var sclFunc = Colorscale.makeColorScaleFunc(
Colorscale.extractScale(
trace.colorscale,
trace.zmin,
trace.zmax
)
);
s.selectAll('path.choroplethlocation').each(function(pt) {
d3.select(this)
.attr('fill', function(pt) { return sclFunc(pt.z); })
.call(Color.stroke, pt.mlc || markerLine.color)
.call(Drawing.dashLine, '', pt.mlw || markerLine.width || 0);
});
});
}