TreemapView.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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. import { __extends } from "tslib";
  41. import { bind, each, indexOf, curry, extend, normalizeCssArray, isFunction } from 'zrender/lib/core/util.js';
  42. import * as graphic from '../../util/graphic.js';
  43. import { getECData } from '../../util/innerStore.js';
  44. import { isHighDownDispatcher, setAsHighDownDispatcher, setDefaultStateProxy, enableHoverFocus, Z2_EMPHASIS_LIFT } from '../../util/states.js';
  45. import DataDiffer from '../../data/DataDiffer.js';
  46. import * as helper from '../helper/treeHelper.js';
  47. import Breadcrumb from './Breadcrumb.js';
  48. import RoamController from '../../component/helper/RoamController.js';
  49. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  50. import * as matrix from 'zrender/lib/core/matrix.js';
  51. import * as animationUtil from '../../util/animation.js';
  52. import makeStyleMapper from '../../model/mixin/makeStyleMapper.js';
  53. import ChartView from '../../view/Chart.js';
  54. import Displayable from 'zrender/lib/graphic/Displayable.js';
  55. import { makeInner, convertOptionIdName } from '../../util/model.js';
  56. import { windowOpen } from '../../util/format.js';
  57. import { setLabelStyle, getLabelStatesModels } from '../../label/labelStyle.js';
  58. var Group = graphic.Group;
  59. var Rect = graphic.Rect;
  60. var DRAG_THRESHOLD = 3;
  61. var PATH_LABEL_NOAMAL = 'label';
  62. var PATH_UPPERLABEL_NORMAL = 'upperLabel';
  63. // Should larger than emphasis states lift z
  64. var Z2_BASE = Z2_EMPHASIS_LIFT * 10; // Should bigger than every z2.
  65. var Z2_BG = Z2_EMPHASIS_LIFT * 2;
  66. var Z2_CONTENT = Z2_EMPHASIS_LIFT * 3;
  67. var getStateItemStyle = makeStyleMapper([['fill', 'color'],
  68. // `borderColor` and `borderWidth` has been occupied,
  69. // so use `stroke` to indicate the stroke of the rect.
  70. ['stroke', 'strokeColor'], ['lineWidth', 'strokeWidth'], ['shadowBlur'], ['shadowOffsetX'], ['shadowOffsetY'], ['shadowColor']
  71. // Option decal is in `DecalObject` but style.decal is in `PatternObject`.
  72. // So do not transfer decal directly.
  73. ]);
  74. var getItemStyleNormal = function (model) {
  75. // Normal style props should include emphasis style props.
  76. var itemStyle = getStateItemStyle(model);
  77. // Clear styles set by emphasis.
  78. itemStyle.stroke = itemStyle.fill = itemStyle.lineWidth = null;
  79. return itemStyle;
  80. };
  81. var inner = makeInner();
  82. var TreemapView = /** @class */function (_super) {
  83. __extends(TreemapView, _super);
  84. function TreemapView() {
  85. var _this = _super !== null && _super.apply(this, arguments) || this;
  86. _this.type = TreemapView.type;
  87. _this._state = 'ready';
  88. _this._storage = createStorage();
  89. return _this;
  90. }
  91. /**
  92. * @override
  93. */
  94. TreemapView.prototype.render = function (seriesModel, ecModel, api, payload) {
  95. var models = ecModel.findComponents({
  96. mainType: 'series',
  97. subType: 'treemap',
  98. query: payload
  99. });
  100. if (indexOf(models, seriesModel) < 0) {
  101. return;
  102. }
  103. this.seriesModel = seriesModel;
  104. this.api = api;
  105. this.ecModel = ecModel;
  106. var types = ['treemapZoomToNode', 'treemapRootToNode'];
  107. var targetInfo = helper.retrieveTargetInfo(payload, types, seriesModel);
  108. var payloadType = payload && payload.type;
  109. var layoutInfo = seriesModel.layoutInfo;
  110. var isInit = !this._oldTree;
  111. var thisStorage = this._storage;
  112. // Mark new root when action is treemapRootToNode.
  113. var reRoot = payloadType === 'treemapRootToNode' && targetInfo && thisStorage ? {
  114. rootNodeGroup: thisStorage.nodeGroup[targetInfo.node.getRawIndex()],
  115. direction: payload.direction
  116. } : null;
  117. var containerGroup = this._giveContainerGroup(layoutInfo);
  118. var hasAnimation = seriesModel.get('animation');
  119. var renderResult = this._doRender(containerGroup, seriesModel, reRoot);
  120. hasAnimation && !isInit && (!payloadType || payloadType === 'treemapZoomToNode' || payloadType === 'treemapRootToNode') ? this._doAnimation(containerGroup, renderResult, seriesModel, reRoot) : renderResult.renderFinally();
  121. this._resetController(api);
  122. this._renderBreadcrumb(seriesModel, api, targetInfo);
  123. };
  124. TreemapView.prototype._giveContainerGroup = function (layoutInfo) {
  125. var containerGroup = this._containerGroup;
  126. if (!containerGroup) {
  127. // FIXME
  128. // 加一层containerGroup是为了clip,但是现在clip功能并没有实现。
  129. containerGroup = this._containerGroup = new Group();
  130. this._initEvents(containerGroup);
  131. this.group.add(containerGroup);
  132. }
  133. containerGroup.x = layoutInfo.x;
  134. containerGroup.y = layoutInfo.y;
  135. return containerGroup;
  136. };
  137. TreemapView.prototype._doRender = function (containerGroup, seriesModel, reRoot) {
  138. var thisTree = seriesModel.getData().tree;
  139. var oldTree = this._oldTree;
  140. // Clear last shape records.
  141. var lastsForAnimation = createStorage();
  142. var thisStorage = createStorage();
  143. var oldStorage = this._storage;
  144. var willInvisibleEls = [];
  145. function doRenderNode(thisNode, oldNode, parentGroup, depth) {
  146. return renderNode(seriesModel, thisStorage, oldStorage, reRoot, lastsForAnimation, willInvisibleEls, thisNode, oldNode, parentGroup, depth);
  147. }
  148. // Notice: When thisTree and oldTree are the same tree (see list.cloneShallow),
  149. // the oldTree is actually losted, so we cannot find all of the old graphic
  150. // elements from tree. So we use this strategy: make element storage, move
  151. // from old storage to new storage, clear old storage.
  152. dualTravel(thisTree.root ? [thisTree.root] : [], oldTree && oldTree.root ? [oldTree.root] : [], containerGroup, thisTree === oldTree || !oldTree, 0);
  153. // Process all removing.
  154. var willDeleteEls = clearStorage(oldStorage);
  155. this._oldTree = thisTree;
  156. this._storage = thisStorage;
  157. if (this._controllerHost) {
  158. var _oldRootLayout = this.seriesModel.layoutInfo;
  159. var rootLayout = thisTree.root.getLayout();
  160. if (rootLayout.width === _oldRootLayout.width && rootLayout.height === _oldRootLayout.height) {
  161. this._controllerHost.zoom = 1;
  162. }
  163. }
  164. return {
  165. lastsForAnimation: lastsForAnimation,
  166. willDeleteEls: willDeleteEls,
  167. renderFinally: renderFinally
  168. };
  169. function dualTravel(thisViewChildren, oldViewChildren, parentGroup, sameTree, depth) {
  170. // When 'render' is triggered by action,
  171. // 'this' and 'old' may be the same tree,
  172. // we use rawIndex in that case.
  173. if (sameTree) {
  174. oldViewChildren = thisViewChildren;
  175. each(thisViewChildren, function (child, index) {
  176. !child.isRemoved() && processNode(index, index);
  177. });
  178. }
  179. // Diff hierarchically (diff only in each subtree, but not whole).
  180. // because, consistency of view is important.
  181. else {
  182. new DataDiffer(oldViewChildren, thisViewChildren, getKey, getKey).add(processNode).update(processNode).remove(curry(processNode, null)).execute();
  183. }
  184. function getKey(node) {
  185. // Identify by name or raw index.
  186. return node.getId();
  187. }
  188. function processNode(newIndex, oldIndex) {
  189. var thisNode = newIndex != null ? thisViewChildren[newIndex] : null;
  190. var oldNode = oldIndex != null ? oldViewChildren[oldIndex] : null;
  191. var group = doRenderNode(thisNode, oldNode, parentGroup, depth);
  192. group && dualTravel(thisNode && thisNode.viewChildren || [], oldNode && oldNode.viewChildren || [], group, sameTree, depth + 1);
  193. }
  194. }
  195. function clearStorage(storage) {
  196. var willDeleteEls = createStorage();
  197. storage && each(storage, function (store, storageName) {
  198. var delEls = willDeleteEls[storageName];
  199. each(store, function (el) {
  200. el && (delEls.push(el), inner(el).willDelete = true);
  201. });
  202. });
  203. return willDeleteEls;
  204. }
  205. function renderFinally() {
  206. each(willDeleteEls, function (els) {
  207. each(els, function (el) {
  208. el.parent && el.parent.remove(el);
  209. });
  210. });
  211. each(willInvisibleEls, function (el) {
  212. el.invisible = true;
  213. // Setting invisible is for optimizing, so no need to set dirty,
  214. // just mark as invisible.
  215. el.dirty();
  216. });
  217. }
  218. };
  219. TreemapView.prototype._doAnimation = function (containerGroup, renderResult, seriesModel, reRoot) {
  220. var durationOption = seriesModel.get('animationDurationUpdate');
  221. var easingOption = seriesModel.get('animationEasing');
  222. // TODO: do not support function until necessary.
  223. var duration = (isFunction(durationOption) ? 0 : durationOption) || 0;
  224. var easing = (isFunction(easingOption) ? null : easingOption) || 'cubicOut';
  225. var animationWrap = animationUtil.createWrap();
  226. // Make delete animations.
  227. each(renderResult.willDeleteEls, function (store, storageName) {
  228. each(store, function (el, rawIndex) {
  229. if (el.invisible) {
  230. return;
  231. }
  232. var parent = el.parent; // Always has parent, and parent is nodeGroup.
  233. var target;
  234. var innerStore = inner(parent);
  235. if (reRoot && reRoot.direction === 'drillDown') {
  236. target = parent === reRoot.rootNodeGroup
  237. // This is the content element of view root.
  238. // Only `content` will enter this branch, because
  239. // `background` and `nodeGroup` will not be deleted.
  240. ? {
  241. shape: {
  242. x: 0,
  243. y: 0,
  244. width: innerStore.nodeWidth,
  245. height: innerStore.nodeHeight
  246. },
  247. style: {
  248. opacity: 0
  249. }
  250. }
  251. // Others.
  252. : {
  253. style: {
  254. opacity: 0
  255. }
  256. };
  257. } else {
  258. var targetX = 0;
  259. var targetY = 0;
  260. if (!innerStore.willDelete) {
  261. // Let node animate to right-bottom corner, cooperating with fadeout,
  262. // which is appropriate for user understanding.
  263. // Divided by 2 for reRoot rolling up effect.
  264. targetX = innerStore.nodeWidth / 2;
  265. targetY = innerStore.nodeHeight / 2;
  266. }
  267. target = storageName === 'nodeGroup' ? {
  268. x: targetX,
  269. y: targetY,
  270. style: {
  271. opacity: 0
  272. }
  273. } : {
  274. shape: {
  275. x: targetX,
  276. y: targetY,
  277. width: 0,
  278. height: 0
  279. },
  280. style: {
  281. opacity: 0
  282. }
  283. };
  284. }
  285. // TODO: do not support delay until necessary.
  286. target && animationWrap.add(el, target, duration, 0, easing);
  287. });
  288. });
  289. // Make other animations
  290. each(this._storage, function (store, storageName) {
  291. each(store, function (el, rawIndex) {
  292. var last = renderResult.lastsForAnimation[storageName][rawIndex];
  293. var target = {};
  294. if (!last) {
  295. return;
  296. }
  297. if (el instanceof graphic.Group) {
  298. if (last.oldX != null) {
  299. target.x = el.x;
  300. target.y = el.y;
  301. el.x = last.oldX;
  302. el.y = last.oldY;
  303. }
  304. } else {
  305. if (last.oldShape) {
  306. target.shape = extend({}, el.shape);
  307. el.setShape(last.oldShape);
  308. }
  309. if (last.fadein) {
  310. el.setStyle('opacity', 0);
  311. target.style = {
  312. opacity: 1
  313. };
  314. }
  315. // When animation is stopped for succedent animation starting,
  316. // el.style.opacity might not be 1
  317. else if (el.style.opacity !== 1) {
  318. target.style = {
  319. opacity: 1
  320. };
  321. }
  322. }
  323. animationWrap.add(el, target, duration, 0, easing);
  324. });
  325. }, this);
  326. this._state = 'animating';
  327. animationWrap.finished(bind(function () {
  328. this._state = 'ready';
  329. renderResult.renderFinally();
  330. }, this)).start();
  331. };
  332. TreemapView.prototype._resetController = function (api) {
  333. var controller = this._controller;
  334. var controllerHost = this._controllerHost;
  335. if (!controllerHost) {
  336. this._controllerHost = {
  337. target: this.group
  338. };
  339. controllerHost = this._controllerHost;
  340. }
  341. // Init controller.
  342. if (!controller) {
  343. controller = this._controller = new RoamController(api.getZr());
  344. controller.enable(this.seriesModel.get('roam'));
  345. controllerHost.zoomLimit = this.seriesModel.get('scaleLimit');
  346. controllerHost.zoom = this.seriesModel.get('zoom');
  347. controller.on('pan', bind(this._onPan, this));
  348. controller.on('zoom', bind(this._onZoom, this));
  349. }
  350. var rect = new BoundingRect(0, 0, api.getWidth(), api.getHeight());
  351. controller.setPointerChecker(function (e, x, y) {
  352. return rect.contain(x, y);
  353. });
  354. };
  355. TreemapView.prototype._clearController = function () {
  356. var controller = this._controller;
  357. this._controllerHost = null;
  358. if (controller) {
  359. controller.dispose();
  360. controller = null;
  361. }
  362. };
  363. TreemapView.prototype._onPan = function (e) {
  364. if (this._state !== 'animating' && (Math.abs(e.dx) > DRAG_THRESHOLD || Math.abs(e.dy) > DRAG_THRESHOLD)) {
  365. // These param must not be cached.
  366. var root = this.seriesModel.getData().tree.root;
  367. if (!root) {
  368. return;
  369. }
  370. var rootLayout = root.getLayout();
  371. if (!rootLayout) {
  372. return;
  373. }
  374. this.api.dispatchAction({
  375. type: 'treemapMove',
  376. from: this.uid,
  377. seriesId: this.seriesModel.id,
  378. rootRect: {
  379. x: rootLayout.x + e.dx,
  380. y: rootLayout.y + e.dy,
  381. width: rootLayout.width,
  382. height: rootLayout.height
  383. }
  384. });
  385. }
  386. };
  387. TreemapView.prototype._onZoom = function (e) {
  388. var mouseX = e.originX;
  389. var mouseY = e.originY;
  390. var zoomDelta = e.scale;
  391. if (this._state !== 'animating') {
  392. // These param must not be cached.
  393. var root = this.seriesModel.getData().tree.root;
  394. if (!root) {
  395. return;
  396. }
  397. var rootLayout = root.getLayout();
  398. if (!rootLayout) {
  399. return;
  400. }
  401. var rect = new BoundingRect(rootLayout.x, rootLayout.y, rootLayout.width, rootLayout.height);
  402. // scaleLimit
  403. var zoomLimit = null;
  404. var _controllerHost = this._controllerHost;
  405. zoomLimit = _controllerHost.zoomLimit;
  406. var newZoom = _controllerHost.zoom = _controllerHost.zoom || 1;
  407. newZoom *= zoomDelta;
  408. if (zoomLimit) {
  409. var zoomMin = zoomLimit.min || 0;
  410. var zoomMax = zoomLimit.max || Infinity;
  411. newZoom = Math.max(Math.min(zoomMax, newZoom), zoomMin);
  412. }
  413. var zoomScale = newZoom / _controllerHost.zoom;
  414. _controllerHost.zoom = newZoom;
  415. var layoutInfo = this.seriesModel.layoutInfo;
  416. // Transform mouse coord from global to containerGroup.
  417. mouseX -= layoutInfo.x;
  418. mouseY -= layoutInfo.y;
  419. // Scale root bounding rect.
  420. var m = matrix.create();
  421. matrix.translate(m, m, [-mouseX, -mouseY]);
  422. matrix.scale(m, m, [zoomScale, zoomScale]);
  423. matrix.translate(m, m, [mouseX, mouseY]);
  424. rect.applyTransform(m);
  425. this.api.dispatchAction({
  426. type: 'treemapRender',
  427. from: this.uid,
  428. seriesId: this.seriesModel.id,
  429. rootRect: {
  430. x: rect.x,
  431. y: rect.y,
  432. width: rect.width,
  433. height: rect.height
  434. }
  435. });
  436. }
  437. };
  438. TreemapView.prototype._initEvents = function (containerGroup) {
  439. var _this = this;
  440. containerGroup.on('click', function (e) {
  441. if (_this._state !== 'ready') {
  442. return;
  443. }
  444. var nodeClick = _this.seriesModel.get('nodeClick', true);
  445. if (!nodeClick) {
  446. return;
  447. }
  448. var targetInfo = _this.findTarget(e.offsetX, e.offsetY);
  449. if (!targetInfo) {
  450. return;
  451. }
  452. var node = targetInfo.node;
  453. if (node.getLayout().isLeafRoot) {
  454. _this._rootToNode(targetInfo);
  455. } else {
  456. if (nodeClick === 'zoomToNode') {
  457. _this._zoomToNode(targetInfo);
  458. } else if (nodeClick === 'link') {
  459. var itemModel = node.hostTree.data.getItemModel(node.dataIndex);
  460. var link = itemModel.get('link', true);
  461. var linkTarget = itemModel.get('target', true) || 'blank';
  462. link && windowOpen(link, linkTarget);
  463. }
  464. }
  465. }, this);
  466. };
  467. TreemapView.prototype._renderBreadcrumb = function (seriesModel, api, targetInfo) {
  468. var _this = this;
  469. if (!targetInfo) {
  470. targetInfo = seriesModel.get('leafDepth', true) != null ? {
  471. node: seriesModel.getViewRoot()
  472. }
  473. // FIXME
  474. // better way?
  475. // Find breadcrumb tail on center of containerGroup.
  476. : this.findTarget(api.getWidth() / 2, api.getHeight() / 2);
  477. if (!targetInfo) {
  478. targetInfo = {
  479. node: seriesModel.getData().tree.root
  480. };
  481. }
  482. }
  483. (this._breadcrumb || (this._breadcrumb = new Breadcrumb(this.group))).render(seriesModel, api, targetInfo.node, function (node) {
  484. if (_this._state !== 'animating') {
  485. helper.aboveViewRoot(seriesModel.getViewRoot(), node) ? _this._rootToNode({
  486. node: node
  487. }) : _this._zoomToNode({
  488. node: node
  489. });
  490. }
  491. });
  492. };
  493. /**
  494. * @override
  495. */
  496. TreemapView.prototype.remove = function () {
  497. this._clearController();
  498. this._containerGroup && this._containerGroup.removeAll();
  499. this._storage = createStorage();
  500. this._state = 'ready';
  501. this._breadcrumb && this._breadcrumb.remove();
  502. };
  503. TreemapView.prototype.dispose = function () {
  504. this._clearController();
  505. };
  506. TreemapView.prototype._zoomToNode = function (targetInfo) {
  507. this.api.dispatchAction({
  508. type: 'treemapZoomToNode',
  509. from: this.uid,
  510. seriesId: this.seriesModel.id,
  511. targetNode: targetInfo.node
  512. });
  513. };
  514. TreemapView.prototype._rootToNode = function (targetInfo) {
  515. this.api.dispatchAction({
  516. type: 'treemapRootToNode',
  517. from: this.uid,
  518. seriesId: this.seriesModel.id,
  519. targetNode: targetInfo.node
  520. });
  521. };
  522. /**
  523. * @public
  524. * @param {number} x Global coord x.
  525. * @param {number} y Global coord y.
  526. * @return {Object} info If not found, return undefined;
  527. * @return {number} info.node Target node.
  528. * @return {number} info.offsetX x refer to target node.
  529. * @return {number} info.offsetY y refer to target node.
  530. */
  531. TreemapView.prototype.findTarget = function (x, y) {
  532. var targetInfo;
  533. var viewRoot = this.seriesModel.getViewRoot();
  534. viewRoot.eachNode({
  535. attr: 'viewChildren',
  536. order: 'preorder'
  537. }, function (node) {
  538. var bgEl = this._storage.background[node.getRawIndex()];
  539. // If invisible, there might be no element.
  540. if (bgEl) {
  541. var point = bgEl.transformCoordToLocal(x, y);
  542. var shape = bgEl.shape;
  543. // For performance consideration, don't use 'getBoundingRect'.
  544. if (shape.x <= point[0] && point[0] <= shape.x + shape.width && shape.y <= point[1] && point[1] <= shape.y + shape.height) {
  545. targetInfo = {
  546. node: node,
  547. offsetX: point[0],
  548. offsetY: point[1]
  549. };
  550. } else {
  551. return false; // Suppress visit subtree.
  552. }
  553. }
  554. }, this);
  555. return targetInfo;
  556. };
  557. TreemapView.type = 'treemap';
  558. return TreemapView;
  559. }(ChartView);
  560. /**
  561. * @inner
  562. */
  563. function createStorage() {
  564. return {
  565. nodeGroup: [],
  566. background: [],
  567. content: []
  568. };
  569. }
  570. /**
  571. * @inner
  572. * @return Return undefined means do not travel further.
  573. */
  574. function renderNode(seriesModel, thisStorage, oldStorage, reRoot, lastsForAnimation, willInvisibleEls, thisNode, oldNode, parentGroup, depth) {
  575. // Whether under viewRoot.
  576. if (!thisNode) {
  577. // Deleting nodes will be performed finally. This method just find
  578. // element from old storage, or create new element, set them to new
  579. // storage, and set styles.
  580. return;
  581. }
  582. // -------------------------------------------------------------------
  583. // Start of closure variables available in "Procedures in renderNode".
  584. var thisLayout = thisNode.getLayout();
  585. var data = seriesModel.getData();
  586. var nodeModel = thisNode.getModel();
  587. // Only for enabling highlight/downplay. Clear firstly.
  588. // Because some node will not be rendered.
  589. data.setItemGraphicEl(thisNode.dataIndex, null);
  590. if (!thisLayout || !thisLayout.isInView) {
  591. return;
  592. }
  593. var thisWidth = thisLayout.width;
  594. var thisHeight = thisLayout.height;
  595. var borderWidth = thisLayout.borderWidth;
  596. var thisInvisible = thisLayout.invisible;
  597. var thisRawIndex = thisNode.getRawIndex();
  598. var oldRawIndex = oldNode && oldNode.getRawIndex();
  599. var thisViewChildren = thisNode.viewChildren;
  600. var upperHeight = thisLayout.upperHeight;
  601. var isParent = thisViewChildren && thisViewChildren.length;
  602. var itemStyleNormalModel = nodeModel.getModel('itemStyle');
  603. var itemStyleEmphasisModel = nodeModel.getModel(['emphasis', 'itemStyle']);
  604. var itemStyleBlurModel = nodeModel.getModel(['blur', 'itemStyle']);
  605. var itemStyleSelectModel = nodeModel.getModel(['select', 'itemStyle']);
  606. var borderRadius = itemStyleNormalModel.get('borderRadius') || 0;
  607. // End of closure ariables available in "Procedures in renderNode".
  608. // -----------------------------------------------------------------
  609. // Node group
  610. var group = giveGraphic('nodeGroup', Group);
  611. if (!group) {
  612. return;
  613. }
  614. parentGroup.add(group);
  615. // x,y are not set when el is above view root.
  616. group.x = thisLayout.x || 0;
  617. group.y = thisLayout.y || 0;
  618. group.markRedraw();
  619. inner(group).nodeWidth = thisWidth;
  620. inner(group).nodeHeight = thisHeight;
  621. if (thisLayout.isAboveViewRoot) {
  622. return group;
  623. }
  624. // Background
  625. var bg = giveGraphic('background', Rect, depth, Z2_BG);
  626. bg && renderBackground(group, bg, isParent && thisLayout.upperLabelHeight);
  627. var emphasisModel = nodeModel.getModel('emphasis');
  628. var focus = emphasisModel.get('focus');
  629. var blurScope = emphasisModel.get('blurScope');
  630. var isDisabled = emphasisModel.get('disabled');
  631. var focusOrIndices = focus === 'ancestor' ? thisNode.getAncestorsIndices() : focus === 'descendant' ? thisNode.getDescendantIndices() : focus;
  632. // No children, render content.
  633. if (isParent) {
  634. // Because of the implementation about "traverse" in graphic hover style, we
  635. // can not set hover listener on the "group" of non-leaf node. Otherwise the
  636. // hover event from the descendents will be listenered.
  637. if (isHighDownDispatcher(group)) {
  638. setAsHighDownDispatcher(group, false);
  639. }
  640. if (bg) {
  641. setAsHighDownDispatcher(bg, !isDisabled);
  642. // Only for enabling highlight/downplay.
  643. data.setItemGraphicEl(thisNode.dataIndex, bg);
  644. enableHoverFocus(bg, focusOrIndices, blurScope);
  645. }
  646. } else {
  647. var content = giveGraphic('content', Rect, depth, Z2_CONTENT);
  648. content && renderContent(group, content);
  649. bg.disableMorphing = true;
  650. if (bg && isHighDownDispatcher(bg)) {
  651. setAsHighDownDispatcher(bg, false);
  652. }
  653. setAsHighDownDispatcher(group, !isDisabled);
  654. // Only for enabling highlight/downplay.
  655. data.setItemGraphicEl(thisNode.dataIndex, group);
  656. enableHoverFocus(group, focusOrIndices, blurScope);
  657. }
  658. return group;
  659. // ----------------------------
  660. // | Procedures in renderNode |
  661. // ----------------------------
  662. function renderBackground(group, bg, useUpperLabel) {
  663. var ecData = getECData(bg);
  664. // For tooltip.
  665. ecData.dataIndex = thisNode.dataIndex;
  666. ecData.seriesIndex = seriesModel.seriesIndex;
  667. bg.setShape({
  668. x: 0,
  669. y: 0,
  670. width: thisWidth,
  671. height: thisHeight,
  672. r: borderRadius
  673. });
  674. if (thisInvisible) {
  675. // If invisible, do not set visual, otherwise the element will
  676. // change immediately before animation. We think it is OK to
  677. // remain its origin color when moving out of the view window.
  678. processInvisible(bg);
  679. } else {
  680. bg.invisible = false;
  681. var style = thisNode.getVisual('style');
  682. var visualBorderColor = style.stroke;
  683. var normalStyle = getItemStyleNormal(itemStyleNormalModel);
  684. normalStyle.fill = visualBorderColor;
  685. var emphasisStyle = getStateItemStyle(itemStyleEmphasisModel);
  686. emphasisStyle.fill = itemStyleEmphasisModel.get('borderColor');
  687. var blurStyle = getStateItemStyle(itemStyleBlurModel);
  688. blurStyle.fill = itemStyleBlurModel.get('borderColor');
  689. var selectStyle = getStateItemStyle(itemStyleSelectModel);
  690. selectStyle.fill = itemStyleSelectModel.get('borderColor');
  691. if (useUpperLabel) {
  692. var upperLabelWidth = thisWidth - 2 * borderWidth;
  693. prepareText(
  694. // PENDING: convert ZRColor to ColorString for text.
  695. bg, visualBorderColor, style.opacity, {
  696. x: borderWidth,
  697. y: 0,
  698. width: upperLabelWidth,
  699. height: upperHeight
  700. });
  701. }
  702. // For old bg.
  703. else {
  704. bg.removeTextContent();
  705. }
  706. bg.setStyle(normalStyle);
  707. bg.ensureState('emphasis').style = emphasisStyle;
  708. bg.ensureState('blur').style = blurStyle;
  709. bg.ensureState('select').style = selectStyle;
  710. setDefaultStateProxy(bg);
  711. }
  712. group.add(bg);
  713. }
  714. function renderContent(group, content) {
  715. var ecData = getECData(content);
  716. // For tooltip.
  717. ecData.dataIndex = thisNode.dataIndex;
  718. ecData.seriesIndex = seriesModel.seriesIndex;
  719. var contentWidth = Math.max(thisWidth - 2 * borderWidth, 0);
  720. var contentHeight = Math.max(thisHeight - 2 * borderWidth, 0);
  721. content.culling = true;
  722. content.setShape({
  723. x: borderWidth,
  724. y: borderWidth,
  725. width: contentWidth,
  726. height: contentHeight,
  727. r: borderRadius
  728. });
  729. if (thisInvisible) {
  730. // If invisible, do not set visual, otherwise the element will
  731. // change immediately before animation. We think it is OK to
  732. // remain its origin color when moving out of the view window.
  733. processInvisible(content);
  734. } else {
  735. content.invisible = false;
  736. var nodeStyle = thisNode.getVisual('style');
  737. var visualColor = nodeStyle.fill;
  738. var normalStyle = getItemStyleNormal(itemStyleNormalModel);
  739. normalStyle.fill = visualColor;
  740. normalStyle.decal = nodeStyle.decal;
  741. var emphasisStyle = getStateItemStyle(itemStyleEmphasisModel);
  742. var blurStyle = getStateItemStyle(itemStyleBlurModel);
  743. var selectStyle = getStateItemStyle(itemStyleSelectModel);
  744. // PENDING: convert ZRColor to ColorString for text.
  745. prepareText(content, visualColor, nodeStyle.opacity, null);
  746. content.setStyle(normalStyle);
  747. content.ensureState('emphasis').style = emphasisStyle;
  748. content.ensureState('blur').style = blurStyle;
  749. content.ensureState('select').style = selectStyle;
  750. setDefaultStateProxy(content);
  751. }
  752. group.add(content);
  753. }
  754. function processInvisible(element) {
  755. // Delay invisible setting utill animation finished,
  756. // avoid element vanish suddenly before animation.
  757. !element.invisible && willInvisibleEls.push(element);
  758. }
  759. function prepareText(rectEl, visualColor, visualOpacity,
  760. // Can be null/undefined
  761. upperLabelRect) {
  762. var normalLabelModel = nodeModel.getModel(upperLabelRect ? PATH_UPPERLABEL_NORMAL : PATH_LABEL_NOAMAL);
  763. var defaultText = convertOptionIdName(nodeModel.get('name'), null);
  764. var isShow = normalLabelModel.getShallow('show');
  765. setLabelStyle(rectEl, getLabelStatesModels(nodeModel, upperLabelRect ? PATH_UPPERLABEL_NORMAL : PATH_LABEL_NOAMAL), {
  766. defaultText: isShow ? defaultText : null,
  767. inheritColor: visualColor,
  768. defaultOpacity: visualOpacity,
  769. labelFetcher: seriesModel,
  770. labelDataIndex: thisNode.dataIndex
  771. });
  772. var textEl = rectEl.getTextContent();
  773. if (!textEl) {
  774. return;
  775. }
  776. var textStyle = textEl.style;
  777. var textPadding = normalizeCssArray(textStyle.padding || 0);
  778. if (upperLabelRect) {
  779. rectEl.setTextConfig({
  780. layoutRect: upperLabelRect
  781. });
  782. textEl.disableLabelLayout = true;
  783. }
  784. textEl.beforeUpdate = function () {
  785. var width = Math.max((upperLabelRect ? upperLabelRect.width : rectEl.shape.width) - textPadding[1] - textPadding[3], 0);
  786. var height = Math.max((upperLabelRect ? upperLabelRect.height : rectEl.shape.height) - textPadding[0] - textPadding[2], 0);
  787. if (textStyle.width !== width || textStyle.height !== height) {
  788. textEl.setStyle({
  789. width: width,
  790. height: height
  791. });
  792. }
  793. };
  794. textStyle.truncateMinChar = 2;
  795. textStyle.lineOverflow = 'truncate';
  796. addDrillDownIcon(textStyle, upperLabelRect, thisLayout);
  797. var textEmphasisState = textEl.getState('emphasis');
  798. addDrillDownIcon(textEmphasisState ? textEmphasisState.style : null, upperLabelRect, thisLayout);
  799. }
  800. function addDrillDownIcon(style, upperLabelRect, thisLayout) {
  801. var text = style ? style.text : null;
  802. if (!upperLabelRect && thisLayout.isLeafRoot && text != null) {
  803. var iconChar = seriesModel.get('drillDownIcon', true);
  804. style.text = iconChar ? iconChar + ' ' + text : text;
  805. }
  806. }
  807. function giveGraphic(storageName, Ctor, depth, z) {
  808. var element = oldRawIndex != null && oldStorage[storageName][oldRawIndex];
  809. var lasts = lastsForAnimation[storageName];
  810. if (element) {
  811. // Remove from oldStorage
  812. oldStorage[storageName][oldRawIndex] = null;
  813. prepareAnimationWhenHasOld(lasts, element);
  814. }
  815. // If invisible and no old element, do not create new element (for optimizing).
  816. else if (!thisInvisible) {
  817. element = new Ctor();
  818. if (element instanceof Displayable) {
  819. element.z2 = calculateZ2(depth, z);
  820. }
  821. prepareAnimationWhenNoOld(lasts, element);
  822. }
  823. // Set to thisStorage
  824. return thisStorage[storageName][thisRawIndex] = element;
  825. }
  826. function prepareAnimationWhenHasOld(lasts, element) {
  827. var lastCfg = lasts[thisRawIndex] = {};
  828. if (element instanceof Group) {
  829. lastCfg.oldX = element.x;
  830. lastCfg.oldY = element.y;
  831. } else {
  832. lastCfg.oldShape = extend({}, element.shape);
  833. }
  834. }
  835. // If a element is new, we need to find the animation start point carefully,
  836. // otherwise it will looks strange when 'zoomToNode'.
  837. function prepareAnimationWhenNoOld(lasts, element) {
  838. var lastCfg = lasts[thisRawIndex] = {};
  839. var parentNode = thisNode.parentNode;
  840. var isGroup = element instanceof graphic.Group;
  841. if (parentNode && (!reRoot || reRoot.direction === 'drillDown')) {
  842. var parentOldX = 0;
  843. var parentOldY = 0;
  844. // New nodes appear from right-bottom corner in 'zoomToNode' animation.
  845. // For convenience, get old bounding rect from background.
  846. var parentOldBg = lastsForAnimation.background[parentNode.getRawIndex()];
  847. if (!reRoot && parentOldBg && parentOldBg.oldShape) {
  848. parentOldX = parentOldBg.oldShape.width;
  849. parentOldY = parentOldBg.oldShape.height;
  850. }
  851. // When no parent old shape found, its parent is new too,
  852. // so we can just use {x:0, y:0}.
  853. if (isGroup) {
  854. lastCfg.oldX = 0;
  855. lastCfg.oldY = parentOldY;
  856. } else {
  857. lastCfg.oldShape = {
  858. x: parentOldX,
  859. y: parentOldY,
  860. width: 0,
  861. height: 0
  862. };
  863. }
  864. }
  865. // Fade in, user can be aware that these nodes are new.
  866. lastCfg.fadein = !isGroup;
  867. }
  868. }
  869. // We cannot set all background with the same z, because the behaviour of
  870. // drill down and roll up differ background creation sequence from tree
  871. // hierarchy sequence, which cause lower background elements to overlap
  872. // upper ones. So we calculate z based on depth.
  873. // Moreover, we try to shrink down z interval to [0, 1] to avoid that
  874. // treemap with large z overlaps other components.
  875. function calculateZ2(depth, z2InLevel) {
  876. return depth * Z2_BASE + z2InLevel;
  877. }
  878. export default TreemapView;