UNPKG

@antv/f2

Version:

Charts for mobile visualization.

144 lines (143 loc) 5.05 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper")); var _jsx = require("../../jsx"); var _component = _interopRequireDefault(require("../../base/component")); var _src = require("../../deps/d3-hierarchy/src"); var _attr = require("../../attr"); var _util = require("../../util"); var _coord = _interopRequireDefault(require("../../controller/coord")); var _util2 = require("@antv/util"); function rootParent(data) { var d = data; while (d.depth > 1) { d = d.parent; } return d; } var _default = function _default(View) { return /*#__PURE__*/function (_Component) { (0, _inherits2.default)(Sunburst, _Component); var _super = (0, _createSuper2.default)(Sunburst); function Sunburst(props, context) { var _this; (0, _classCallCheck2.default)(this, Sunburst); _this = _super.call(this, props, context); var coord = props.coord, color = props.color, data = props.data; var width = context.width, height = context.height, theme = context.theme; _this.coordController = new _coord.default(); var _assertThisInitialize = (0, _assertThisInitialized2.default)(_this), coordController = _assertThisInitialize.coordController; _this.coord = coordController.create(coord, { width: width, height: height }); _this.color = new _attr.Category((0, _objectSpread2.default)((0, _objectSpread2.default)({ range: theme.colors }, color), {}, { data: data })); return _this; } (0, _createClass2.default)(Sunburst, [{ key: "didMount", value: function didMount() { var _this2 = this; var props = this.props, container = this.container; var onClick = props.onClick; var canvas = container.get('canvas'); this.triggerRef = []; canvas.on('click', function (ev) { var points = ev.points; var shape = _this2.triggerRef.find(function (ref) { return (0, _util.isInBBox)(ref.current.getBBox(), points[0]); }); if (shape) { ev.shape = shape; // @ts-ignore ev.payload = shape.payload; onClick && onClick(ev); } }); } }, { key: "_mapping", value: function _mapping(children) { var colorAttr = this.color, coord = this.coord; for (var i = 0, len = children.length; i < len; i++) { var node = children[i]; var root = rootParent(node); var color = colorAttr.mapping(root.data[colorAttr.field]); node.color = color; var x0 = node.x0, x1 = node.x1, y0 = node.y0, y1 = node.y1; var rect = coord.convertRect({ x: [x0, x1], y: [y0, y1] }); (0, _util2.mix)(node, rect); // 递归处理 if (node.children && node.children.length) { this._mapping(node.children); } } } }, { key: "sunburst", value: function sunburst() { var props = this.props; var data = props.data, value = props.value, _props$sort = props.sort, sort = _props$sort === void 0 ? true : _props$sort; var root = (0, _src.hierarchy)({ children: data }).sum(function (d) { return d[value]; }); // 内置按value大小顺序排序,支持传入sort函数 if (sort === true || (0, _util2.isFunction)(sort)) { var sortFn = (0, _util2.isFunction)(sort) ? sort : function (a, b) { return b[value] - a[value]; }; root.sort(sortFn); } var nodes = (0, _src.partition)()(root); var children = nodes.children; this._mapping(children); return nodes; } }, { key: "render", value: function render() { var node = this.sunburst(); var coord = this.coord, props = this.props; return (0, _jsx.jsx)(View, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, { coord: coord, node: node, triggerRef: this.triggerRef })); } }]); return Sunburst; }(_component.default); }; exports.default = _default;