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

234 строки
8.8 KiB

  1. var _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. 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; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _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; }
  5. function _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; }
  6. // 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. import React from 'react';
  26. import PropTypes from 'prop-types';
  27. import { hierarchy, pack, partition, treemapSquarify, treemapResquarify, treemapSlice, treemapDice, treemapSliceDice, treemapBinary, treemap } from 'd3-hierarchy';
  28. import { CONTINUOUS_COLOR_RANGE, DEFAULT_COLOR, DEFAULT_OPACITY, OPACITY_TYPE } from '../theme';
  29. import { AnimationPropType } from '../animation';
  30. import { getAttributeFunctor, getMissingScaleProps } from '../utils/scales-utils';
  31. import { MarginPropType, getInnerDimensions } from '../utils/chart-utils';
  32. import TreemapDOM from './treemap-dom';
  33. import TreemapSVG from './treemap-svg';
  34. var TREEMAP_TILE_MODES = {
  35. squarify: treemapSquarify,
  36. resquarify: treemapResquarify,
  37. slice: treemapSlice,
  38. dice: treemapDice,
  39. slicedice: treemapSliceDice,
  40. binary: treemapBinary
  41. };
  42. var TREEMAP_LAYOUT_MODES = ['circlePack', 'partition', 'partition-pivot'];
  43. var NOOP = function NOOP(d) {
  44. return d;
  45. };
  46. var ATTRIBUTES = ['opacity', 'color'];
  47. var DEFAULT_MARGINS = {
  48. left: 40,
  49. right: 10,
  50. top: 10,
  51. bottom: 40
  52. };
  53. /**
  54. * Get the map of scale functions from the given props.
  55. * @param {Object} props Props for the component.
  56. * @returns {Object} Map of scale functions.
  57. * @private
  58. */
  59. function _getScaleFns(props) {
  60. var data = props.data;
  61. var allData = data.children || [];
  62. // Adding _allData property to the object to reuse the existing
  63. // getAttributeFunctor function.
  64. var compatibleProps = _extends({}, props, getMissingScaleProps(props, allData, ATTRIBUTES), {
  65. _allData: allData
  66. });
  67. return {
  68. opacity: getAttributeFunctor(compatibleProps, 'opacity'),
  69. color: getAttributeFunctor(compatibleProps, 'color')
  70. };
  71. }
  72. var Treemap = function (_React$Component) {
  73. _inherits(Treemap, _React$Component);
  74. function Treemap(props) {
  75. _classCallCheck(this, Treemap);
  76. var _this = _possibleConstructorReturn(this, (Treemap.__proto__ || Object.getPrototypeOf(Treemap)).call(this, props));
  77. _this.state = _extends({
  78. scales: _getScaleFns(props)
  79. }, getInnerDimensions(props, props.margin));
  80. return _this;
  81. }
  82. _createClass(Treemap, [{
  83. key: 'componentWillReceiveProps',
  84. value: function componentWillReceiveProps(props) {
  85. this.setState(_extends({
  86. scales: _getScaleFns(props)
  87. }, getInnerDimensions(props, props.margin)));
  88. }
  89. /**
  90. * Create the list of nodes to render.
  91. * @returns {Array} Array of nodes.
  92. * @private
  93. */
  94. }, {
  95. key: '_getNodesToRender',
  96. value: function _getNodesToRender() {
  97. var _state = this.state,
  98. innerWidth = _state.innerWidth,
  99. innerHeight = _state.innerHeight;
  100. var _props = this.props,
  101. data = _props.data,
  102. mode = _props.mode,
  103. padding = _props.padding,
  104. sortFunction = _props.sortFunction,
  105. getSize = _props.getSize;
  106. if (!data) {
  107. return [];
  108. }
  109. if (mode === 'partition' || mode === 'partition-pivot') {
  110. var partitionFunction = partition().size(mode === 'partition-pivot' ? [innerHeight, innerWidth] : [innerWidth, innerHeight]).padding(padding);
  111. var _structuredInput = hierarchy(data).sum(getSize).sort(function (a, b) {
  112. return sortFunction(a, b, getSize);
  113. });
  114. var mappedNodes = partitionFunction(_structuredInput).descendants();
  115. if (mode === 'partition-pivot') {
  116. return mappedNodes.map(function (node) {
  117. return _extends({}, node, {
  118. x0: node.y0,
  119. x1: node.y1,
  120. y0: node.x0,
  121. y1: node.x1
  122. });
  123. });
  124. }
  125. return mappedNodes;
  126. }
  127. if (mode === 'circlePack') {
  128. var packingFunction = pack().size([innerWidth, innerHeight]).padding(padding);
  129. var _structuredInput2 = hierarchy(data).sum(getSize).sort(function (a, b) {
  130. return sortFunction(a, b, getSize);
  131. });
  132. return packingFunction(_structuredInput2).descendants();
  133. }
  134. var tileFn = TREEMAP_TILE_MODES[mode];
  135. var treemapingFunction = treemap(tileFn).tile(tileFn).size([innerWidth, innerHeight]).padding(padding);
  136. var structuredInput = hierarchy(data).sum(getSize).sort(function (a, b) {
  137. return sortFunction(a, b, getSize);
  138. });
  139. return treemapingFunction(structuredInput).descendants();
  140. }
  141. }, {
  142. key: 'render',
  143. value: function render() {
  144. var renderMode = this.props.renderMode;
  145. var scales = this.state.scales;
  146. var nodes = this._getNodesToRender();
  147. var TreemapElement = renderMode === 'SVG' ? TreemapSVG : TreemapDOM;
  148. return React.createElement(TreemapElement, _extends({}, this.props, { nodes: nodes, scales: scales }));
  149. }
  150. }]);
  151. return Treemap;
  152. }(React.Component);
  153. Treemap.displayName = 'Treemap';
  154. Treemap.propTypes = {
  155. animation: AnimationPropType,
  156. className: PropTypes.string,
  157. data: PropTypes.object.isRequired,
  158. height: PropTypes.number.isRequired,
  159. hideRootNode: PropTypes.bool,
  160. margin: MarginPropType,
  161. mode: PropTypes.oneOf(Object.keys(TREEMAP_TILE_MODES).concat(TREEMAP_LAYOUT_MODES)),
  162. onLeafClick: PropTypes.func,
  163. onLeafMouseOver: PropTypes.func,
  164. onLeafMouseOut: PropTypes.func,
  165. useCirclePacking: PropTypes.bool,
  166. padding: PropTypes.number.isRequired,
  167. sortFunction: PropTypes.func,
  168. width: PropTypes.number.isRequired,
  169. getSize: PropTypes.func,
  170. getColor: PropTypes.func
  171. };
  172. Treemap.defaultProps = {
  173. className: '',
  174. colorRange: CONTINUOUS_COLOR_RANGE,
  175. _colorValue: DEFAULT_COLOR,
  176. data: {
  177. children: []
  178. },
  179. hideRootNode: false,
  180. margin: DEFAULT_MARGINS,
  181. mode: 'squarify',
  182. onLeafClick: NOOP,
  183. onLeafMouseOver: NOOP,
  184. onLeafMouseOut: NOOP,
  185. opacityType: OPACITY_TYPE,
  186. _opacityValue: DEFAULT_OPACITY,
  187. padding: 1,
  188. sortFunction: function sortFunction(a, b, accessor) {
  189. if (!accessor) {
  190. return 0;
  191. }
  192. return accessor(a) - accessor(b);
  193. },
  194. getSize: function getSize(d) {
  195. return d.size;
  196. },
  197. getColor: function getColor(d) {
  198. return d.color;
  199. },
  200. getLabel: function getLabel(d) {
  201. return d.title;
  202. }
  203. };
  204. export default Treemap;