treemapVisual.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 VisualMapping = require("../../visual/VisualMapping");
  20. var zrColor = require("zrender/lib/tool/color");
  21. var zrUtil = require("zrender/lib/core/util");
  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. var isArray = zrUtil.isArray;
  41. var ITEM_STYLE_NORMAL = 'itemStyle';
  42. var _default = {
  43. seriesType: 'treemap',
  44. reset: function (seriesModel, ecModel, api, payload) {
  45. var tree = seriesModel.getData().tree;
  46. var root = tree.root;
  47. var seriesItemStyleModel = seriesModel.getModel(ITEM_STYLE_NORMAL);
  48. if (root.isRemoved()) {
  49. return;
  50. }
  51. var levelItemStyles = zrUtil.map(tree.levelModels, function (levelModel) {
  52. return levelModel ? levelModel.get(ITEM_STYLE_NORMAL) : null;
  53. });
  54. travelTree(root, // Visual should calculate from tree root but not view root.
  55. {}, levelItemStyles, seriesItemStyleModel, seriesModel.getViewRoot().getAncestors(), seriesModel);
  56. }
  57. };
  58. function travelTree(node, designatedVisual, levelItemStyles, seriesItemStyleModel, viewRootAncestors, seriesModel) {
  59. var nodeModel = node.getModel();
  60. var nodeLayout = node.getLayout(); // Optimize
  61. if (!nodeLayout || nodeLayout.invisible || !nodeLayout.isInView) {
  62. return;
  63. }
  64. var nodeItemStyleModel = node.getModel(ITEM_STYLE_NORMAL);
  65. var levelItemStyle = levelItemStyles[node.depth];
  66. var visuals = buildVisuals(nodeItemStyleModel, designatedVisual, levelItemStyle, seriesItemStyleModel); // calculate border color
  67. var borderColor = nodeItemStyleModel.get('borderColor');
  68. var borderColorSaturation = nodeItemStyleModel.get('borderColorSaturation');
  69. var thisNodeColor;
  70. if (borderColorSaturation != null) {
  71. // For performance, do not always execute 'calculateColor'.
  72. thisNodeColor = calculateColor(visuals, node);
  73. borderColor = calculateBorderColor(borderColorSaturation, thisNodeColor);
  74. }
  75. node.setVisual('borderColor', borderColor);
  76. var viewChildren = node.viewChildren;
  77. if (!viewChildren || !viewChildren.length) {
  78. thisNodeColor = calculateColor(visuals, node); // Apply visual to this node.
  79. node.setVisual('color', thisNodeColor);
  80. } else {
  81. var mapping = buildVisualMapping(node, nodeModel, nodeLayout, nodeItemStyleModel, visuals, viewChildren); // Designate visual to children.
  82. zrUtil.each(viewChildren, function (child, index) {
  83. // If higher than viewRoot, only ancestors of viewRoot is needed to visit.
  84. if (child.depth >= viewRootAncestors.length || child === viewRootAncestors[child.depth]) {
  85. var childVisual = mapVisual(nodeModel, visuals, child, index, mapping, seriesModel);
  86. travelTree(child, childVisual, levelItemStyles, seriesItemStyleModel, viewRootAncestors, seriesModel);
  87. }
  88. });
  89. }
  90. }
  91. function buildVisuals(nodeItemStyleModel, designatedVisual, levelItemStyle, seriesItemStyleModel) {
  92. var visuals = zrUtil.extend({}, designatedVisual);
  93. zrUtil.each(['color', 'colorAlpha', 'colorSaturation'], function (visualName) {
  94. // Priority: thisNode > thisLevel > parentNodeDesignated > seriesModel
  95. var val = nodeItemStyleModel.get(visualName, true); // Ignore parent
  96. val == null && levelItemStyle && (val = levelItemStyle[visualName]);
  97. val == null && (val = designatedVisual[visualName]);
  98. val == null && (val = seriesItemStyleModel.get(visualName));
  99. val != null && (visuals[visualName] = val);
  100. });
  101. return visuals;
  102. }
  103. function calculateColor(visuals) {
  104. var color = getValueVisualDefine(visuals, 'color');
  105. if (color) {
  106. var colorAlpha = getValueVisualDefine(visuals, 'colorAlpha');
  107. var colorSaturation = getValueVisualDefine(visuals, 'colorSaturation');
  108. if (colorSaturation) {
  109. color = zrColor.modifyHSL(color, null, null, colorSaturation);
  110. }
  111. if (colorAlpha) {
  112. color = zrColor.modifyAlpha(color, colorAlpha);
  113. }
  114. return color;
  115. }
  116. }
  117. function calculateBorderColor(borderColorSaturation, thisNodeColor) {
  118. return thisNodeColor != null ? zrColor.modifyHSL(thisNodeColor, null, null, borderColorSaturation) : null;
  119. }
  120. function getValueVisualDefine(visuals, name) {
  121. var value = visuals[name];
  122. if (value != null && value !== 'none') {
  123. return value;
  124. }
  125. }
  126. function buildVisualMapping(node, nodeModel, nodeLayout, nodeItemStyleModel, visuals, viewChildren) {
  127. if (!viewChildren || !viewChildren.length) {
  128. return;
  129. }
  130. var rangeVisual = getRangeVisual(nodeModel, 'color') || visuals.color != null && visuals.color !== 'none' && (getRangeVisual(nodeModel, 'colorAlpha') || getRangeVisual(nodeModel, 'colorSaturation'));
  131. if (!rangeVisual) {
  132. return;
  133. }
  134. var visualMin = nodeModel.get('visualMin');
  135. var visualMax = nodeModel.get('visualMax');
  136. var dataExtent = nodeLayout.dataExtent.slice();
  137. visualMin != null && visualMin < dataExtent[0] && (dataExtent[0] = visualMin);
  138. visualMax != null && visualMax > dataExtent[1] && (dataExtent[1] = visualMax);
  139. var colorMappingBy = nodeModel.get('colorMappingBy');
  140. var opt = {
  141. type: rangeVisual.name,
  142. dataExtent: dataExtent,
  143. visual: rangeVisual.range
  144. };
  145. if (opt.type === 'color' && (colorMappingBy === 'index' || colorMappingBy === 'id')) {
  146. opt.mappingMethod = 'category';
  147. opt.loop = true; // categories is ordinal, so do not set opt.categories.
  148. } else {
  149. opt.mappingMethod = 'linear';
  150. }
  151. var mapping = new VisualMapping(opt);
  152. mapping.__drColorMappingBy = colorMappingBy;
  153. return mapping;
  154. } // Notice: If we dont have the attribute 'colorRange', but only use
  155. // attribute 'color' to represent both concepts of 'colorRange' and 'color',
  156. // (It means 'colorRange' when 'color' is Array, means 'color' when not array),
  157. // this problem will be encountered:
  158. // If a level-1 node dont have children, and its siblings has children,
  159. // and colorRange is set on level-1, then the node can not be colored.
  160. // So we separate 'colorRange' and 'color' to different attributes.
  161. function getRangeVisual(nodeModel, name) {
  162. // 'colorRange', 'colorARange', 'colorSRange'.
  163. // If not exsits on this node, fetch from levels and series.
  164. var range = nodeModel.get(name);
  165. return isArray(range) && range.length ? {
  166. name: name,
  167. range: range
  168. } : null;
  169. }
  170. function mapVisual(nodeModel, visuals, child, index, mapping, seriesModel) {
  171. var childVisuals = zrUtil.extend({}, visuals);
  172. if (mapping) {
  173. var mappingType = mapping.type;
  174. var colorMappingBy = mappingType === 'color' && mapping.__drColorMappingBy;
  175. var value = colorMappingBy === 'index' ? index : colorMappingBy === 'id' ? seriesModel.mapIdToIndex(child.getId()) : child.getValue(nodeModel.get('visualDimension'));
  176. childVisuals[mappingType] = mapping.mapValueToVisual(value);
  177. }
  178. return childVisuals;
  179. }
  180. module.exports = _default;