treemapVisual.js 7.6 KB

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