TreemapSeries.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. var zrUtil = require("zrender/lib/core/util");
  20. var SeriesModel = require("../../model/Series");
  21. var Tree = require("../../data/Tree");
  22. var Model = require("../../model/Model");
  23. var _format = require("../../util/format");
  24. var encodeHTML = _format.encodeHTML;
  25. var addCommas = _format.addCommas;
  26. var _treeHelper = require("../helper/treeHelper");
  27. var wrapTreePathInfo = _treeHelper.wrapTreePathInfo;
  28. /*
  29. * Licensed to the Apache Software Foundation (ASF) under one
  30. * or more contributor license agreements. See the NOTICE file
  31. * distributed with this work for additional information
  32. * regarding copyright ownership. The ASF licenses this file
  33. * to you under the Apache License, Version 2.0 (the
  34. * "License"); you may not use this file except in compliance
  35. * with the License. You may obtain a copy of the License at
  36. *
  37. * http://www.apache.org/licenses/LICENSE-2.0
  38. *
  39. * Unless required by applicable law or agreed to in writing,
  40. * software distributed under the License is distributed on an
  41. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  42. * KIND, either express or implied. See the License for the
  43. * specific language governing permissions and limitations
  44. * under the License.
  45. */
  46. var _default = SeriesModel.extend({
  47. type: 'series.treemap',
  48. layoutMode: 'box',
  49. dependencies: ['grid', 'polar'],
  50. preventUsingHoverLayer: true,
  51. /**
  52. * @type {module:echarts/data/Tree~Node}
  53. */
  54. _viewRoot: null,
  55. defaultOption: {
  56. // Disable progressive rendering
  57. progressive: 0,
  58. // center: ['50%', '50%'], // not supported in ec3.
  59. // size: ['80%', '80%'], // deprecated, compatible with ec2.
  60. left: 'center',
  61. top: 'middle',
  62. right: null,
  63. bottom: null,
  64. width: '80%',
  65. height: '80%',
  66. sort: true,
  67. // Can be null or false or true
  68. // (order by desc default, asc not supported yet (strange effect))
  69. clipWindow: 'origin',
  70. // Size of clipped window when zooming. 'origin' or 'fullscreen'
  71. squareRatio: 0.5 * (1 + Math.sqrt(5)),
  72. // golden ratio
  73. leafDepth: null,
  74. // Nodes on depth from root are regarded as leaves.
  75. // Count from zero (zero represents only view root).
  76. drillDownIcon: '▶',
  77. // Use html character temporarily because it is complicated
  78. // to align specialized icon. ▷▶❒❐▼✚
  79. zoomToNodeRatio: 0.32 * 0.32,
  80. // Be effective when using zoomToNode. Specify the proportion of the
  81. // target node area in the view area.
  82. roam: true,
  83. // true, false, 'scale' or 'zoom', 'move'.
  84. nodeClick: 'zoomToNode',
  85. // Leaf node click behaviour: 'zoomToNode', 'link', false.
  86. // If leafDepth is set and clicking a node which has children but
  87. // be on left depth, the behaviour would be changing root. Otherwise
  88. // use behavious defined above.
  89. animation: true,
  90. animationDurationUpdate: 900,
  91. animationEasing: 'quinticInOut',
  92. breadcrumb: {
  93. show: true,
  94. height: 22,
  95. left: 'center',
  96. top: 'bottom',
  97. // right
  98. // bottom
  99. emptyItemWidth: 25,
  100. // Width of empty node.
  101. itemStyle: {
  102. color: 'rgba(0,0,0,0.7)',
  103. //'#5793f3',
  104. borderColor: 'rgba(255,255,255,0.7)',
  105. borderWidth: 1,
  106. shadowColor: 'rgba(150,150,150,1)',
  107. shadowBlur: 3,
  108. shadowOffsetX: 0,
  109. shadowOffsetY: 0,
  110. textStyle: {
  111. color: '#fff'
  112. }
  113. },
  114. emphasis: {
  115. textStyle: {}
  116. }
  117. },
  118. label: {
  119. show: true,
  120. // Do not use textDistance, for ellipsis rect just the same as treemap node rect.
  121. distance: 0,
  122. padding: 5,
  123. position: 'inside',
  124. // Can be [5, '5%'] or position stirng like 'insideTopLeft', ...
  125. // formatter: null,
  126. color: '#fff',
  127. ellipsis: true // align
  128. // verticalAlign
  129. },
  130. upperLabel: {
  131. // Label when node is parent.
  132. show: false,
  133. position: [0, '50%'],
  134. height: 20,
  135. // formatter: null,
  136. color: '#fff',
  137. ellipsis: true,
  138. // align: null,
  139. verticalAlign: 'middle'
  140. },
  141. itemStyle: {
  142. color: null,
  143. // Can be 'none' if not necessary.
  144. colorAlpha: null,
  145. // Can be 'none' if not necessary.
  146. colorSaturation: null,
  147. // Can be 'none' if not necessary.
  148. borderWidth: 0,
  149. gapWidth: 0,
  150. borderColor: '#fff',
  151. borderColorSaturation: null // If specified, borderColor will be ineffective, and the
  152. // border color is evaluated by color of current node and
  153. // borderColorSaturation.
  154. },
  155. emphasis: {
  156. upperLabel: {
  157. show: true,
  158. position: [0, '50%'],
  159. color: '#fff',
  160. ellipsis: true,
  161. verticalAlign: 'middle'
  162. }
  163. },
  164. visualDimension: 0,
  165. // Can be 0, 1, 2, 3.
  166. visualMin: null,
  167. visualMax: null,
  168. color: [],
  169. // + treemapSeries.color should not be modified. Please only modified
  170. // level[n].color (if necessary).
  171. // + Specify color list of each level. level[0].color would be global
  172. // color list if not specified. (see method `setDefault`).
  173. // + But set as a empty array to forbid fetch color from global palette
  174. // when using nodeModel.get('color'), otherwise nodes on deep level
  175. // will always has color palette set and are not able to inherit color
  176. // from parent node.
  177. // + TreemapSeries.color can not be set as 'none', otherwise effect
  178. // legend color fetching (see seriesColor.js).
  179. colorAlpha: null,
  180. // Array. Specify color alpha range of each level, like [0.2, 0.8]
  181. colorSaturation: null,
  182. // Array. Specify color saturation of each level, like [0.2, 0.5]
  183. colorMappingBy: 'index',
  184. // 'value' or 'index' or 'id'.
  185. visibleMin: 10,
  186. // If area less than this threshold (unit: pixel^2), node will not
  187. // be rendered. Only works when sort is 'asc' or 'desc'.
  188. childrenVisibleMin: null,
  189. // If area of a node less than this threshold (unit: pixel^2),
  190. // grandchildren will not show.
  191. // Why grandchildren? If not grandchildren but children,
  192. // some siblings show children and some not,
  193. // the appearance may be mess and not consistent,
  194. levels: [] // Each item: {
  195. // visibleMin, itemStyle, visualDimension, label
  196. // }
  197. // data: {
  198. // value: [],
  199. // children: [],
  200. // link: 'http://xxx.xxx.xxx',
  201. // target: 'blank' or 'self'
  202. // }
  203. },
  204. /**
  205. * @override
  206. */
  207. getInitialData: function (option, ecModel) {
  208. // Create a virtual root.
  209. var root = {
  210. name: option.name,
  211. children: option.data
  212. };
  213. completeTreeValue(root);
  214. var levels = option.levels || [];
  215. levels = option.levels = setDefault(levels, ecModel);
  216. var treeOption = {};
  217. treeOption.levels = levels; // Make sure always a new tree is created when setOption,
  218. // in TreemapView, we check whether oldTree === newTree
  219. // to choose mappings approach among old shapes and new shapes.
  220. return Tree.createTree(root, this, treeOption).data;
  221. },
  222. optionUpdated: function () {
  223. this.resetViewRoot();
  224. },
  225. /**
  226. * @override
  227. * @param {number} dataIndex
  228. * @param {boolean} [mutipleSeries=false]
  229. */
  230. formatTooltip: function (dataIndex) {
  231. var data = this.getData();
  232. var value = this.getRawValue(dataIndex);
  233. var formattedValue = zrUtil.isArray(value) ? addCommas(value[0]) : addCommas(value);
  234. var name = data.getName(dataIndex);
  235. return encodeHTML(name + ': ' + formattedValue);
  236. },
  237. /**
  238. * Add tree path to tooltip param
  239. *
  240. * @override
  241. * @param {number} dataIndex
  242. * @return {Object}
  243. */
  244. getDataParams: function (dataIndex) {
  245. var params = SeriesModel.prototype.getDataParams.apply(this, arguments);
  246. var node = this.getData().tree.getNodeByDataIndex(dataIndex);
  247. params.treePathInfo = wrapTreePathInfo(node, this);
  248. return params;
  249. },
  250. /**
  251. * @public
  252. * @param {Object} layoutInfo {
  253. * x: containerGroup x
  254. * y: containerGroup y
  255. * width: containerGroup width
  256. * height: containerGroup height
  257. * }
  258. */
  259. setLayoutInfo: function (layoutInfo) {
  260. /**
  261. * @readOnly
  262. * @type {Object}
  263. */
  264. this.layoutInfo = this.layoutInfo || {};
  265. zrUtil.extend(this.layoutInfo, layoutInfo);
  266. },
  267. /**
  268. * @param {string} id
  269. * @return {number} index
  270. */
  271. mapIdToIndex: function (id) {
  272. // A feature is implemented:
  273. // index is monotone increasing with the sequence of
  274. // input id at the first time.
  275. // This feature can make sure that each data item and its
  276. // mapped color have the same index between data list and
  277. // color list at the beginning, which is useful for user
  278. // to adjust data-color mapping.
  279. /**
  280. * @private
  281. * @type {Object}
  282. */
  283. var idIndexMap = this._idIndexMap;
  284. if (!idIndexMap) {
  285. idIndexMap = this._idIndexMap = zrUtil.createHashMap();
  286. /**
  287. * @private
  288. * @type {number}
  289. */
  290. this._idIndexMapCount = 0;
  291. }
  292. var index = idIndexMap.get(id);
  293. if (index == null) {
  294. idIndexMap.set(id, index = this._idIndexMapCount++);
  295. }
  296. return index;
  297. },
  298. getViewRoot: function () {
  299. return this._viewRoot;
  300. },
  301. /**
  302. * @param {module:echarts/data/Tree~Node} [viewRoot]
  303. */
  304. resetViewRoot: function (viewRoot) {
  305. viewRoot ? this._viewRoot = viewRoot : viewRoot = this._viewRoot;
  306. var root = this.getRawData().tree.root;
  307. if (!viewRoot || viewRoot !== root && !root.contains(viewRoot)) {
  308. this._viewRoot = root;
  309. }
  310. }
  311. });
  312. /**
  313. * @param {Object} dataNode
  314. */
  315. function completeTreeValue(dataNode) {
  316. // Postorder travel tree.
  317. // If value of none-leaf node is not set,
  318. // calculate it by suming up the value of all children.
  319. var sum = 0;
  320. zrUtil.each(dataNode.children, function (child) {
  321. completeTreeValue(child);
  322. var childValue = child.value;
  323. zrUtil.isArray(childValue) && (childValue = childValue[0]);
  324. sum += childValue;
  325. });
  326. var thisValue = dataNode.value;
  327. if (zrUtil.isArray(thisValue)) {
  328. thisValue = thisValue[0];
  329. }
  330. if (thisValue == null || isNaN(thisValue)) {
  331. thisValue = sum;
  332. } // Value should not less than 0.
  333. if (thisValue < 0) {
  334. thisValue = 0;
  335. }
  336. zrUtil.isArray(dataNode.value) ? dataNode.value[0] = thisValue : dataNode.value = thisValue;
  337. }
  338. /**
  339. * set default to level configuration
  340. */
  341. function setDefault(levels, ecModel) {
  342. var globalColorList = ecModel.get('color');
  343. if (!globalColorList) {
  344. return;
  345. }
  346. levels = levels || [];
  347. var hasColorDefine;
  348. zrUtil.each(levels, function (levelDefine) {
  349. var model = new Model(levelDefine);
  350. var modelColor = model.get('color');
  351. if (model.get('itemStyle.color') || modelColor && modelColor !== 'none') {
  352. hasColorDefine = true;
  353. }
  354. });
  355. if (!hasColorDefine) {
  356. var level0 = levels[0] || (levels[0] = {});
  357. level0.color = globalColorList.slice();
  358. }
  359. return levels;
  360. }
  361. module.exports = _default;