treemapVisual.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. var VisualMapping = require("../../visual/VisualMapping");
  2. var zrColor = require("zrender/lib/tool/color");
  3. var zrUtil = require("zrender/lib/core/util");
  4. var isArray = zrUtil.isArray;
  5. var ITEM_STYLE_NORMAL = 'itemStyle.normal';
  6. function _default(ecModel, api, payload) {
  7. var condition = {
  8. mainType: 'series',
  9. subType: 'treemap',
  10. query: payload
  11. };
  12. ecModel.eachComponent(condition, function (seriesModel) {
  13. var tree = seriesModel.getData().tree;
  14. var root = tree.root;
  15. var seriesItemStyleModel = seriesModel.getModel(ITEM_STYLE_NORMAL);
  16. if (root.isRemoved()) {
  17. return;
  18. }
  19. var levelItemStyles = zrUtil.map(tree.levelModels, function (levelModel) {
  20. return levelModel ? levelModel.get(ITEM_STYLE_NORMAL) : null;
  21. });
  22. travelTree(root, // Visual should calculate from tree root but not view root.
  23. {}, levelItemStyles, seriesItemStyleModel, seriesModel.getViewRoot().getAncestors(), seriesModel);
  24. });
  25. }
  26. function travelTree(node, designatedVisual, levelItemStyles, seriesItemStyleModel, viewRootAncestors, seriesModel) {
  27. var nodeModel = node.getModel();
  28. var nodeLayout = node.getLayout(); // Optimize
  29. if (!nodeLayout || nodeLayout.invisible || !nodeLayout.isInView) {
  30. return;
  31. }
  32. var nodeItemStyleModel = node.getModel(ITEM_STYLE_NORMAL);
  33. var levelItemStyle = levelItemStyles[node.depth];
  34. var visuals = buildVisuals(nodeItemStyleModel, designatedVisual, levelItemStyle, seriesItemStyleModel); // calculate border color
  35. var borderColor = nodeItemStyleModel.get('borderColor');
  36. var borderColorSaturation = nodeItemStyleModel.get('borderColorSaturation');
  37. var thisNodeColor;
  38. if (borderColorSaturation != null) {
  39. // For performance, do not always execute 'calculateColor'.
  40. thisNodeColor = calculateColor(visuals, node);
  41. borderColor = calculateBorderColor(borderColorSaturation, thisNodeColor);
  42. }
  43. node.setVisual('borderColor', borderColor);
  44. var viewChildren = node.viewChildren;
  45. if (!viewChildren || !viewChildren.length) {
  46. thisNodeColor = calculateColor(visuals, node); // Apply visual to this node.
  47. node.setVisual('color', thisNodeColor);
  48. } else {
  49. var mapping = buildVisualMapping(node, nodeModel, nodeLayout, nodeItemStyleModel, visuals, viewChildren); // Designate visual to children.
  50. zrUtil.each(viewChildren, function (child, index) {
  51. // If higher than viewRoot, only ancestors of viewRoot is needed to visit.
  52. if (child.depth >= viewRootAncestors.length || child === viewRootAncestors[child.depth]) {
  53. var childVisual = mapVisual(nodeModel, visuals, child, index, mapping, seriesModel);
  54. travelTree(child, childVisual, levelItemStyles, seriesItemStyleModel, viewRootAncestors, seriesModel);
  55. }
  56. });
  57. }
  58. }
  59. function buildVisuals(nodeItemStyleModel, designatedVisual, levelItemStyle, seriesItemStyleModel) {
  60. var visuals = zrUtil.extend({}, designatedVisual);
  61. zrUtil.each(['color', 'colorAlpha', 'colorSaturation'], function (visualName) {
  62. // Priority: thisNode > thisLevel > parentNodeDesignated > seriesModel
  63. var val = nodeItemStyleModel.get(visualName, true); // Ignore parent
  64. val == null && levelItemStyle && (val = levelItemStyle[visualName]);
  65. val == null && (val = designatedVisual[visualName]);
  66. val == null && (val = seriesItemStyleModel.get(visualName));
  67. val != null && (visuals[visualName] = val);
  68. });
  69. return visuals;
  70. }
  71. function calculateColor(visuals) {
  72. var color = getValueVisualDefine(visuals, 'color');
  73. if (color) {
  74. var colorAlpha = getValueVisualDefine(visuals, 'colorAlpha');
  75. var colorSaturation = getValueVisualDefine(visuals, 'colorSaturation');
  76. if (colorSaturation) {
  77. color = zrColor.modifyHSL(color, null, null, colorSaturation);
  78. }
  79. if (colorAlpha) {
  80. color = zrColor.modifyAlpha(color, colorAlpha);
  81. }
  82. return color;
  83. }
  84. }
  85. function calculateBorderColor(borderColorSaturation, thisNodeColor) {
  86. return thisNodeColor != null ? zrColor.modifyHSL(thisNodeColor, null, null, borderColorSaturation) : null;
  87. }
  88. function getValueVisualDefine(visuals, name) {
  89. var value = visuals[name];
  90. if (value != null && value !== 'none') {
  91. return value;
  92. }
  93. }
  94. function buildVisualMapping(node, nodeModel, nodeLayout, nodeItemStyleModel, visuals, viewChildren) {
  95. if (!viewChildren || !viewChildren.length) {
  96. return;
  97. }
  98. var rangeVisual = getRangeVisual(nodeModel, 'color') || visuals.color != null && visuals.color !== 'none' && (getRangeVisual(nodeModel, 'colorAlpha') || getRangeVisual(nodeModel, 'colorSaturation'));
  99. if (!rangeVisual) {
  100. return;
  101. }
  102. var visualMin = nodeModel.get('visualMin');
  103. var visualMax = nodeModel.get('visualMax');
  104. var dataExtent = nodeLayout.dataExtent.slice();
  105. visualMin != null && visualMin < dataExtent[0] && (dataExtent[0] = visualMin);
  106. visualMax != null && visualMax > dataExtent[1] && (dataExtent[1] = visualMax);
  107. var colorMappingBy = nodeModel.get('colorMappingBy');
  108. var opt = {
  109. type: rangeVisual.name,
  110. dataExtent: dataExtent,
  111. visual: rangeVisual.range
  112. };
  113. if (opt.type === 'color' && (colorMappingBy === 'index' || colorMappingBy === 'id')) {
  114. opt.mappingMethod = 'category';
  115. opt.loop = true; // categories is ordinal, so do not set opt.categories.
  116. } else {
  117. opt.mappingMethod = 'linear';
  118. }
  119. var mapping = new VisualMapping(opt);
  120. mapping.__drColorMappingBy = colorMappingBy;
  121. return mapping;
  122. } // Notice: If we dont have the attribute 'colorRange', but only use
  123. // attribute 'color' to represent both concepts of 'colorRange' and 'color',
  124. // (It means 'colorRange' when 'color' is Array, means 'color' when not array),
  125. // this problem will be encountered:
  126. // If a level-1 node dont have children, and its siblings has children,
  127. // and colorRange is set on level-1, then the node can not be colored.
  128. // So we separate 'colorRange' and 'color' to different attributes.
  129. function getRangeVisual(nodeModel, name) {
  130. // 'colorRange', 'colorARange', 'colorSRange'.
  131. // If not exsits on this node, fetch from levels and series.
  132. var range = nodeModel.get(name);
  133. return isArray(range) && range.length ? {
  134. name: name,
  135. range: range
  136. } : null;
  137. }
  138. function mapVisual(nodeModel, visuals, child, index, mapping, seriesModel) {
  139. var childVisuals = zrUtil.extend({}, visuals);
  140. if (mapping) {
  141. var mappingType = mapping.type;
  142. var colorMappingBy = mappingType === 'color' && mapping.__drColorMappingBy;
  143. var value = colorMappingBy === 'index' ? index : colorMappingBy === 'id' ? seriesModel.mapIdToIndex(child.getId()) : child.getValue(nodeModel.get('visualDimension'));
  144. childVisuals[mappingType] = mapping.mapValueToVisual(value);
  145. }
  146. return childVisuals;
  147. }
  148. module.exports = _default;