UNPKG

8.03 kBJavaScriptView Raw
1var _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
3var _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
5function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7function _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
9function _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// Copyright (c) 2017 Uber Technologies, Inc.
12//
13// Permission is hereby granted, free of charge, to any person obtaining a copy
14// of this software and associated documentation files (the "Software"), to deal
15// in the Software without restriction, including without limitation the rights
16// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17// copies of the Software, and to permit persons to whom the Software is
18// furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included in
21// all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29// THE SOFTWARE.
30
31import React from 'react';
32import PropTypes from 'prop-types';
33import AbstractSeries from './abstract-series';
34import Animation from '../../animation';
35import { ANIMATED_SERIES_PROPS } from '../../utils/series-utils';
36var predefinedClassName = 'rv-xy-plot__series rv-xy-plot__series--label';
37
38var getTextAnchor = function getTextAnchor(labelAnchorX, leftOfMiddle) {
39 return labelAnchorX ? labelAnchorX : leftOfMiddle ? 'start' : 'end';
40};
41var getDominantBaseline = function getDominantBaseline(labelAnchorY, aboveMiddle) {
42 return labelAnchorY ? labelAnchorY : aboveMiddle ? 'text-before-edge' : 'text-after-edge';
43};
44
45var 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
150LabelSeries.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};
173LabelSeries.defaultProps = _extends({}, AbstractSeries.defaultProps, {
174 animation: false,
175 rotation: 0,
176 getLabel: function getLabel(d) {
177 return d.label;
178 }
179});
180LabelSeries.displayName = 'LabelSeries';
181export default LabelSeries;
\No newline at end of file