UNPKG

6.66 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) 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';
32import PropTypes from 'prop-types';
33import * as d3Shape from 'd3-shape';
34
35import Animation from '../../animation';
36import { DEFAULT_OPACITY } from '../../theme';
37import { ANIMATED_SERIES_PROPS } from '../../utils/series-utils';
38import { warning } from '../../utils/react-utils';
39
40import AbstractSeries from './abstract-series';
41
42var predefinedClassName = 'rv-xy-plot__series rv-xy-plot__series--line';
43
44var STROKE_STYLES = {
45 dashed: '6, 2',
46 solid: null
47};
48
49var LineSeries = function (_AbstractSeries) {
50 _inherits(LineSeries, _AbstractSeries);
51
52 function LineSeries() {
53 _classCallCheck(this, LineSeries);
54
55 return _possibleConstructorReturn(this, (LineSeries.__proto__ || Object.getPrototypeOf(LineSeries)).apply(this, arguments));
56 }
57
58 _createClass(LineSeries, [{
59 key: '_renderLine',
60 value: function _renderLine(data, x, y, curve, getNull) {
61 var line = d3Shape.line();
62 if (curve !== null) {
63 if (typeof curve === 'string' && d3Shape[curve]) {
64 line = line.curve(d3Shape[curve]);
65 } else if (typeof curve === 'function') {
66 line = line.curve(curve);
67 }
68 }
69 line = line.defined(getNull);
70 line = line.x(x).y(y);
71 return line(data);
72 }
73 }, {
74 key: 'render',
75 value: function render() {
76 var _props = this.props,
77 animation = _props.animation,
78 className = _props.className,
79 data = _props.data;
80
81
82 if (this.props.nullAccessor) {
83 warning('nullAccessor has been renamed to getNull', true);
84 }
85
86 if (!data) {
87 return null;
88 }
89
90 if (animation) {
91 return React.createElement(
92 Animation,
93 _extends({}, this.props, { animatedProps: ANIMATED_SERIES_PROPS }),
94 React.createElement(LineSeries, _extends({}, this.props, { animation: null }))
95 );
96 }
97
98 var _props2 = this.props,
99 curve = _props2.curve,
100 marginLeft = _props2.marginLeft,
101 marginTop = _props2.marginTop,
102 strokeDasharray = _props2.strokeDasharray,
103 strokeStyle = _props2.strokeStyle,
104 strokeWidth = _props2.strokeWidth,
105 style = _props2.style;
106
107
108 var x = this._getAttributeFunctor('x');
109 var y = this._getAttributeFunctor('y');
110 var stroke = this._getAttributeValue('stroke') || this._getAttributeValue('color');
111 var newOpacity = this._getAttributeValue('opacity');
112 var opacity = Number.isFinite(newOpacity) ? newOpacity : DEFAULT_OPACITY;
113 var getNull = this.props.nullAccessor || this.props.getNull;
114 var d = this._renderLine(data, x, y, curve, getNull);
115
116 return React.createElement('path', {
117 d: d,
118 className: predefinedClassName + ' ' + className,
119 transform: 'translate(' + marginLeft + ',' + marginTop + ')',
120 onMouseOver: this._seriesMouseOverHandler,
121 onMouseOut: this._seriesMouseOutHandler,
122 onClick: this._seriesClickHandler,
123 onContextMenu: this._seriesRightClickHandler,
124 style: _extends({
125 opacity: opacity,
126 strokeDasharray: STROKE_STYLES[strokeStyle] || strokeDasharray,
127 strokeWidth: strokeWidth,
128 stroke: stroke
129 }, style)
130 });
131 }
132 }]);
133
134 return LineSeries;
135}(AbstractSeries);
136
137LineSeries.displayName = 'LineSeries';
138LineSeries.propTypes = _extends({}, AbstractSeries.propTypes, {
139 strokeStyle: PropTypes.oneOf(Object.keys(STROKE_STYLES)),
140 curve: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
141 getNull: PropTypes.func
142});
143LineSeries.defaultProps = _extends({}, AbstractSeries.defaultProps, {
144 strokeStyle: 'solid',
145 style: {},
146 opacity: 1,
147 curve: null,
148 className: '',
149 getNull: function getNull() {
150 return true;
151 }
152});
153
154export default LineSeries;
\No newline at end of file