1 | 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; }; }();
|
2 |
|
3 | 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; };
|
4 |
|
5 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
6 |
|
7 | 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; }
|
8 |
|
9 | 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; }
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | import PropTypes from 'prop-types';
|
32 | import { voronoi } from 'd3-voronoi';
|
33 | import { PureComponent } from 'react';
|
34 |
|
35 | import { AnimationPropType } from '../../animation';
|
36 | import { getAttributeFunctor, getAttr0Functor, getAttributeValue, getScaleObjectFromProps, getScalePropTypesByAttribute } from '../../utils/scales-utils';
|
37 |
|
38 | var propTypes = _extends({}, getScalePropTypesByAttribute('x'), getScalePropTypesByAttribute('y'), getScalePropTypesByAttribute('size'), getScalePropTypesByAttribute('opacity'), getScalePropTypesByAttribute('color'), {
|
39 | width: PropTypes.number,
|
40 | height: PropTypes.number,
|
41 | data: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.object, PropTypes.array])),
|
42 | onValueMouseOver: PropTypes.func,
|
43 | onValueMouseOut: PropTypes.func,
|
44 | onValueClick: PropTypes.func,
|
45 | onValueRightClick: PropTypes.func,
|
46 | onSeriesMouseOver: PropTypes.func,
|
47 | onSeriesMouseOut: PropTypes.func,
|
48 | onSeriesClick: PropTypes.func,
|
49 | onSeriesRightClick: PropTypes.func,
|
50 | onNearestX: PropTypes.func,
|
51 | onNearestXY: PropTypes.func,
|
52 | style: PropTypes.object,
|
53 | animation: AnimationPropType,
|
54 | stack: PropTypes.bool
|
55 | });
|
56 |
|
57 | var defaultProps = {
|
58 | className: '',
|
59 | stack: false,
|
60 | style: {}
|
61 | };
|
62 |
|
63 | var AbstractSeries = function (_PureComponent) {
|
64 | _inherits(AbstractSeries, _PureComponent);
|
65 |
|
66 | function AbstractSeries() {
|
67 | var _ref;
|
68 |
|
69 | var _temp, _this, _ret;
|
70 |
|
71 | _classCallCheck(this, AbstractSeries);
|
72 |
|
73 | for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
74 | args[_key] = arguments[_key];
|
75 | }
|
76 |
|
77 | return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = AbstractSeries.__proto__ || Object.getPrototypeOf(AbstractSeries)).call.apply(_ref, [this].concat(args))), _this), _this._seriesClickHandler = function (event) {
|
78 | var onSeriesClick = _this.props.onSeriesClick;
|
79 |
|
80 | if (onSeriesClick) {
|
81 | onSeriesClick({ event: event });
|
82 | }
|
83 | }, _this._seriesMouseOutHandler = function (event) {
|
84 | var onSeriesMouseOut = _this.props.onSeriesMouseOut;
|
85 |
|
86 | if (onSeriesMouseOut) {
|
87 | onSeriesMouseOut({ event: event });
|
88 | }
|
89 | }, _this._seriesMouseOverHandler = function (event) {
|
90 | var onSeriesMouseOver = _this.props.onSeriesMouseOver;
|
91 |
|
92 | if (onSeriesMouseOver) {
|
93 | onSeriesMouseOver({ event: event });
|
94 | }
|
95 | }, _this._seriesRightClickHandler = function (event) {
|
96 | var onSeriesRightClick = _this.props.onSeriesRightClick;
|
97 |
|
98 | if (onSeriesRightClick) {
|
99 | onSeriesRightClick({ event: event });
|
100 | }
|
101 | }, _this._valueClickHandler = function (d, event) {
|
102 | var _this$props = _this.props,
|
103 | onValueClick = _this$props.onValueClick,
|
104 | onSeriesClick = _this$props.onSeriesClick;
|
105 |
|
106 | if (onValueClick) {
|
107 | onValueClick(d, { event: event });
|
108 | }
|
109 | if (onSeriesClick) {
|
110 | onSeriesClick({ event: event });
|
111 | }
|
112 | }, _this._valueMouseOutHandler = function (d, event) {
|
113 | var _this$props2 = _this.props,
|
114 | onValueMouseOut = _this$props2.onValueMouseOut,
|
115 | onSeriesMouseOut = _this$props2.onSeriesMouseOut;
|
116 |
|
117 | if (onValueMouseOut) {
|
118 | onValueMouseOut(d, { event: event });
|
119 | }
|
120 | if (onSeriesMouseOut) {
|
121 | onSeriesMouseOut({ event: event });
|
122 | }
|
123 | }, _this._valueMouseOverHandler = function (d, event) {
|
124 | var _this$props3 = _this.props,
|
125 | onValueMouseOver = _this$props3.onValueMouseOver,
|
126 | onSeriesMouseOver = _this$props3.onSeriesMouseOver;
|
127 |
|
128 | if (onValueMouseOver) {
|
129 | onValueMouseOver(d, { event: event });
|
130 | }
|
131 | if (onSeriesMouseOver) {
|
132 | onSeriesMouseOver({ event: event });
|
133 | }
|
134 | }, _this._valueRightClickHandler = function (d, event) {
|
135 | var _this$props4 = _this.props,
|
136 | onValueRightClick = _this$props4.onValueRightClick,
|
137 | onSeriesRightClick = _this$props4.onSeriesRightClick;
|
138 |
|
139 | if (onValueRightClick) {
|
140 | onValueRightClick(d, { event: event });
|
141 | }
|
142 | if (onSeriesRightClick) {
|
143 | onSeriesRightClick({ event: event });
|
144 | }
|
145 | }, _temp), _possibleConstructorReturn(_this, _ret);
|
146 | }
|
147 |
|
148 | _createClass(AbstractSeries, [{
|
149 | key: 'onParentMouseMove',
|
150 | value: function onParentMouseMove(event) {
|
151 | var _props = this.props,
|
152 | onNearestX = _props.onNearestX,
|
153 | onNearestXY = _props.onNearestXY,
|
154 | data = _props.data;
|
155 |
|
156 | if (!onNearestX && !onNearestXY || !data) {
|
157 | return;
|
158 | }
|
159 | if (onNearestXY) {
|
160 | this._handleNearestXY(event);
|
161 | } else {
|
162 | this._handleNearestX(event);
|
163 | }
|
164 | }
|
165 | }, {
|
166 | key: 'onParentTouchMove',
|
167 | value: function onParentTouchMove(e) {
|
168 | e.preventDefault();
|
169 | this.onParentMouseMove(e);
|
170 | }
|
171 | }, {
|
172 | key: 'onParentTouchStart',
|
173 | value: function onParentTouchStart(e) {
|
174 |
|
175 | e.preventDefault();
|
176 | }
|
177 |
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | }, {
|
186 | key: '_getAttr0Functor',
|
187 | value: function _getAttr0Functor(attr) {
|
188 | return getAttr0Functor(this.props, attr);
|
189 | }
|
190 |
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | }, {
|
199 | key: '_getAttributeFunctor',
|
200 | value: function _getAttributeFunctor(attr) {
|
201 | return getAttributeFunctor(this.props, attr);
|
202 | }
|
203 |
|
204 | |
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | }, {
|
213 | key: '_getAttributeValue',
|
214 | value: function _getAttributeValue(attr) {
|
215 | return getAttributeValue(this.props, attr);
|
216 | }
|
217 |
|
218 | |
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | }, {
|
226 | key: '_getScaleDistance',
|
227 | value: function _getScaleDistance(attr) {
|
228 | var scaleObject = getScaleObjectFromProps(this.props, attr);
|
229 | return scaleObject ? scaleObject.distance : 0;
|
230 | }
|
231 | }, {
|
232 | key: '_getXYCoordinateInContainer',
|
233 | value: function _getXYCoordinateInContainer(event) {
|
234 | var _props2 = this.props,
|
235 | _props2$marginTop = _props2.marginTop,
|
236 | marginTop = _props2$marginTop === undefined ? 0 : _props2$marginTop,
|
237 | _props2$marginLeft = _props2.marginLeft,
|
238 | marginLeft = _props2$marginLeft === undefined ? 0 : _props2$marginLeft;
|
239 | var evt = event.nativeEvent,
|
240 | currentTarget = event.currentTarget;
|
241 |
|
242 | var rect = currentTarget.getBoundingClientRect();
|
243 | var x = evt.clientX;
|
244 | var y = evt.clientY;
|
245 | if (evt.type === 'touchmove') {
|
246 | x = evt.touches[0].pageX;
|
247 | y = evt.touches[0].pageY;
|
248 | }
|
249 | return {
|
250 | x: x - rect.left - currentTarget.clientLeft - marginLeft,
|
251 | y: y - rect.top - currentTarget.clientTop - marginTop
|
252 | };
|
253 | }
|
254 | }, {
|
255 | key: '_handleNearestX',
|
256 | value: function _handleNearestX(event) {
|
257 | var _props3 = this.props,
|
258 | onNearestX = _props3.onNearestX,
|
259 | data = _props3.data;
|
260 |
|
261 | var minDistance = Number.POSITIVE_INFINITY;
|
262 | var value = null;
|
263 | var valueIndex = null;
|
264 |
|
265 | var coordinate = this._getXYCoordinateInContainer(event);
|
266 | var xScaleFn = this._getAttributeFunctor('x');
|
267 |
|
268 | data.forEach(function (item, i) {
|
269 | var currentCoordinate = xScaleFn(item);
|
270 | var newDistance = Math.abs(coordinate.x - currentCoordinate);
|
271 | if (newDistance < minDistance) {
|
272 | minDistance = newDistance;
|
273 | value = item;
|
274 | valueIndex = i;
|
275 | }
|
276 | });
|
277 | if (!value) {
|
278 | return;
|
279 | }
|
280 | onNearestX(value, {
|
281 | innerX: xScaleFn(value),
|
282 | index: valueIndex,
|
283 | event: event.nativeEvent
|
284 | });
|
285 | }
|
286 | }, {
|
287 | key: '_handleNearestXY',
|
288 | value: function _handleNearestXY(event) {
|
289 | var _props4 = this.props,
|
290 | onNearestXY = _props4.onNearestXY,
|
291 | data = _props4.data;
|
292 |
|
293 |
|
294 | var coordinate = this._getXYCoordinateInContainer(event);
|
295 | var xScaleFn = this._getAttributeFunctor('x');
|
296 | var yScaleFn = this._getAttributeFunctor('y');
|
297 |
|
298 |
|
299 | var voronoiInstance = voronoi().x(xScaleFn).y(yScaleFn);
|
300 |
|
301 | var foundPoint = voronoiInstance(data).find(coordinate.x, coordinate.y);
|
302 | var value = foundPoint.data;
|
303 |
|
304 | if (!value) {
|
305 | return;
|
306 | }
|
307 | onNearestXY(value, {
|
308 | innerX: foundPoint[0],
|
309 | innerY: foundPoint[1],
|
310 | index: foundPoint.index,
|
311 | event: event.nativeEvent
|
312 | });
|
313 | }
|
314 |
|
315 | |
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 | |
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | |
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 | |
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | |
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 | |
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 | |
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 | |
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 | }], [{
|
375 | key: 'getParentConfig',
|
376 |
|
377 | |
378 |
|
379 |
|
380 |
|
381 | value: function getParentConfig() {
|
382 | return {};
|
383 | }
|
384 |
|
385 | |
386 |
|
387 |
|
388 |
|
389 |
|
390 | }, {
|
391 | key: 'requiresSVG',
|
392 | get: function get() {
|
393 | return true;
|
394 | }
|
395 | }]);
|
396 |
|
397 | return AbstractSeries;
|
398 | }(PureComponent);
|
399 |
|
400 | AbstractSeries.displayName = 'AbstractSeries';
|
401 | AbstractSeries.propTypes = propTypes;
|
402 | AbstractSeries.defaultProps = defaultProps;
|
403 |
|
404 | export default AbstractSeries; |
\ | No newline at end of file |