UNPKG

3.47 kBJavaScriptView Raw
1import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3import _createClass from "@babel/runtime/helpers/esm/createClass";
4import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
5import _inherits from "@babel/runtime/helpers/esm/inherits";
6import _createSuper from "@babel/runtime/helpers/esm/createSuper";
7import { Category } from '../../attr';
8import Component from '../../base/component';
9import CoordController from '../../controller/coord';
10import { hierarchy, treemap, treemapBinary } from '../../deps/d3-hierarchy/src';
11import { jsx } from '../../jsx';
12export default (function (View) {
13 return /*#__PURE__*/function (_Component) {
14 _inherits(Treemap, _Component);
15
16 var _super = _createSuper(Treemap);
17
18 function Treemap(props, context, updater) {
19 var _this;
20
21 _classCallCheck(this, Treemap);
22
23 _this = _super.call(this, props, context, updater);
24 var coord = props.coord,
25 color = props.color,
26 data = props.data;
27 var width = context.width,
28 height = context.height,
29 theme = context.theme;
30 _this.coordController = new CoordController();
31
32 var _assertThisInitialize = _assertThisInitialized(_this),
33 coordController = _assertThisInitialize.coordController;
34
35 _this.coord = coordController.create(coord, {
36 width: width,
37 height: height
38 });
39 _this.color = new Category(_objectSpread(_objectSpread({
40 range: theme.colors
41 }, color), {}, {
42 data: data
43 }));
44 return _this;
45 }
46
47 _createClass(Treemap, [{
48 key: "treemapLayout",
49 value: function treemapLayout() {
50 var props = this.props,
51 coord = this.coord,
52 colorAttr = this.color;
53 var data = props.data,
54 value = props.value;
55 var root = hierarchy({
56 children: data
57 }).sum(function (d) {
58 return d[value];
59 }).sort(function (a, b) {
60 return b[value] - a[value];
61 });
62 var treemapLayout = treemap() // 默认treemapSquarify
63 .tile(treemapBinary) // .size([1, 1])
64 // @ts-ignore
65 .round(false); // .padding(space)
66 // .paddingInner(space);
67 // .paddingOuter(options.paddingOuter)
68 // .paddingTop(options.paddingTop)
69 // .paddingRight(options.paddingRight)
70 // .paddingBottom(options.paddingBottom)
71 // .paddingLeft(options.paddingLeft);
72
73 var nodes = treemapLayout(root);
74 return nodes.children.map(function (item) {
75 var data = item.data,
76 x0 = item.x0,
77 y0 = item.y0,
78 x1 = item.x1,
79 y1 = item.y1;
80 var color = colorAttr.mapping(data[colorAttr.field]);
81 var rect = coord.convertRect({
82 x: [x0, x1],
83 y: [y0, y1]
84 });
85 return _objectSpread({
86 key: data.key,
87 origin: data,
88 color: color
89 }, rect);
90 });
91 }
92 }, {
93 key: "render",
94 value: function render() {
95 var nodes = this.treemapLayout();
96 var props = this.props,
97 coord = this.coord;
98 return jsx(View, _objectSpread(_objectSpread({
99 nodes: nodes
100 }, props), {}, {
101 coord: coord
102 }));
103 }
104 }]);
105
106 return Treemap;
107 }(Component);
108});
\No newline at end of file