Breadcrumb.js 6.4 KB

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