TreemapSeries.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 * as zrUtil from 'zrender/lib/core/util.js';
  42. import SeriesModel from '../../model/Series.js';
  43. import Tree from '../../data/Tree.js';
  44. import Model from '../../model/Model.js';
  45. import { wrapTreePathInfo } from '../helper/treeHelper.js';
  46. import { normalizeToArray } from '../../util/model.js';
  47. import { createTooltipMarkup } from '../../component/tooltip/tooltipMarkup.js';
  48. import enableAriaDecalForTree from '../helper/enableAriaDecalForTree.js';
  49. import tokens from '../../visual/tokens.js';
  50. var TreemapSeriesModel = /** @class */function (_super) {
  51. __extends(TreemapSeriesModel, _super);
  52. function TreemapSeriesModel() {
  53. var _this = _super !== null && _super.apply(this, arguments) || this;
  54. _this.type = TreemapSeriesModel.type;
  55. _this.preventUsingHoverLayer = true;
  56. return _this;
  57. }
  58. /**
  59. * @override
  60. */
  61. TreemapSeriesModel.prototype.getInitialData = function (option, ecModel) {
  62. // Create a virtual root.
  63. var root = {
  64. name: option.name,
  65. children: option.data
  66. };
  67. completeTreeValue(root);
  68. var levels = option.levels || [];
  69. // Used in "visual priority" in `treemapVisual.js`.
  70. // This way is a little tricky, must satisfy the precondition:
  71. // 1. There is no `treeNode.getModel('itemStyle.xxx')` used.
  72. // 2. The `Model.prototype.getModel()` will not use any clone-like way.
  73. var designatedVisualItemStyle = this.designatedVisualItemStyle = {};
  74. var designatedVisualModel = new Model({
  75. itemStyle: designatedVisualItemStyle
  76. }, this, ecModel);
  77. levels = option.levels = setDefault(levels, ecModel);
  78. var levelModels = zrUtil.map(levels || [], function (levelDefine) {
  79. return new Model(levelDefine, designatedVisualModel, ecModel);
  80. }, this);
  81. // Make sure always a new tree is created when setOption,
  82. // in TreemapView, we check whether oldTree === newTree
  83. // to choose mappings approach among old shapes and new shapes.
  84. var tree = Tree.createTree(root, this, beforeLink);
  85. function beforeLink(nodeData) {
  86. nodeData.wrapMethod('getItemModel', function (model, idx) {
  87. var node = tree.getNodeByDataIndex(idx);
  88. var levelModel = node ? levelModels[node.depth] : null;
  89. // If no levelModel, we also need `designatedVisualModel`.
  90. model.parentModel = levelModel || designatedVisualModel;
  91. return model;
  92. });
  93. }
  94. return tree.data;
  95. };
  96. TreemapSeriesModel.prototype.optionUpdated = function () {
  97. this.resetViewRoot();
  98. };
  99. /**
  100. * @override
  101. * @param {number} dataIndex
  102. * @param {boolean} [mutipleSeries=false]
  103. */
  104. TreemapSeriesModel.prototype.formatTooltip = function (dataIndex, multipleSeries, dataType) {
  105. var data = this.getData();
  106. var value = this.getRawValue(dataIndex);
  107. var name = data.getName(dataIndex);
  108. return createTooltipMarkup('nameValue', {
  109. name: name,
  110. value: value
  111. });
  112. };
  113. /**
  114. * Add tree path to tooltip param
  115. *
  116. * @override
  117. * @param {number} dataIndex
  118. * @return {Object}
  119. */
  120. TreemapSeriesModel.prototype.getDataParams = function (dataIndex) {
  121. var params = _super.prototype.getDataParams.apply(this, arguments);
  122. var node = this.getData().tree.getNodeByDataIndex(dataIndex);
  123. params.treeAncestors = wrapTreePathInfo(node, this);
  124. // compatitable the previous code.
  125. params.treePathInfo = params.treeAncestors;
  126. return params;
  127. };
  128. /**
  129. * @public
  130. * @param {Object} layoutInfo {
  131. * x: containerGroup x
  132. * y: containerGroup y
  133. * width: containerGroup width
  134. * height: containerGroup height
  135. * }
  136. */
  137. TreemapSeriesModel.prototype.setLayoutInfo = function (layoutInfo) {
  138. /**
  139. * @readOnly
  140. * @type {Object}
  141. */
  142. this.layoutInfo = this.layoutInfo || {};
  143. zrUtil.extend(this.layoutInfo, layoutInfo);
  144. };
  145. /**
  146. * @param {string} id
  147. * @return {number} index
  148. */
  149. TreemapSeriesModel.prototype.mapIdToIndex = function (id) {
  150. // A feature is implemented:
  151. // index is monotone increasing with the sequence of
  152. // input id at the first time.
  153. // This feature can make sure that each data item and its
  154. // mapped color have the same index between data list and
  155. // color list at the beginning, which is useful for user
  156. // to adjust data-color mapping.
  157. /**
  158. * @private
  159. * @type {Object}
  160. */
  161. var idIndexMap = this._idIndexMap;
  162. if (!idIndexMap) {
  163. idIndexMap = this._idIndexMap = zrUtil.createHashMap();
  164. /**
  165. * @private
  166. * @type {number}
  167. */
  168. this._idIndexMapCount = 0;
  169. }
  170. var index = idIndexMap.get(id);
  171. if (index == null) {
  172. idIndexMap.set(id, index = this._idIndexMapCount++);
  173. }
  174. return index;
  175. };
  176. TreemapSeriesModel.prototype.getViewRoot = function () {
  177. return this._viewRoot;
  178. };
  179. TreemapSeriesModel.prototype.resetViewRoot = function (viewRoot) {
  180. viewRoot ? this._viewRoot = viewRoot : viewRoot = this._viewRoot;
  181. var root = this.getRawData().tree.root;
  182. if (!viewRoot || viewRoot !== root && !root.contains(viewRoot)) {
  183. this._viewRoot = root;
  184. }
  185. };
  186. TreemapSeriesModel.prototype.enableAriaDecal = function () {
  187. enableAriaDecalForTree(this);
  188. };
  189. TreemapSeriesModel.type = 'series.treemap';
  190. TreemapSeriesModel.layoutMode = 'box';
  191. TreemapSeriesModel.defaultOption = {
  192. // Disable progressive rendering
  193. progressive: 0,
  194. // size: ['80%', '80%'], // deprecated, compatible with ec2.
  195. // `coordinateSystem` can be declared as 'matrix', 'calendar',
  196. // which provides box layout container.
  197. coordinateSystemUsage: 'box',
  198. left: tokens.size.l,
  199. top: tokens.size.xxxl,
  200. right: tokens.size.l,
  201. bottom: tokens.size.xxxl,
  202. sort: true,
  203. clipWindow: 'origin',
  204. squareRatio: 0.5 * (1 + Math.sqrt(5)),
  205. leafDepth: null,
  206. drillDownIcon: '▶',
  207. // to align specialized icon. ▷▶❒❐▼✚
  208. zoomToNodeRatio: 0.32 * 0.32,
  209. scaleLimit: {
  210. max: 5,
  211. min: 0.2
  212. },
  213. roam: true,
  214. roamTrigger: 'global',
  215. nodeClick: 'zoomToNode',
  216. animation: true,
  217. animationDurationUpdate: 900,
  218. animationEasing: 'quinticInOut',
  219. breadcrumb: {
  220. show: true,
  221. height: 22,
  222. left: 'center',
  223. bottom: tokens.size.m,
  224. // right
  225. // bottom
  226. emptyItemWidth: 25,
  227. itemStyle: {
  228. color: tokens.color.backgroundShade,
  229. textStyle: {
  230. color: tokens.color.secondary
  231. }
  232. },
  233. emphasis: {
  234. itemStyle: {
  235. color: tokens.color.background
  236. }
  237. }
  238. },
  239. label: {
  240. show: true,
  241. // Do not use textDistance, for ellipsis rect just the same as treemap node rect.
  242. distance: 0,
  243. padding: 5,
  244. position: 'inside',
  245. // formatter: null,
  246. color: tokens.color.neutral00,
  247. overflow: 'truncate'
  248. // align
  249. // verticalAlign
  250. },
  251. upperLabel: {
  252. show: false,
  253. position: [0, '50%'],
  254. height: 20,
  255. // formatter: null,
  256. // color: '#fff',
  257. overflow: 'truncate',
  258. // align: null,
  259. verticalAlign: 'middle'
  260. },
  261. itemStyle: {
  262. color: null,
  263. colorAlpha: null,
  264. colorSaturation: null,
  265. borderWidth: 0,
  266. gapWidth: 0,
  267. borderColor: tokens.color.neutral00,
  268. borderColorSaturation: null // If specified, borderColor will be ineffective, and the
  269. // border color is evaluated by color of current node and
  270. // borderColorSaturation.
  271. },
  272. emphasis: {
  273. upperLabel: {
  274. show: true,
  275. position: [0, '50%'],
  276. overflow: 'truncate',
  277. verticalAlign: 'middle'
  278. }
  279. },
  280. visualDimension: 0,
  281. visualMin: null,
  282. visualMax: null,
  283. color: [],
  284. // level[n].color (if necessary).
  285. // + Specify color list of each level. level[0].color would be global
  286. // color list if not specified. (see method `setDefault`).
  287. // + But set as a empty array to forbid fetch color from global palette
  288. // when using nodeModel.get('color'), otherwise nodes on deep level
  289. // will always has color palette set and are not able to inherit color
  290. // from parent node.
  291. // + TreemapSeries.color can not be set as 'none', otherwise effect
  292. // legend color fetching (see seriesColor.js).
  293. colorAlpha: null,
  294. colorSaturation: null,
  295. colorMappingBy: 'index',
  296. visibleMin: 10,
  297. // be rendered. Only works when sort is 'asc' or 'desc'.
  298. childrenVisibleMin: null,
  299. // grandchildren will not show.
  300. // Why grandchildren? If not grandchildren but children,
  301. // some siblings show children and some not,
  302. // the appearance may be mess and not consistent,
  303. levels: [] // Each item: {
  304. // visibleMin, itemStyle, visualDimension, label
  305. // }
  306. };
  307. return TreemapSeriesModel;
  308. }(SeriesModel);
  309. /**
  310. * @param {Object} dataNode
  311. */
  312. function completeTreeValue(dataNode) {
  313. // Postorder travel tree.
  314. // If value of none-leaf node is not set,
  315. // calculate it by suming up the value of all children.
  316. var sum = 0;
  317. zrUtil.each(dataNode.children, function (child) {
  318. completeTreeValue(child);
  319. var childValue = child.value;
  320. zrUtil.isArray(childValue) && (childValue = childValue[0]);
  321. sum += childValue;
  322. });
  323. var thisValue = dataNode.value;
  324. if (zrUtil.isArray(thisValue)) {
  325. thisValue = thisValue[0];
  326. }
  327. if (thisValue == null || isNaN(thisValue)) {
  328. thisValue = sum;
  329. }
  330. // Value should not less than 0.
  331. if (thisValue < 0) {
  332. thisValue = 0;
  333. }
  334. zrUtil.isArray(dataNode.value) ? dataNode.value[0] = thisValue : dataNode.value = thisValue;
  335. }
  336. /**
  337. * set default to level configuration
  338. */
  339. function setDefault(levels, ecModel) {
  340. var globalColorList = normalizeToArray(ecModel.get('color'));
  341. var globalDecalList = normalizeToArray(ecModel.get(['aria', 'decal', 'decals']));
  342. if (!globalColorList) {
  343. return;
  344. }
  345. levels = levels || [];
  346. var hasColorDefine;
  347. var hasDecalDefine;
  348. zrUtil.each(levels, function (levelDefine) {
  349. var model = new Model(levelDefine);
  350. var modelColor = model.get('color');
  351. var modelDecal = model.get('decal');
  352. if (model.get(['itemStyle', 'color']) || modelColor && modelColor !== 'none') {
  353. hasColorDefine = true;
  354. }
  355. if (model.get(['itemStyle', 'decal']) || modelDecal && modelDecal !== 'none') {
  356. hasDecalDefine = true;
  357. }
  358. });
  359. var level0 = levels[0] || (levels[0] = {});
  360. if (!hasColorDefine) {
  361. level0.color = globalColorList.slice();
  362. }
  363. if (!hasDecalDefine && globalDecalList) {
  364. level0.decal = globalDecalList.slice();
  365. }
  366. return levels;
  367. }
  368. export default TreemapSeriesModel;