treemapLayout.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. /*
  41. * A third-party license is embedded for some of the code in this file:
  42. * The treemap layout implementation was originally copied from
  43. * "d3.js" with some modifications made for this project.
  44. * (See more details in the comment of the method "squarify" below.)
  45. * The use of the source code of this file is also subject to the terms
  46. * and consitions of the license of "d3.js" (BSD-3Clause, see
  47. * </licenses/LICENSE-d3>).
  48. */
  49. import * as zrUtil from 'zrender/lib/core/util.js';
  50. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  51. import { parsePercent, MAX_SAFE_INTEGER } from '../../util/number.js';
  52. import * as layout from '../../util/layout.js';
  53. import * as helper from '../helper/treeHelper.js';
  54. var mathMax = Math.max;
  55. var mathMin = Math.min;
  56. var retrieveValue = zrUtil.retrieve;
  57. var each = zrUtil.each;
  58. var PATH_BORDER_WIDTH = ['itemStyle', 'borderWidth'];
  59. var PATH_GAP_WIDTH = ['itemStyle', 'gapWidth'];
  60. var PATH_UPPER_LABEL_SHOW = ['upperLabel', 'show'];
  61. var PATH_UPPER_LABEL_HEIGHT = ['upperLabel', 'height'];
  62. ;
  63. /**
  64. * @public
  65. */
  66. export default {
  67. seriesType: 'treemap',
  68. reset: function (seriesModel, ecModel, api, payload) {
  69. // Layout result in each node:
  70. // {x, y, width, height, area, borderWidth}
  71. var ecWidth = api.getWidth();
  72. var ecHeight = api.getHeight();
  73. var seriesOption = seriesModel.option;
  74. var layoutInfo = layout.getLayoutRect(seriesModel.getBoxLayoutParams(), {
  75. width: api.getWidth(),
  76. height: api.getHeight()
  77. });
  78. var size = seriesOption.size || []; // Compatible with ec2.
  79. var containerWidth = parsePercent(retrieveValue(layoutInfo.width, size[0]), ecWidth);
  80. var containerHeight = parsePercent(retrieveValue(layoutInfo.height, size[1]), ecHeight);
  81. // Fetch payload info.
  82. var payloadType = payload && payload.type;
  83. var types = ['treemapZoomToNode', 'treemapRootToNode'];
  84. var targetInfo = helper.retrieveTargetInfo(payload, types, seriesModel);
  85. var rootRect = payloadType === 'treemapRender' || payloadType === 'treemapMove' ? payload.rootRect : null;
  86. var viewRoot = seriesModel.getViewRoot();
  87. var viewAbovePath = helper.getPathToRoot(viewRoot);
  88. if (payloadType !== 'treemapMove') {
  89. var rootSize = payloadType === 'treemapZoomToNode' ? estimateRootSize(seriesModel, targetInfo, viewRoot, containerWidth, containerHeight) : rootRect ? [rootRect.width, rootRect.height] : [containerWidth, containerHeight];
  90. var sort_1 = seriesOption.sort;
  91. if (sort_1 && sort_1 !== 'asc' && sort_1 !== 'desc') {
  92. // Default to be desc order.
  93. sort_1 = 'desc';
  94. }
  95. var options = {
  96. squareRatio: seriesOption.squareRatio,
  97. sort: sort_1,
  98. leafDepth: seriesOption.leafDepth
  99. };
  100. // layout should be cleared because using updateView but not update.
  101. viewRoot.hostTree.clearLayouts();
  102. // TODO
  103. // optimize: if out of view clip, do not layout.
  104. // But take care that if do not render node out of view clip,
  105. // how to calculate start po
  106. var viewRootLayout_1 = {
  107. x: 0,
  108. y: 0,
  109. width: rootSize[0],
  110. height: rootSize[1],
  111. area: rootSize[0] * rootSize[1]
  112. };
  113. viewRoot.setLayout(viewRootLayout_1);
  114. squarify(viewRoot, options, false, 0);
  115. // Supplement layout.
  116. viewRootLayout_1 = viewRoot.getLayout();
  117. each(viewAbovePath, function (node, index) {
  118. var childValue = (viewAbovePath[index + 1] || viewRoot).getValue();
  119. node.setLayout(zrUtil.extend({
  120. dataExtent: [childValue, childValue],
  121. borderWidth: 0,
  122. upperHeight: 0
  123. }, viewRootLayout_1));
  124. });
  125. }
  126. var treeRoot = seriesModel.getData().tree.root;
  127. treeRoot.setLayout(calculateRootPosition(layoutInfo, rootRect, targetInfo), true);
  128. seriesModel.setLayoutInfo(layoutInfo);
  129. // FIXME
  130. // 现在没有clip功能,暂时取ec高宽。
  131. prunning(treeRoot,
  132. // Transform to base element coordinate system.
  133. new BoundingRect(-layoutInfo.x, -layoutInfo.y, ecWidth, ecHeight), viewAbovePath, viewRoot, 0);
  134. }
  135. };
  136. /**
  137. * Layout treemap with squarify algorithm.
  138. * The original presentation of this algorithm
  139. * was made by Mark Bruls, Kees Huizing, and Jarke J. van Wijk
  140. * <https://graphics.ethz.ch/teaching/scivis_common/Literature/squarifiedTreeMaps.pdf>.
  141. * The implementation of this algorithm was originally copied from "d3.js"
  142. * <https://github.com/d3/d3/blob/9cc9a875e636a1dcf36cc1e07bdf77e1ad6e2c74/src/layout/treemap.js>
  143. * with some modifications made for this program.
  144. * See the license statement at the head of this file.
  145. *
  146. * @protected
  147. * @param {module:echarts/data/Tree~TreeNode} node
  148. * @param {Object} options
  149. * @param {string} options.sort 'asc' or 'desc'
  150. * @param {number} options.squareRatio
  151. * @param {boolean} hideChildren
  152. * @param {number} depth
  153. */
  154. function squarify(node, options, hideChildren, depth) {
  155. var width;
  156. var height;
  157. if (node.isRemoved()) {
  158. return;
  159. }
  160. var thisLayout = node.getLayout();
  161. width = thisLayout.width;
  162. height = thisLayout.height;
  163. // Considering border and gap
  164. var nodeModel = node.getModel();
  165. var borderWidth = nodeModel.get(PATH_BORDER_WIDTH);
  166. var halfGapWidth = nodeModel.get(PATH_GAP_WIDTH) / 2;
  167. var upperLabelHeight = getUpperLabelHeight(nodeModel);
  168. var upperHeight = Math.max(borderWidth, upperLabelHeight);
  169. var layoutOffset = borderWidth - halfGapWidth;
  170. var layoutOffsetUpper = upperHeight - halfGapWidth;
  171. node.setLayout({
  172. borderWidth: borderWidth,
  173. upperHeight: upperHeight,
  174. upperLabelHeight: upperLabelHeight
  175. }, true);
  176. width = mathMax(width - 2 * layoutOffset, 0);
  177. height = mathMax(height - layoutOffset - layoutOffsetUpper, 0);
  178. var totalArea = width * height;
  179. var viewChildren = initChildren(node, nodeModel, totalArea, options, hideChildren, depth);
  180. if (!viewChildren.length) {
  181. return;
  182. }
  183. var rect = {
  184. x: layoutOffset,
  185. y: layoutOffsetUpper,
  186. width: width,
  187. height: height
  188. };
  189. var rowFixedLength = mathMin(width, height);
  190. var best = Infinity; // the best row score so far
  191. var row = [];
  192. row.area = 0;
  193. for (var i = 0, len = viewChildren.length; i < len;) {
  194. var child = viewChildren[i];
  195. row.push(child);
  196. row.area += child.getLayout().area;
  197. var score = worst(row, rowFixedLength, options.squareRatio);
  198. // continue with this orientation
  199. if (score <= best) {
  200. i++;
  201. best = score;
  202. }
  203. // abort, and try a different orientation
  204. else {
  205. row.area -= row.pop().getLayout().area;
  206. position(row, rowFixedLength, rect, halfGapWidth, false);
  207. rowFixedLength = mathMin(rect.width, rect.height);
  208. row.length = row.area = 0;
  209. best = Infinity;
  210. }
  211. }
  212. if (row.length) {
  213. position(row, rowFixedLength, rect, halfGapWidth, true);
  214. }
  215. if (!hideChildren) {
  216. var childrenVisibleMin = nodeModel.get('childrenVisibleMin');
  217. if (childrenVisibleMin != null && totalArea < childrenVisibleMin) {
  218. hideChildren = true;
  219. }
  220. }
  221. for (var i = 0, len = viewChildren.length; i < len; i++) {
  222. squarify(viewChildren[i], options, hideChildren, depth + 1);
  223. }
  224. }
  225. /**
  226. * Set area to each child, and calculate data extent for visual coding.
  227. */
  228. function initChildren(node, nodeModel, totalArea, options, hideChildren, depth) {
  229. var viewChildren = node.children || [];
  230. var orderBy = options.sort;
  231. orderBy !== 'asc' && orderBy !== 'desc' && (orderBy = null);
  232. var overLeafDepth = options.leafDepth != null && options.leafDepth <= depth;
  233. // leafDepth has higher priority.
  234. if (hideChildren && !overLeafDepth) {
  235. return node.viewChildren = [];
  236. }
  237. // Sort children, order by desc.
  238. viewChildren = zrUtil.filter(viewChildren, function (child) {
  239. return !child.isRemoved();
  240. });
  241. sort(viewChildren, orderBy);
  242. var info = statistic(nodeModel, viewChildren, orderBy);
  243. if (info.sum === 0) {
  244. return node.viewChildren = [];
  245. }
  246. info.sum = filterByThreshold(nodeModel, totalArea, info.sum, orderBy, viewChildren);
  247. if (info.sum === 0) {
  248. return node.viewChildren = [];
  249. }
  250. // Set area to each child.
  251. for (var i = 0, len = viewChildren.length; i < len; i++) {
  252. var area = viewChildren[i].getValue() / info.sum * totalArea;
  253. // Do not use setLayout({...}, true), because it is needed to clear last layout.
  254. viewChildren[i].setLayout({
  255. area: area
  256. });
  257. }
  258. if (overLeafDepth) {
  259. viewChildren.length && node.setLayout({
  260. isLeafRoot: true
  261. }, true);
  262. viewChildren.length = 0;
  263. }
  264. node.viewChildren = viewChildren;
  265. node.setLayout({
  266. dataExtent: info.dataExtent
  267. }, true);
  268. return viewChildren;
  269. }
  270. /**
  271. * Consider 'visibleMin'. Modify viewChildren and get new sum.
  272. */
  273. function filterByThreshold(nodeModel, totalArea, sum, orderBy, orderedChildren) {
  274. // visibleMin is not supported yet when no option.sort.
  275. if (!orderBy) {
  276. return sum;
  277. }
  278. var visibleMin = nodeModel.get('visibleMin');
  279. var len = orderedChildren.length;
  280. var deletePoint = len;
  281. // Always travel from little value to big value.
  282. for (var i = len - 1; i >= 0; i--) {
  283. var value = orderedChildren[orderBy === 'asc' ? len - i - 1 : i].getValue();
  284. if (value / sum * totalArea < visibleMin) {
  285. deletePoint = i;
  286. sum -= value;
  287. }
  288. }
  289. orderBy === 'asc' ? orderedChildren.splice(0, len - deletePoint) : orderedChildren.splice(deletePoint, len - deletePoint);
  290. return sum;
  291. }
  292. /**
  293. * Sort
  294. */
  295. function sort(viewChildren, orderBy) {
  296. if (orderBy) {
  297. viewChildren.sort(function (a, b) {
  298. var diff = orderBy === 'asc' ? a.getValue() - b.getValue() : b.getValue() - a.getValue();
  299. return diff === 0 ? orderBy === 'asc' ? a.dataIndex - b.dataIndex : b.dataIndex - a.dataIndex : diff;
  300. });
  301. }
  302. return viewChildren;
  303. }
  304. /**
  305. * Statistic
  306. */
  307. function statistic(nodeModel, children, orderBy) {
  308. // Calculate sum.
  309. var sum = 0;
  310. for (var i = 0, len = children.length; i < len; i++) {
  311. sum += children[i].getValue();
  312. }
  313. // Statistic data extent for latter visual coding.
  314. // Notice: data extent should be calculate based on raw children
  315. // but not filtered view children, otherwise visual mapping will not
  316. // be stable when zoom (where children is filtered by visibleMin).
  317. var dimension = nodeModel.get('visualDimension');
  318. var dataExtent;
  319. // The same as area dimension.
  320. if (!children || !children.length) {
  321. dataExtent = [NaN, NaN];
  322. } else if (dimension === 'value' && orderBy) {
  323. dataExtent = [children[children.length - 1].getValue(), children[0].getValue()];
  324. orderBy === 'asc' && dataExtent.reverse();
  325. }
  326. // Other dimension.
  327. else {
  328. dataExtent = [Infinity, -Infinity];
  329. each(children, function (child) {
  330. var value = child.getValue(dimension);
  331. value < dataExtent[0] && (dataExtent[0] = value);
  332. value > dataExtent[1] && (dataExtent[1] = value);
  333. });
  334. }
  335. return {
  336. sum: sum,
  337. dataExtent: dataExtent
  338. };
  339. }
  340. /**
  341. * Computes the score for the specified row,
  342. * as the worst aspect ratio.
  343. */
  344. function worst(row, rowFixedLength, ratio) {
  345. var areaMax = 0;
  346. var areaMin = Infinity;
  347. for (var i = 0, area = void 0, len = row.length; i < len; i++) {
  348. area = row[i].getLayout().area;
  349. if (area) {
  350. area < areaMin && (areaMin = area);
  351. area > areaMax && (areaMax = area);
  352. }
  353. }
  354. var squareArea = row.area * row.area;
  355. var f = rowFixedLength * rowFixedLength * ratio;
  356. return squareArea ? mathMax(f * areaMax / squareArea, squareArea / (f * areaMin)) : Infinity;
  357. }
  358. /**
  359. * Positions the specified row of nodes. Modifies `rect`.
  360. */
  361. function position(row, rowFixedLength, rect, halfGapWidth, flush) {
  362. // When rowFixedLength === rect.width,
  363. // it is horizontal subdivision,
  364. // rowFixedLength is the width of the subdivision,
  365. // rowOtherLength is the height of the subdivision,
  366. // and nodes will be positioned from left to right.
  367. // wh[idx0WhenH] means: when horizontal,
  368. // wh[idx0WhenH] => wh[0] => 'width'.
  369. // xy[idx1WhenH] => xy[1] => 'y'.
  370. var idx0WhenH = rowFixedLength === rect.width ? 0 : 1;
  371. var idx1WhenH = 1 - idx0WhenH;
  372. var xy = ['x', 'y'];
  373. var wh = ['width', 'height'];
  374. var last = rect[xy[idx0WhenH]];
  375. var rowOtherLength = rowFixedLength ? row.area / rowFixedLength : 0;
  376. if (flush || rowOtherLength > rect[wh[idx1WhenH]]) {
  377. rowOtherLength = rect[wh[idx1WhenH]]; // over+underflow
  378. }
  379. for (var i = 0, rowLen = row.length; i < rowLen; i++) {
  380. var node = row[i];
  381. var nodeLayout = {};
  382. var step = rowOtherLength ? node.getLayout().area / rowOtherLength : 0;
  383. var wh1 = nodeLayout[wh[idx1WhenH]] = mathMax(rowOtherLength - 2 * halfGapWidth, 0);
  384. // We use Math.max/min to avoid negative width/height when considering gap width.
  385. var remain = rect[xy[idx0WhenH]] + rect[wh[idx0WhenH]] - last;
  386. var modWH = i === rowLen - 1 || remain < step ? remain : step;
  387. var wh0 = nodeLayout[wh[idx0WhenH]] = mathMax(modWH - 2 * halfGapWidth, 0);
  388. nodeLayout[xy[idx1WhenH]] = rect[xy[idx1WhenH]] + mathMin(halfGapWidth, wh1 / 2);
  389. nodeLayout[xy[idx0WhenH]] = last + mathMin(halfGapWidth, wh0 / 2);
  390. last += modWH;
  391. node.setLayout(nodeLayout, true);
  392. }
  393. rect[xy[idx1WhenH]] += rowOtherLength;
  394. rect[wh[idx1WhenH]] -= rowOtherLength;
  395. }
  396. // Return [containerWidth, containerHeight] as default.
  397. function estimateRootSize(seriesModel, targetInfo, viewRoot, containerWidth, containerHeight) {
  398. // If targetInfo.node exists, we zoom to the node,
  399. // so estimate whole width and height by target node.
  400. var currNode = (targetInfo || {}).node;
  401. var defaultSize = [containerWidth, containerHeight];
  402. if (!currNode || currNode === viewRoot) {
  403. return defaultSize;
  404. }
  405. var parent;
  406. var viewArea = containerWidth * containerHeight;
  407. var area = viewArea * seriesModel.option.zoomToNodeRatio;
  408. while (parent = currNode.parentNode) {
  409. // jshint ignore:line
  410. var sum = 0;
  411. var siblings = parent.children;
  412. for (var i = 0, len = siblings.length; i < len; i++) {
  413. sum += siblings[i].getValue();
  414. }
  415. var currNodeValue = currNode.getValue();
  416. if (currNodeValue === 0) {
  417. return defaultSize;
  418. }
  419. area *= sum / currNodeValue;
  420. // Considering border, suppose aspect ratio is 1.
  421. var parentModel = parent.getModel();
  422. var borderWidth = parentModel.get(PATH_BORDER_WIDTH);
  423. var upperHeight = Math.max(borderWidth, getUpperLabelHeight(parentModel));
  424. area += 4 * borderWidth * borderWidth + (3 * borderWidth + upperHeight) * Math.pow(area, 0.5);
  425. area > MAX_SAFE_INTEGER && (area = MAX_SAFE_INTEGER);
  426. currNode = parent;
  427. }
  428. area < viewArea && (area = viewArea);
  429. var scale = Math.pow(area / viewArea, 0.5);
  430. return [containerWidth * scale, containerHeight * scale];
  431. }
  432. // Root position based on coord of containerGroup
  433. function calculateRootPosition(layoutInfo, rootRect, targetInfo) {
  434. if (rootRect) {
  435. return {
  436. x: rootRect.x,
  437. y: rootRect.y
  438. };
  439. }
  440. var defaultPosition = {
  441. x: 0,
  442. y: 0
  443. };
  444. if (!targetInfo) {
  445. return defaultPosition;
  446. }
  447. // If targetInfo is fetched by 'retrieveTargetInfo',
  448. // old tree and new tree are the same tree,
  449. // so the node still exists and we can visit it.
  450. var targetNode = targetInfo.node;
  451. var layout = targetNode.getLayout();
  452. if (!layout) {
  453. return defaultPosition;
  454. }
  455. // Transform coord from local to container.
  456. var targetCenter = [layout.width / 2, layout.height / 2];
  457. var node = targetNode;
  458. while (node) {
  459. var nodeLayout = node.getLayout();
  460. targetCenter[0] += nodeLayout.x;
  461. targetCenter[1] += nodeLayout.y;
  462. node = node.parentNode;
  463. }
  464. return {
  465. x: layoutInfo.width / 2 - targetCenter[0],
  466. y: layoutInfo.height / 2 - targetCenter[1]
  467. };
  468. }
  469. // Mark nodes visible for prunning when visual coding and rendering.
  470. // Prunning depends on layout and root position, so we have to do it after layout.
  471. function prunning(node, clipRect, viewAbovePath, viewRoot, depth) {
  472. var nodeLayout = node.getLayout();
  473. var nodeInViewAbovePath = viewAbovePath[depth];
  474. var isAboveViewRoot = nodeInViewAbovePath && nodeInViewAbovePath === node;
  475. if (nodeInViewAbovePath && !isAboveViewRoot || depth === viewAbovePath.length && node !== viewRoot) {
  476. return;
  477. }
  478. node.setLayout({
  479. // isInView means: viewRoot sub tree + viewAbovePath
  480. isInView: true,
  481. // invisible only means: outside view clip so that the node can not
  482. // see but still layout for animation preparation but not render.
  483. invisible: !isAboveViewRoot && !clipRect.intersect(nodeLayout),
  484. isAboveViewRoot: isAboveViewRoot
  485. }, true);
  486. // Transform to child coordinate.
  487. var childClipRect = new BoundingRect(clipRect.x - nodeLayout.x, clipRect.y - nodeLayout.y, clipRect.width, clipRect.height);
  488. each(node.viewChildren || [], function (child) {
  489. prunning(child, childClipRect, viewAbovePath, viewRoot, depth + 1);
  490. });
  491. }
  492. function getUpperLabelHeight(model) {
  493. return model.get(PATH_UPPER_LABEL_SHOW) ? model.get(PATH_UPPER_LABEL_HEIGHT) : 0;
  494. }