UNPKG

8.28 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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
7function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
9function _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; }
10
11function _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; }
12
13// Copyright (c) 2016 - 2017 Uber Technologies, Inc.
14//
15// Permission is hereby granted, free of charge, to any person obtaining a copy
16// of this software and associated documentation files (the "Software"), to deal
17// in the Software without restriction, including without limitation the rights
18// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19// copies of the Software, and to permit persons to whom the Software is
20// furnished to do so, subject to the following conditions:
21//
22// The above copyright notice and this permission notice shall be included in
23// all copies or substantial portions of the Software.
24//
25// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31// THE SOFTWARE.
32
33import React from 'react';
34
35import PropTypes from 'prop-types';
36
37import Animation from '../../animation';
38import { ANIMATED_SERIES_PROPS, getStackParams } from '../../utils/series-utils';
39
40import AbstractSeries from './abstract-series';
41
42var predefinedClassName = 'rv-xy-plot__series rv-xy-plot__series--bar';
43
44var BarSeries = function (_AbstractSeries) {
45 _inherits(BarSeries, _AbstractSeries);
46
47 function BarSeries() {
48 _classCallCheck(this, BarSeries);
49
50 return _possibleConstructorReturn(this, (BarSeries.__proto__ || Object.getPrototypeOf(BarSeries)).apply(this, arguments));
51 }
52
53 _createClass(BarSeries, [{
54 key: 'render',
55 value: function render() {
56 var _this2 = this;
57
58 var _props = this.props,
59 animation = _props.animation,
60 className = _props.className,
61 data = _props.data,
62 linePosAttr = _props.linePosAttr,
63 lineSizeAttr = _props.lineSizeAttr,
64 marginLeft = _props.marginLeft,
65 marginTop = _props.marginTop,
66 style = _props.style,
67 valuePosAttr = _props.valuePosAttr,
68 valueSizeAttr = _props.valueSizeAttr,
69 barWidth = _props.barWidth;
70
71
72 if (!data) {
73 return null;
74 }
75
76 if (animation) {
77 return React.createElement(
78 Animation,
79 _extends({}, this.props, { animatedProps: ANIMATED_SERIES_PROPS }),
80 React.createElement(BarSeries, _extends({}, this.props, { animation: null }))
81 );
82 }
83
84 var _getStackParams = getStackParams(this.props),
85 sameTypeTotal = _getStackParams.sameTypeTotal,
86 sameTypeIndex = _getStackParams.sameTypeIndex;
87
88 var distance = this._getScaleDistance(linePosAttr);
89 var lineFunctor = this._getAttributeFunctor(linePosAttr);
90 var valueFunctor = this._getAttributeFunctor(valuePosAttr);
91 var value0Functor = this._getAttr0Functor(valuePosAttr);
92 var fillFunctor = this._getAttributeFunctor('fill') || this._getAttributeFunctor('color');
93 var strokeFunctor = this._getAttributeFunctor('stroke') || this._getAttributeFunctor('color');
94 var opacityFunctor = this._getAttributeFunctor('opacity');
95
96 var halfSpace = distance / 2 * barWidth;
97
98 return React.createElement(
99 'g',
100 {
101 className: predefinedClassName + ' ' + className,
102 transform: 'translate(' + marginLeft + ',' + marginTop + ')'
103 },
104 data.map(function (d, i) {
105 var _attrs;
106
107 // totalSpaceAvailable is the space we have available to draw all the
108 // bars of a same 'linePosAttr' value (a.k.a. sameTypeTotal)
109 var totalSpaceAvailable = halfSpace * 2;
110 var totalSpaceCenter = lineFunctor(d);
111 // totalSpaceStartingPoint is the first pixel were we can start drawing
112 var totalSpaceStartingPoint = totalSpaceCenter - halfSpace;
113 // spaceTakenByInterBarsPixels has the overhead space consumed by each bar of sameTypeTotal
114 var spaceTakenByInterBarsPixels = (sameTypeTotal - 1) / sameTypeTotal;
115 // spacePerBar is the space we have available to draw sameTypeIndex bar
116 var spacePerBar = totalSpaceAvailable / sameTypeTotal - spaceTakenByInterBarsPixels;
117 // barStartingPoint is the first pixel were we can start drawing sameTypeIndex bar
118 var barStartingPoint = totalSpaceStartingPoint + spacePerBar * sameTypeIndex + sameTypeIndex;
119
120 var attrs = (_attrs = {
121 style: _extends({
122 opacity: opacityFunctor && opacityFunctor(d),
123 stroke: strokeFunctor && strokeFunctor(d),
124 fill: fillFunctor && fillFunctor(d)
125 }, style)
126 }, _defineProperty(_attrs, linePosAttr, barStartingPoint), _defineProperty(_attrs, lineSizeAttr, spacePerBar), _defineProperty(_attrs, valuePosAttr, Math.min(value0Functor(d), valueFunctor(d))), _defineProperty(_attrs, valueSizeAttr, Math.abs(-value0Functor(d) + valueFunctor(d))), _defineProperty(_attrs, 'onClick', function onClick(e) {
127 return _this2._valueClickHandler(d, e);
128 }), _defineProperty(_attrs, 'onContextMenu', function onContextMenu(e) {
129 return _this2._valueRightClickHandler(d, e);
130 }), _defineProperty(_attrs, 'onMouseOver', function onMouseOver(e) {
131 return _this2._valueMouseOverHandler(d, e);
132 }), _defineProperty(_attrs, 'onMouseOut', function onMouseOut(e) {
133 return _this2._valueMouseOutHandler(d, e);
134 }), _defineProperty(_attrs, 'key', i), _attrs);
135 return React.createElement('rect', attrs);
136 })
137 );
138 }
139 }], [{
140 key: 'propTypes',
141 get: function get() {
142 return _extends({}, AbstractSeries.propTypes, {
143 linePosAttr: PropTypes.string,
144 valuePosAttr: PropTypes.string,
145 lineSizeAttr: PropTypes.string,
146 valueSizeAttr: PropTypes.string,
147 cluster: PropTypes.string,
148 barWidth: PropTypes.number
149 });
150 }
151 }, {
152 key: 'defaultProps',
153 get: function get() {
154 return {
155 barWidth: 0.85
156 };
157 }
158 }]);
159
160 return BarSeries;
161}(AbstractSeries);
162
163BarSeries.displayName = 'BarSeries';
164
165export default BarSeries;
\No newline at end of file