Breadcrumb.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 * as graphic from '../../util/graphic.js';
  41. import { getECData } from '../../util/innerStore.js';
  42. import * as layout from '../../util/layout.js';
  43. import { wrapTreePathInfo } from '../helper/treeHelper.js';
  44. import { curry, defaults } from 'zrender/lib/core/util.js';
  45. import { convertOptionIdName } from '../../util/model.js';
  46. import { toggleHoverEmphasis, Z2_EMPHASIS_LIFT } from '../../util/states.js';
  47. import { createTextStyle } from '../../label/labelStyle.js';
  48. var TEXT_PADDING = 8;
  49. var ITEM_GAP = 8;
  50. var ARRAY_LENGTH = 5;
  51. var Breadcrumb = /** @class */function () {
  52. function Breadcrumb(containerGroup) {
  53. this.group = new graphic.Group();
  54. containerGroup.add(this.group);
  55. }
  56. Breadcrumb.prototype.render = function (seriesModel, api, targetNode, onSelect) {
  57. var model = seriesModel.getModel('breadcrumb');
  58. var thisGroup = this.group;
  59. thisGroup.removeAll();
  60. if (!model.get('show') || !targetNode) {
  61. return;
  62. }
  63. var normalStyleModel = model.getModel('itemStyle');
  64. var emphasisModel = model.getModel('emphasis');
  65. var textStyleModel = normalStyleModel.getModel('textStyle');
  66. var emphasisTextStyleModel = emphasisModel.getModel(['itemStyle', 'textStyle']);
  67. var layoutParam = {
  68. pos: {
  69. left: model.get('left'),
  70. right: model.get('right'),
  71. top: model.get('top'),
  72. bottom: model.get('bottom')
  73. },
  74. box: {
  75. width: api.getWidth(),
  76. height: api.getHeight()
  77. },
  78. emptyItemWidth: model.get('emptyItemWidth'),
  79. totalWidth: 0,
  80. renderList: []
  81. };
  82. this._prepare(targetNode, layoutParam, textStyleModel);
  83. this._renderContent(seriesModel, layoutParam, normalStyleModel, emphasisModel, textStyleModel, emphasisTextStyleModel, onSelect);
  84. layout.positionElement(thisGroup, layoutParam.pos, layoutParam.box);
  85. };
  86. /**
  87. * Prepare render list and total width
  88. * @private
  89. */
  90. Breadcrumb.prototype._prepare = function (targetNode, layoutParam, textStyleModel) {
  91. for (var node = targetNode; node; node = node.parentNode) {
  92. var text = convertOptionIdName(node.getModel().get('name'), '');
  93. var textRect = textStyleModel.getTextRect(text);
  94. var itemWidth = Math.max(textRect.width + TEXT_PADDING * 2, layoutParam.emptyItemWidth);
  95. layoutParam.totalWidth += itemWidth + ITEM_GAP;
  96. layoutParam.renderList.push({
  97. node: node,
  98. text: text,
  99. width: itemWidth
  100. });
  101. }
  102. };
  103. /**
  104. * @private
  105. */
  106. Breadcrumb.prototype._renderContent = function (seriesModel, layoutParam, normalStyleModel, emphasisModel, textStyleModel, emphasisTextStyleModel, onSelect) {
  107. // Start rendering.
  108. var lastX = 0;
  109. var emptyItemWidth = layoutParam.emptyItemWidth;
  110. var height = seriesModel.get(['breadcrumb', 'height']);
  111. var availableSize = layout.getAvailableSize(layoutParam.pos, layoutParam.box);
  112. var totalWidth = layoutParam.totalWidth;
  113. var renderList = layoutParam.renderList;
  114. var emphasisItemStyle = emphasisModel.getModel('itemStyle').getItemStyle();
  115. for (var i = renderList.length - 1; i >= 0; i--) {
  116. var item = renderList[i];
  117. var itemNode = item.node;
  118. var itemWidth = item.width;
  119. var text = item.text;
  120. // Hdie text and shorten width if necessary.
  121. if (totalWidth > availableSize.width) {
  122. totalWidth -= itemWidth - emptyItemWidth;
  123. itemWidth = emptyItemWidth;
  124. text = null;
  125. }
  126. var el = new graphic.Polygon({
  127. shape: {
  128. points: makeItemPoints(lastX, 0, itemWidth, height, i === renderList.length - 1, i === 0)
  129. },
  130. style: defaults(normalStyleModel.getItemStyle(), {
  131. lineJoin: 'bevel'
  132. }),
  133. textContent: new graphic.Text({
  134. style: createTextStyle(textStyleModel, {
  135. text: text
  136. })
  137. }),
  138. textConfig: {
  139. position: 'inside'
  140. },
  141. z2: Z2_EMPHASIS_LIFT * 1e4,
  142. onclick: curry(onSelect, itemNode)
  143. });
  144. el.disableLabelAnimation = true;
  145. el.getTextContent().ensureState('emphasis').style = createTextStyle(emphasisTextStyleModel, {
  146. text: text
  147. });
  148. el.ensureState('emphasis').style = emphasisItemStyle;
  149. toggleHoverEmphasis(el, emphasisModel.get('focus'), emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  150. this.group.add(el);
  151. packEventData(el, seriesModel, itemNode);
  152. lastX += itemWidth + ITEM_GAP;
  153. }
  154. };
  155. Breadcrumb.prototype.remove = function () {
  156. this.group.removeAll();
  157. };
  158. return Breadcrumb;
  159. }();
  160. function makeItemPoints(x, y, itemWidth, itemHeight, head, tail) {
  161. var points = [[head ? x : x - ARRAY_LENGTH, y], [x + itemWidth, y], [x + itemWidth, y + itemHeight], [head ? x : x - ARRAY_LENGTH, y + itemHeight]];
  162. !tail && points.splice(2, 0, [x + itemWidth + ARRAY_LENGTH, y + itemHeight / 2]);
  163. !head && points.push([x, y + itemHeight / 2]);
  164. return points;
  165. }
  166. // Package custom mouse event.
  167. function packEventData(el, seriesModel, itemNode) {
  168. getECData(el).eventData = {
  169. componentType: 'series',
  170. componentSubType: 'treemap',
  171. componentIndex: seriesModel.componentIndex,
  172. seriesIndex: seriesModel.seriesIndex,
  173. seriesName: seriesModel.name,
  174. seriesType: 'treemap',
  175. selfType: 'breadcrumb',
  176. nodeData: {
  177. dataIndex: itemNode && itemNode.dataIndex,
  178. name: itemNode && itemNode.name
  179. },
  180. treePathInfo: itemNode && wrapTreePathInfo(itemNode, seriesModel)
  181. };
  182. }
  183. export default Breadcrumb;