treemapVisual.js 7.8 KB

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