TreemapSeries.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. scaleLimit: null,
  206. roam: true,
  207. nodeClick: 'zoomToNode',
  208. animation: true,
  209. animationDurationUpdate: 900,
  210. animationEasing: 'quinticInOut',
  211. breadcrumb: {
  212. show: true,
  213. height: 22,
  214. left: 'center',
  215. top: 'bottom',
  216. // right
  217. // bottom
  218. emptyItemWidth: 25,
  219. itemStyle: {
  220. color: 'rgba(0,0,0,0.7)',
  221. textStyle: {
  222. color: '#fff'
  223. }
  224. },
  225. emphasis: {
  226. itemStyle: {
  227. color: 'rgba(0,0,0,0.9)' // '#5793f3',
  228. }
  229. }
  230. },
  231. label: {
  232. show: true,
  233. // Do not use textDistance, for ellipsis rect just the same as treemap node rect.
  234. distance: 0,
  235. padding: 5,
  236. position: 'inside',
  237. // formatter: null,
  238. color: '#fff',
  239. overflow: 'truncate'
  240. // align
  241. // verticalAlign
  242. },
  243. upperLabel: {
  244. show: false,
  245. position: [0, '50%'],
  246. height: 20,
  247. // formatter: null,
  248. // color: '#fff',
  249. overflow: 'truncate',
  250. // align: null,
  251. verticalAlign: 'middle'
  252. },
  253. itemStyle: {
  254. color: null,
  255. colorAlpha: null,
  256. colorSaturation: null,
  257. borderWidth: 0,
  258. gapWidth: 0,
  259. borderColor: '#fff',
  260. borderColorSaturation: null // If specified, borderColor will be ineffective, and the
  261. // border color is evaluated by color of current node and
  262. // borderColorSaturation.
  263. },
  264. emphasis: {
  265. upperLabel: {
  266. show: true,
  267. position: [0, '50%'],
  268. overflow: 'truncate',
  269. verticalAlign: 'middle'
  270. }
  271. },
  272. visualDimension: 0,
  273. visualMin: null,
  274. visualMax: null,
  275. color: [],
  276. // level[n].color (if necessary).
  277. // + Specify color list of each level. level[0].color would be global
  278. // color list if not specified. (see method `setDefault`).
  279. // + But set as a empty array to forbid fetch color from global palette
  280. // when using nodeModel.get('color'), otherwise nodes on deep level
  281. // will always has color palette set and are not able to inherit color
  282. // from parent node.
  283. // + TreemapSeries.color can not be set as 'none', otherwise effect
  284. // legend color fetching (see seriesColor.js).
  285. colorAlpha: null,
  286. colorSaturation: null,
  287. colorMappingBy: 'index',
  288. visibleMin: 10,
  289. // be rendered. Only works when sort is 'asc' or 'desc'.
  290. childrenVisibleMin: null,
  291. // grandchildren will not show.
  292. // Why grandchildren? If not grandchildren but children,
  293. // some siblings show children and some not,
  294. // the appearance may be mess and not consistent,
  295. levels: [] // Each item: {
  296. // visibleMin, itemStyle, visualDimension, label
  297. // }
  298. };
  299. return TreemapSeriesModel;
  300. }(SeriesModel);
  301. /**
  302. * @param {Object} dataNode
  303. */
  304. function completeTreeValue(dataNode) {
  305. // Postorder travel tree.
  306. // If value of none-leaf node is not set,
  307. // calculate it by suming up the value of all children.
  308. var sum = 0;
  309. zrUtil.each(dataNode.children, function (child) {
  310. completeTreeValue(child);
  311. var childValue = child.value;
  312. zrUtil.isArray(childValue) && (childValue = childValue[0]);
  313. sum += childValue;
  314. });
  315. var thisValue = dataNode.value;
  316. if (zrUtil.isArray(thisValue)) {
  317. thisValue = thisValue[0];
  318. }
  319. if (thisValue == null || isNaN(thisValue)) {
  320. thisValue = sum;
  321. }
  322. // Value should not less than 0.
  323. if (thisValue < 0) {
  324. thisValue = 0;
  325. }
  326. zrUtil.isArray(dataNode.value) ? dataNode.value[0] = thisValue : dataNode.value = thisValue;
  327. }
  328. /**
  329. * set default to level configuration
  330. */
  331. function setDefault(levels, ecModel) {
  332. var globalColorList = normalizeToArray(ecModel.get('color'));
  333. var globalDecalList = normalizeToArray(ecModel.get(['aria', 'decal', 'decals']));
  334. if (!globalColorList) {
  335. return;
  336. }
  337. levels = levels || [];
  338. var hasColorDefine;
  339. var hasDecalDefine;
  340. zrUtil.each(levels, function (levelDefine) {
  341. var model = new Model(levelDefine);
  342. var modelColor = model.get('color');
  343. var modelDecal = model.get('decal');
  344. if (model.get(['itemStyle', 'color']) || modelColor && modelColor !== 'none') {
  345. hasColorDefine = true;
  346. }
  347. if (model.get(['itemStyle', 'decal']) || modelDecal && modelDecal !== 'none') {
  348. hasDecalDefine = true;
  349. }
  350. });
  351. var level0 = levels[0] || (levels[0] = {});
  352. if (!hasColorDefine) {
  353. level0.color = globalColorList.slice();
  354. }
  355. if (!hasDecalDefine && globalDecalList) {
  356. level0.decal = globalDecalList.slice();
  357. }
  358. return levels;
  359. }
  360. export default TreemapSeriesModel;