treemapVisual.js 7.8 KB

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