1 | 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; };
|
2 |
|
3 | 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; }; }();
|
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 React from 'react';
|
32 | import PropTypes from 'prop-types';
|
33 | import AbstractSeries from './abstract-series';
|
34 | import Animation from '../../animation';
|
35 | import { ANIMATED_SERIES_PROPS } from '../../utils/series-utils';
|
36 | var predefinedClassName = 'rv-xy-plot__series rv-xy-plot__series--label';
|
37 |
|
38 | var getTextAnchor = function getTextAnchor(labelAnchorX, leftOfMiddle) {
|
39 | return labelAnchorX ? labelAnchorX : leftOfMiddle ? 'start' : 'end';
|
40 | };
|
41 | var getDominantBaseline = function getDominantBaseline(labelAnchorY, aboveMiddle) {
|
42 | return labelAnchorY ? labelAnchorY : aboveMiddle ? 'text-before-edge' : 'text-after-edge';
|
43 | };
|
44 |
|
45 | var LabelSeries = function (_AbstractSeries) {
|
46 | _inherits(LabelSeries, _AbstractSeries);
|
47 |
|
48 | function LabelSeries() {
|
49 | _classCallCheck(this, LabelSeries);
|
50 |
|
51 | return _possibleConstructorReturn(this, (LabelSeries.__proto__ || Object.getPrototypeOf(LabelSeries)).apply(this, arguments));
|
52 | }
|
53 |
|
54 | _createClass(LabelSeries, [{
|
55 | key: 'render',
|
56 | value: function render() {
|
57 | var _this2 = this;
|
58 |
|
59 | var _props = this.props,
|
60 | animation = _props.animation,
|
61 | allowOffsetToBeReversed = _props.allowOffsetToBeReversed,
|
62 | className = _props.className,
|
63 | data = _props.data,
|
64 | _data = _props._data,
|
65 | getLabel = _props.getLabel,
|
66 | marginLeft = _props.marginLeft,
|
67 | marginTop = _props.marginTop,
|
68 | rotation = _props.rotation,
|
69 | style = _props.style,
|
70 | xRange = _props.xRange,
|
71 | yRange = _props.yRange,
|
72 | labelAnchorX = _props.labelAnchorX,
|
73 | labelAnchorY = _props.labelAnchorY;
|
74 |
|
75 | if (!data) {
|
76 | return null;
|
77 | }
|
78 |
|
79 | if (animation) {
|
80 | return React.createElement(
|
81 | Animation,
|
82 | _extends({}, this.props, { animatedProps: ANIMATED_SERIES_PROPS }),
|
83 | React.createElement(LabelSeries, _extends({}, this.props, { animation: null, _data: data }))
|
84 | );
|
85 | }
|
86 |
|
87 | var xFunctor = this._getAttributeFunctor('x');
|
88 | var yFunctor = this._getAttributeFunctor('y');
|
89 |
|
90 | return React.createElement(
|
91 | 'g',
|
92 | {
|
93 | className: predefinedClassName + ' ' + className,
|
94 | transform: 'translate(' + marginLeft + ',' + marginTop + ')',
|
95 | style: style
|
96 | },
|
97 | data.reduce(function (res, d, i) {
|
98 | var markStyle = d.style,
|
99 | xOffset = d.xOffset,
|
100 | yOffset = d.yOffset;
|
101 |
|
102 | if (!getLabel(d)) {
|
103 | return res;
|
104 | }
|
105 | var xVal = xFunctor(d);
|
106 | var yVal = yFunctor(d);
|
107 | var leftOfMiddle = xVal < (xRange[1] - xRange[0]) / 2;
|
108 | var aboveMiddle = yVal < Math.abs(yRange[1] - yRange[0]) / 2;
|
109 |
|
110 | var x = xVal + (allowOffsetToBeReversed && leftOfMiddle ? -1 : 1) * (xOffset || 0);
|
111 | var y = yVal + (allowOffsetToBeReversed && aboveMiddle ? -1 : 1) * (yOffset || 0);
|
112 |
|
113 | var hasRotationValueSet = d.rotation === 0 || d.rotation;
|
114 | var labelRotation = hasRotationValueSet ? d.rotation : rotation;
|
115 | var attrs = _extends({
|
116 | dominantBaseline: getDominantBaseline(labelAnchorY, aboveMiddle),
|
117 | className: 'rv-xy-plot__series--label-text',
|
118 | key: i,
|
119 | onClick: function onClick(e) {
|
120 | return _this2._valueClickHandler(d, e);
|
121 | },
|
122 | onContextMenu: function onContextMenu(e) {
|
123 | return _this2._valueRightClickHandler(d, e);
|
124 | },
|
125 | onMouseOver: function onMouseOver(e) {
|
126 | return _this2._valueMouseOverHandler(d, e);
|
127 | },
|
128 | onMouseOut: function onMouseOut(e) {
|
129 | return _this2._valueMouseOutHandler(d, e);
|
130 | },
|
131 | textAnchor: getTextAnchor(labelAnchorX, leftOfMiddle),
|
132 | x: x,
|
133 | y: y,
|
134 | transform: 'rotate(' + labelRotation + ',' + x + ',' + y + ')'
|
135 | }, markStyle);
|
136 | var textContent = getLabel(_data ? _data[i] : d);
|
137 | return res.concat([React.createElement(
|
138 | 'text',
|
139 | attrs,
|
140 | textContent
|
141 | )]);
|
142 | }, [])
|
143 | );
|
144 | }
|
145 | }]);
|
146 |
|
147 | return LabelSeries;
|
148 | }(AbstractSeries);
|
149 |
|
150 | LabelSeries.propTypes = {
|
151 | animation: PropTypes.bool,
|
152 | allowOffsetToBeReversed: PropTypes.bool,
|
153 | className: PropTypes.string,
|
154 | data: PropTypes.arrayOf(PropTypes.shape({
|
155 | x: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
156 | y: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
157 | angle: PropTypes.number,
|
158 | radius: PropTypes.number,
|
159 | label: PropTypes.string,
|
160 | xOffset: PropTypes.number,
|
161 | yOffset: PropTypes.number,
|
162 | style: PropTypes.object
|
163 | })).isRequired,
|
164 | marginLeft: PropTypes.number,
|
165 | marginTop: PropTypes.number,
|
166 | rotation: PropTypes.number,
|
167 | style: PropTypes.object,
|
168 | xRange: PropTypes.arrayOf(PropTypes.number),
|
169 | yRange: PropTypes.arrayOf(PropTypes.number),
|
170 | labelAnchorX: PropTypes.string,
|
171 | labelAnchorY: PropTypes.string
|
172 | };
|
173 | LabelSeries.defaultProps = _extends({}, AbstractSeries.defaultProps, {
|
174 | animation: false,
|
175 | rotation: 0,
|
176 | getLabel: function getLabel(d) {
|
177 | return d.label;
|
178 | }
|
179 | });
|
180 | LabelSeries.displayName = 'LabelSeries';
|
181 | export default LabelSeries; |
\ | No newline at end of file |