TreemapSeries.js 12 KB

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