-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathchoropleth.js
159 lines (122 loc) · 6.58 KB
/
choropleth.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
153
154
155
156
157
158
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _reactLeaflet = require('react-leaflet');
var _chromaJs = require('chroma-js');
var _chromaJs2 = _interopRequireDefault(_chromaJs);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _assign = require('./assign');
var _assign2 = _interopRequireDefault(_assign);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Choropleth = function (_Component) {
_inherits(Choropleth, _Component);
function Choropleth() {
_classCallCheck(this, Choropleth);
return _possibleConstructorReturn(this, (Choropleth.__proto__ || Object.getPrototypeOf(Choropleth)).apply(this, arguments));
}
_createClass(Choropleth, [{
key: 'isFunction',
value: function isFunction(prop) {
return typeof prop === 'function';
}
}, {
key: 'getColors',
value: function getColors() {
var _this2 = this;
var _props = this.props,
data = _props.data,
valueProperty = _props.valueProperty,
mode = _props.mode,
steps = _props.steps,
scale = _props.scale,
cl = _props.colors;
var colors = {};
var features = Array.isArray(data) ? data : data.features;
var values = features.map(function (item) {
return _this2.isFunction(valueProperty) ? valueProperty(item) : item.properties[valueProperty];
});
colors.limits = _chromaJs2.default.limits(values, mode, steps - 1);
colors.colors = cl || _chromaJs2.default.scale(scale).colors(steps);
return colors;
}
}, {
key: 'getStyle',
value: function getStyle(_ref, feature) {
var limits = _ref.limits,
colors = _ref.colors;
var _props2 = this.props,
valueProperty = _props2.valueProperty,
_props2$visible = _props2.visible,
visible = _props2$visible === undefined ? function () {
return true;
} : _props2$visible,
userStyle = _props2.style;
if (!(this.isFunction(visible) && visible(feature) || feature.properties[visible])) return userStyle;
var featureValue = this.isFunction(valueProperty) ? valueProperty(feature) : feature.properties[valueProperty];
var idx = !isNaN(featureValue) ? limits.findIndex(function (lim) {
return featureValue <= lim;
}) : -1;
if (colors[idx]) {
var style = {
fillColor: colors[idx]
};
switch (typeof userStyle === 'undefined' ? 'undefined' : _typeof(userStyle)) {
case 'function':
return (0, _assign2.default)(userStyle(feature), style);
case 'object':
return (0, _assign2.default)({}, userStyle, style);
default:
return style;
}
} else {
return userStyle;
}
}
}, {
key: 'cloneChildrenWithFeature',
value: function cloneChildrenWithFeature(props, feature) {
var newProps = (0, _assign2.default)({}, props, { feature: feature });
return _react.Children.map(props.children, function (child) {
return child ? (0, _react.cloneElement)(child, newProps) : null;
});
}
}, {
key: 'render',
value: function render() {
var _this3 = this;
var features = Array.isArray(this.props.data) ? this.props.data : this.props.data.features;
var chroms = this.getColors();
var _props3 = this.props,
layerContainer = _props3.layerContainer,
identity = _props3.identity,
options = _objectWithoutProperties(_props3, ['layerContainer', 'identity']); //remove
return _react2.default.createElement(
_reactLeaflet.FeatureGroup,
{ map: this.props.map, layerContainer: layerContainer, ref: function ref(layer) {
return layer ? _this3.leafletElement = layer.leafletElement : null;
} },
features.map(function (feature, idx) {
return _react2.default.createElement(_reactLeaflet.GeoJSON, _extends({
key: identity ? identity(feature) : idx
}, options, {
style: _this3.getStyle(chroms, feature)
}, _this3.getStyle(chroms, feature), {
data: feature,
children: _this3.props.children ? _this3.cloneChildrenWithFeature(_this3.props, feature) : _this3.props.children
}));
})
);
}
}]);
return Choropleth;
}(_react.Component);
exports.default = Choropleth;