Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

272 строки
10 KiB

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ANIMATED_SERIES_PROPS = undefined;
  6. 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; }; // Copyright (c) 2016 - 2017 Uber Technologies, Inc.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. exports.isSeriesChild = isSeriesChild;
  26. exports.getSeriesChildren = getSeriesChildren;
  27. exports.getStackedData = getStackedData;
  28. exports.getSeriesPropsFromChildren = getSeriesPropsFromChildren;
  29. exports.getRadialDomain = getRadialDomain;
  30. exports.getStackParams = getStackParams;
  31. var _react = require('react');
  32. var _react2 = _interopRequireDefault(_react);
  33. var _abstractSeries = require('../plot/series/abstract-series');
  34. var _abstractSeries2 = _interopRequireDefault(_abstractSeries);
  35. var _theme = require('../theme');
  36. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  37. function _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; }
  38. /**
  39. * Check if the component is series or not.
  40. * @param {React.Component} child Component.
  41. * @returns {boolean} True if the child is series, false otherwise.
  42. */
  43. function isSeriesChild(child) {
  44. var prototype = child.type.prototype;
  45. return prototype instanceof _abstractSeries2.default;
  46. }
  47. /**
  48. * Get all series from the 'children' object of the component.
  49. * @param {Object} children Children.
  50. * @returns {Array} Array of children.
  51. */
  52. function getSeriesChildren(children) {
  53. return _react2.default.Children.toArray(children).filter(function (child) {
  54. return child && isSeriesChild(child);
  55. });
  56. }
  57. /**
  58. * Collect the map of repetitions of the series type for all children.
  59. * @param {Array} children Array of children.
  60. * @returns {{}} Map of repetitions where sameTypeTotal is the total amount and
  61. * sameTypeIndex is always 0.
  62. */
  63. function collectSeriesTypesInfo(children) {
  64. var result = {};
  65. children.filter(isSeriesChild).forEach(function (child) {
  66. var displayName = child.type.displayName;
  67. var cluster = child.props.cluster;
  68. if (!result[displayName]) {
  69. result[displayName] = {
  70. sameTypeTotal: 0,
  71. sameTypeIndex: 0,
  72. clusters: new Set()
  73. };
  74. }
  75. result[displayName].clusters.add(cluster);
  76. result[displayName].sameTypeTotal++;
  77. });
  78. return result;
  79. }
  80. /**
  81. * Check series to see if it has angular data that needs to be converted
  82. * @param {Array} data - an array of objects to check
  83. * @returns {Boolean} whether or not this series contains polar configuration
  84. */
  85. function seriesHasAngleRadius() {
  86. var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  87. if (!data) {
  88. return false;
  89. }
  90. return data.some(function (row) {
  91. return row.radius && row.angle;
  92. });
  93. }
  94. /**
  95. * Possibly convert polar coordinates to x/y for computing domain
  96. * @param {Array} data - an array of objects to check
  97. * @param {String} attr - the property being checked
  98. * @returns {Boolean} whether or not this series contains polar configuration
  99. */
  100. function prepareData(data) {
  101. if (!seriesHasAngleRadius(data)) {
  102. return data;
  103. }
  104. return data.map(function (row) {
  105. return _extends({}, row, {
  106. x: row.radius * Math.cos(row.angle),
  107. y: row.radius * Math.sin(row.angle)
  108. });
  109. });
  110. }
  111. /**
  112. * Collect the stacked data for all children in use. If the children don't have
  113. * the data (e.g. the child is invalid series or something else), then the child
  114. * is skipped.
  115. * Each next value of attr is equal to the previous value plus the difference
  116. * between attr0 and attr.
  117. * @param {Array} children Array of children.
  118. * @param {string} attr Attribute to stack by.
  119. * @returns {Array} New array of children for the series.
  120. */
  121. function getStackedData(children, attr) {
  122. var areSomeSeriesStacked = children.some(function (series) {
  123. return series && series.props.stack;
  124. });
  125. // It stores the last segment position added to each bar, separated by cluster.
  126. var latestAttrPositions = {};
  127. return children.reduce(function (accumulator, series, seriesIndex) {
  128. // Skip the children that are not series (e.g. don't have any data).
  129. if (!series) {
  130. accumulator.push(null);
  131. return accumulator;
  132. }
  133. var seriesType = series.type.displayName;
  134. var _series$props = series.props,
  135. data = _series$props.data,
  136. _series$props$cluster = _series$props.cluster,
  137. cluster = _series$props$cluster === undefined ? 'default' : _series$props$cluster,
  138. stack = _series$props.stack;
  139. var preppedData = prepareData(data, attr);
  140. if (!attr || !preppedData || !preppedData.length || areSomeSeriesStacked && !stack) {
  141. accumulator.push(preppedData);
  142. return accumulator;
  143. }
  144. var attr0 = attr + '0';
  145. var baseAttr = attr === 'y' ? 'x' : 'y';
  146. accumulator.push(preppedData.map(function (d, dIndex) {
  147. var _extends2, _latestAttrPositions$2;
  148. if (!latestAttrPositions[cluster]) {
  149. latestAttrPositions[cluster] = {};
  150. }
  151. if (!latestAttrPositions[cluster][seriesType]) {
  152. latestAttrPositions[cluster][seriesType] = {};
  153. }
  154. var prevD = latestAttrPositions[cluster][seriesType][d[baseAttr]];
  155. // It is the first segment of a bar.
  156. if (!prevD) {
  157. var _latestAttrPositions$;
  158. latestAttrPositions[cluster][seriesType][d[baseAttr]] = (_latestAttrPositions$ = {}, _defineProperty(_latestAttrPositions$, attr0, d[attr0]), _defineProperty(_latestAttrPositions$, attr, d[attr]), _latestAttrPositions$);
  159. return _extends({}, d);
  160. }
  161. // Calculate the position of the next segment in a bar.
  162. var nextD = _extends({}, d, (_extends2 = {}, _defineProperty(_extends2, attr0, prevD[attr]), _defineProperty(_extends2, attr, prevD[attr] + d[attr] - (d[attr0] || 0)), _extends2));
  163. latestAttrPositions[cluster][seriesType][d[baseAttr]] = (_latestAttrPositions$2 = {}, _defineProperty(_latestAttrPositions$2, attr0, nextD[attr0]), _defineProperty(_latestAttrPositions$2, attr, nextD[attr]), _latestAttrPositions$2);
  164. return nextD;
  165. }));
  166. return accumulator;
  167. }, []);
  168. }
  169. /**
  170. * Get the list of series props for a child.
  171. * @param {Array} children Array of all children.
  172. * @returns {Array} Array of series props for each child. If a child is not a
  173. * series, than it's undefined.
  174. */
  175. function getSeriesPropsFromChildren(children) {
  176. var result = [];
  177. var seriesTypesInfo = collectSeriesTypesInfo(children);
  178. var seriesIndex = 0;
  179. var _opacityValue = _theme.DEFAULT_OPACITY;
  180. children.forEach(function (child) {
  181. var props = void 0;
  182. if (isSeriesChild(child)) {
  183. var seriesTypeInfo = seriesTypesInfo[child.type.displayName];
  184. var _colorValue = _theme.DISCRETE_COLOR_RANGE[seriesIndex % _theme.DISCRETE_COLOR_RANGE.length];
  185. props = _extends({}, seriesTypeInfo, {
  186. seriesIndex: seriesIndex,
  187. _colorValue: _colorValue,
  188. _opacityValue: _opacityValue
  189. });
  190. seriesTypeInfo.sameTypeIndex++;
  191. seriesIndex++;
  192. if (child.props.cluster) {
  193. props.cluster = child.props.cluster;
  194. // Using Array.from() so we can use .indexOf
  195. props.clusters = Array.from(seriesTypeInfo.clusters);
  196. props.sameTypeTotal = props.clusters.length;
  197. props.sameTypeIndex = props.clusters.indexOf(child.props.cluster);
  198. }
  199. }
  200. result.push(props);
  201. });
  202. return result;
  203. }
  204. /**
  205. * Find the max radius value from the nodes to be rendered after they have been
  206. * transformed into an array
  207. * @param {Array} data - the tree data after it has been broken into a iterable
  208. * it is an array of objects!
  209. * @returns {number} the maximum value in coordinates for the radial variable
  210. */
  211. function getRadialDomain(data) {
  212. return data.reduce(function (res, row) {
  213. return Math.max(row.radius, res);
  214. }, 0);
  215. }
  216. var ANIMATED_SERIES_PROPS = exports.ANIMATED_SERIES_PROPS = ['xRange', 'xDomain', 'x', 'yRange', 'yDomain', 'y', 'colorRange', 'colorDomain', 'color', 'opacityRange', 'opacityDomain', 'opacity', 'strokeRange', 'strokeDomain', 'stroke', 'fillRange', 'fillDomain', 'fill', 'width', 'height', 'marginLeft', 'marginTop', 'marginRight', 'marginBottom', 'data', 'angleDomain', 'angleRange', 'angle', 'radiusDomain', 'radiusRange', 'radius', 'innerRadiusDomain', 'innerRadiusRange', 'innerRadius'];
  217. function getStackParams(props) {
  218. var _stackBy = props._stackBy,
  219. valuePosAttr = props.valuePosAttr,
  220. cluster = props.cluster;
  221. var _props$sameTypeTotal = props.sameTypeTotal,
  222. sameTypeTotal = _props$sameTypeTotal === undefined ? 1 : _props$sameTypeTotal,
  223. _props$sameTypeIndex = props.sameTypeIndex,
  224. sameTypeIndex = _props$sameTypeIndex === undefined ? 0 : _props$sameTypeIndex;
  225. // If bars are stacked, but not clustering, override `sameTypeTotal` and
  226. // `sameTypeIndex` such that bars are stacked and not staggered.
  227. if (_stackBy === valuePosAttr && !cluster) {
  228. sameTypeTotal = 1;
  229. sameTypeIndex = 0;
  230. }
  231. return { sameTypeTotal: sameTypeTotal, sameTypeIndex: sameTypeIndex };
  232. }