TreemapSeries.js 12 KB

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