TreemapView.js 32 KB

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