UNPKG

10.7 kBJavaScriptView Raw
1var _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
3var _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
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) 2016 - 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';
32
33import PropTypes from 'prop-types';
34
35import Animation from '../../animation';
36import { ANIMATED_SERIES_PROPS } from '../../utils/series-utils';
37import { arc as arcBuilder } from 'd3-shape';
38
39import AbstractSeries from './abstract-series';
40import { getAttributeFunctor, getAttr0Functor, extractScalePropsFromProps, getMissingScaleProps, getScalePropTypesByAttribute } from '../../utils/scales-utils';
41
42var predefinedClassName = 'rv-xy-plot__series rv-xy-plot__series--arc';
43var ATTRIBUTES = ['radius', 'angle'];
44
45var defaultProps = _extends({}, AbstractSeries.defaultProps, {
46 center: { x: 0, y: 0 },
47 arcClassName: '',
48 className: '',
49 style: {},
50 padAngle: 0
51});
52
53/**
54 * Prepare the internal representation of row for real use.
55 * This is necessary because d3 insists on starting at 12 oclock and moving
56 * clockwise, rather than starting at 3 oclock and moving counter clockwise
57 * as one might expect from polar
58 * @param {Object} row - coordinate object to be modifed
59 * @return {Object} angle corrected object
60 */
61function modifyRow(row) {
62 var radius = row.radius,
63 angle = row.angle,
64 angle0 = row.angle0;
65
66 var truedAngle = -1 * angle + Math.PI / 2;
67 var truedAngle0 = -1 * angle0 + Math.PI / 2;
68 return _extends({}, row, {
69 x: radius * Math.cos(truedAngle),
70 y: radius * Math.sin(truedAngle),
71 angle: truedAngle,
72 angle0: truedAngle0
73 });
74}
75
76var ArcSeries = function (_AbstractSeries) {
77 _inherits(ArcSeries, _AbstractSeries);
78
79 function ArcSeries(props) {
80 _classCallCheck(this, ArcSeries);
81
82 var _this = _possibleConstructorReturn(this, (ArcSeries.__proto__ || Object.getPrototypeOf(ArcSeries)).call(this, props));
83
84 var scaleProps = _this._getAllScaleProps(props);
85 _this.state = { scaleProps: scaleProps };
86 return _this;
87 }
88
89 _createClass(ArcSeries, [{
90 key: 'componentWillReceiveProps',
91 value: function componentWillReceiveProps(nextProps) {
92 this.setState({ scaleProps: this._getAllScaleProps(nextProps) });
93 }
94
95 /**
96 * Get the map of scales from the props.
97 * @param {Object} props Props.
98 * @param {Array} data Array of all data.
99 * @returns {Object} Map of scales.
100 * @private
101 */
102
103 }, {
104 key: '_getAllScaleProps',
105 value: function _getAllScaleProps(props) {
106 var defaultScaleProps = this._getDefaultScaleProps(props);
107 var userScaleProps = extractScalePropsFromProps(props, ATTRIBUTES);
108 var missingScaleProps = getMissingScaleProps(_extends({}, defaultScaleProps, userScaleProps), props.data, ATTRIBUTES);
109
110 return _extends({}, defaultScaleProps, userScaleProps, missingScaleProps);
111 }
112
113 /**
114 * Get the list of scale-related settings that should be applied by default.
115 * @param {Object} props Object of props.
116 * @returns {Object} Defaults.
117 * @private
118 */
119
120 }, {
121 key: '_getDefaultScaleProps',
122 value: function _getDefaultScaleProps(props) {
123 var innerWidth = props.innerWidth,
124 innerHeight = props.innerHeight;
125
126 var radius = Math.min(innerWidth / 2, innerHeight / 2);
127 return {
128 radiusRange: [0, radius],
129 _radiusValue: radius,
130 angleType: 'literal'
131 };
132 }
133 }, {
134 key: 'render',
135 value: function render() {
136 var _this2 = this;
137
138 var _props = this.props,
139 arcClassName = _props.arcClassName,
140 animation = _props.animation,
141 className = _props.className,
142 center = _props.center,
143 data = _props.data,
144 disableSeries = _props.disableSeries,
145 hideSeries = _props.hideSeries,
146 marginLeft = _props.marginLeft,
147 marginTop = _props.marginTop,
148 padAngle = _props.padAngle,
149 style = _props.style;
150
151
152 if (!data) {
153 return null;
154 }
155
156 if (animation) {
157 var cloneData = data.map(function (d) {
158 return _extends({}, d);
159 });
160 return React.createElement(
161 'g',
162 { className: 'rv-xy-plot__series--arc__animation-wrapper' },
163 React.createElement(
164 Animation,
165 _extends({}, this.props, {
166 animatedProps: ANIMATED_SERIES_PROPS,
167 data: cloneData
168 }),
169 React.createElement(ArcSeries, _extends({}, this.props, {
170 animation: null,
171 disableSeries: true,
172 data: cloneData
173 }))
174 ),
175 React.createElement(ArcSeries, _extends({}, this.props, {
176 animation: null,
177 hideSeries: true,
178 style: { stroke: 'red' }
179 }))
180 );
181 }
182
183 var scaleProps = this.state.scaleProps;
184 var radiusDomain = scaleProps.radiusDomain;
185 // need to generate our own functors as abstract series doesnt have anythign for us
186
187 var radius = getAttributeFunctor(scaleProps, 'radius');
188 var radius0 = getAttr0Functor(scaleProps, 'radius');
189 var angle = getAttributeFunctor(scaleProps, 'angle');
190 var angle0 = getAttr0Functor(scaleProps, 'angle');
191 // but it does have good color support!
192 var fill = this._getAttributeFunctor('fill') || this._getAttributeFunctor('color');
193 var stroke = this._getAttributeFunctor('stroke') || this._getAttributeFunctor('color');
194 var opacity = this._getAttributeFunctor('opacity');
195 var x = this._getAttributeFunctor('x');
196 var y = this._getAttributeFunctor('y');
197
198 return React.createElement(
199 'g',
200 {
201 className: predefinedClassName + ' ' + className,
202 onMouseOver: this._seriesMouseOverHandler,
203 onMouseOut: this._seriesMouseOutHandler,
204 onClick: this._seriesClickHandler,
205 onContextMenu: this._seriesRightClickHandler,
206 opacity: hideSeries ? 0 : 1,
207 pointerEvents: disableSeries ? 'none' : 'all',
208 transform: 'translate(' + (marginLeft + x(center)) + ',' + (marginTop + y(center)) + ')'
209 },
210 data.map(function (row, i) {
211 var noRadius = radiusDomain[1] === radiusDomain[0];
212 var arcArg = {
213 innerRadius: noRadius ? 0 : radius0(row),
214 outerRadius: radius(row),
215 startAngle: angle0(row) || 0,
216 endAngle: angle(row)
217 };
218 var arcedData = arcBuilder().padAngle(padAngle);
219 var rowStyle = row.style || {};
220 var rowClassName = row.className || '';
221 return React.createElement('path', {
222 style: _extends({
223 opacity: opacity && opacity(row),
224 stroke: stroke && stroke(row),
225 fill: fill && fill(row)
226 }, style, rowStyle),
227 onClick: function onClick(e) {
228 return _this2._valueClickHandler(modifyRow(row), e);
229 },
230 onContextMenu: function onContextMenu(e) {
231 return _this2._valueRightClickHandler(modifyRow(row), e);
232 },
233 onMouseOver: function onMouseOver(e) {
234 return _this2._valueMouseOverHandler(modifyRow(row), e);
235 },
236 onMouseOut: function onMouseOut(e) {
237 return _this2._valueMouseOutHandler(modifyRow(row), e);
238 },
239 key: i,
240 className: predefinedClassName + '-path ' + arcClassName + ' ' + rowClassName,
241 d: arcedData(arcArg)
242 });
243 })
244 );
245 }
246 }]);
247
248 return ArcSeries;
249}(AbstractSeries);
250
251ArcSeries.propTypes = _extends({}, AbstractSeries.propTypes, getScalePropTypesByAttribute('radius'), getScalePropTypesByAttribute('angle'), {
252 center: PropTypes.shape({
253 x: PropTypes.number,
254 y: PropTypes.number
255 }),
256 arcClassName: PropTypes.string,
257 padAngle: PropTypes.oneOfType([PropTypes.func, PropTypes.number])
258});
259ArcSeries.defaultProps = defaultProps;
260ArcSeries.displayName = 'ArcSeries';
261
262export default ArcSeries;
\No newline at end of file