TreemapView.js 32 KB

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