SunburstPiece.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 { __extends } from "tslib";
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. import * as graphic from '../../util/graphic.js';
  43. import { toggleHoverEmphasis, SPECIAL_STATES, DISPLAY_STATES } from '../../util/states.js';
  44. import { createTextStyle } from '../../label/labelStyle.js';
  45. import { getECData } from '../../util/innerStore.js';
  46. import { getSectorCornerRadius } from '../helper/pieHelper.js';
  47. import { createOrUpdatePatternFromDecal } from '../../util/decal.js';
  48. import { saveOldStyle } from '../../animation/basicTrasition.js';
  49. var DEFAULT_SECTOR_Z = 2;
  50. var DEFAULT_TEXT_Z = 4;
  51. /**
  52. * Sunburstce of Sunburst including Sector, Label, LabelLine
  53. */
  54. var SunburstPiece =
  55. /** @class */
  56. function (_super) {
  57. __extends(SunburstPiece, _super);
  58. function SunburstPiece(node, seriesModel, ecModel, api) {
  59. var _this = _super.call(this) || this;
  60. _this.z2 = DEFAULT_SECTOR_Z;
  61. _this.textConfig = {
  62. inside: true
  63. };
  64. getECData(_this).seriesIndex = seriesModel.seriesIndex;
  65. var text = new graphic.Text({
  66. z2: DEFAULT_TEXT_Z,
  67. silent: node.getModel().get(['label', 'silent'])
  68. });
  69. _this.setTextContent(text);
  70. _this.updateData(true, node, seriesModel, ecModel, api);
  71. return _this;
  72. }
  73. SunburstPiece.prototype.updateData = function (firstCreate, node, // state: 'emphasis' | 'normal' | 'highlight' | 'downplay',
  74. seriesModel, ecModel, api) {
  75. this.node = node;
  76. node.piece = this;
  77. seriesModel = seriesModel || this._seriesModel;
  78. ecModel = ecModel || this._ecModel;
  79. var sector = this;
  80. getECData(sector).dataIndex = node.dataIndex;
  81. var itemModel = node.getModel();
  82. var emphasisModel = itemModel.getModel('emphasis');
  83. var layout = node.getLayout();
  84. var sectorShape = zrUtil.extend({}, layout);
  85. sectorShape.label = null;
  86. var normalStyle = node.getVisual('style');
  87. normalStyle.lineJoin = 'bevel';
  88. var decal = node.getVisual('decal');
  89. if (decal) {
  90. normalStyle.decal = createOrUpdatePatternFromDecal(decal, api);
  91. }
  92. var cornerRadius = getSectorCornerRadius(itemModel.getModel('itemStyle'), sectorShape, true);
  93. zrUtil.extend(sectorShape, cornerRadius);
  94. zrUtil.each(SPECIAL_STATES, function (stateName) {
  95. var state = sector.ensureState(stateName);
  96. var itemStyleModel = itemModel.getModel([stateName, 'itemStyle']);
  97. state.style = itemStyleModel.getItemStyle(); // border radius
  98. var cornerRadius = getSectorCornerRadius(itemStyleModel, sectorShape);
  99. if (cornerRadius) {
  100. state.shape = cornerRadius;
  101. }
  102. });
  103. if (firstCreate) {
  104. sector.setShape(sectorShape);
  105. sector.shape.r = layout.r0;
  106. graphic.updateProps(sector, {
  107. shape: {
  108. r: layout.r
  109. }
  110. }, seriesModel, node.dataIndex);
  111. } else {
  112. // Disable animation for gradient since no interpolation method
  113. // is supported for gradient
  114. graphic.updateProps(sector, {
  115. shape: sectorShape
  116. }, seriesModel);
  117. saveOldStyle(sector);
  118. }
  119. sector.useStyle(normalStyle);
  120. this._updateLabel(seriesModel);
  121. var cursorStyle = itemModel.getShallow('cursor');
  122. cursorStyle && sector.attr('cursor', cursorStyle);
  123. this._seriesModel = seriesModel || this._seriesModel;
  124. this._ecModel = ecModel || this._ecModel;
  125. var focus = emphasisModel.get('focus');
  126. var focusOrIndices = focus === 'ancestor' ? node.getAncestorsIndices() : focus === 'descendant' ? node.getDescendantIndices() : focus;
  127. toggleHoverEmphasis(this, focusOrIndices, emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  128. };
  129. SunburstPiece.prototype._updateLabel = function (seriesModel) {
  130. var _this = this;
  131. var itemModel = this.node.getModel();
  132. var normalLabelModel = itemModel.getModel('label');
  133. var layout = this.node.getLayout();
  134. var angle = layout.endAngle - layout.startAngle;
  135. var midAngle = (layout.startAngle + layout.endAngle) / 2;
  136. var dx = Math.cos(midAngle);
  137. var dy = Math.sin(midAngle);
  138. var sector = this;
  139. var label = sector.getTextContent();
  140. var dataIndex = this.node.dataIndex;
  141. var labelMinAngle = normalLabelModel.get('minAngle') / 180 * Math.PI;
  142. var isNormalShown = normalLabelModel.get('show') && !(labelMinAngle != null && Math.abs(angle) < labelMinAngle);
  143. label.ignore = !isNormalShown; // TODO use setLabelStyle
  144. zrUtil.each(DISPLAY_STATES, function (stateName) {
  145. var labelStateModel = stateName === 'normal' ? itemModel.getModel('label') : itemModel.getModel([stateName, 'label']);
  146. var isNormal = stateName === 'normal';
  147. var state = isNormal ? label : label.ensureState(stateName);
  148. var text = seriesModel.getFormattedLabel(dataIndex, stateName);
  149. if (isNormal) {
  150. text = text || _this.node.name;
  151. }
  152. state.style = createTextStyle(labelStateModel, {}, null, stateName !== 'normal', true);
  153. if (text) {
  154. state.style.text = text;
  155. } // Not displaying text when angle is too small
  156. var isShown = labelStateModel.get('show');
  157. if (isShown != null && !isNormal) {
  158. state.ignore = !isShown;
  159. }
  160. var labelPosition = getLabelAttr(labelStateModel, 'position');
  161. var sectorState = isNormal ? sector : sector.states[stateName];
  162. var labelColor = sectorState.style.fill;
  163. sectorState.textConfig = {
  164. outsideFill: labelStateModel.get('color') === 'inherit' ? labelColor : null,
  165. inside: labelPosition !== 'outside'
  166. };
  167. var r;
  168. var labelPadding = getLabelAttr(labelStateModel, 'distance') || 0;
  169. var textAlign = getLabelAttr(labelStateModel, 'align');
  170. if (labelPosition === 'outside') {
  171. r = layout.r + labelPadding;
  172. textAlign = midAngle > Math.PI / 2 ? 'right' : 'left';
  173. } else {
  174. if (!textAlign || textAlign === 'center') {
  175. // Put label in the center if it's a circle
  176. if (angle === 2 * Math.PI && layout.r0 === 0) {
  177. r = 0;
  178. } else {
  179. r = (layout.r + layout.r0) / 2;
  180. }
  181. textAlign = 'center';
  182. } else if (textAlign === 'left') {
  183. r = layout.r0 + labelPadding;
  184. if (midAngle > Math.PI / 2) {
  185. textAlign = 'right';
  186. }
  187. } else if (textAlign === 'right') {
  188. r = layout.r - labelPadding;
  189. if (midAngle > Math.PI / 2) {
  190. textAlign = 'left';
  191. }
  192. }
  193. }
  194. state.style.align = textAlign;
  195. state.style.verticalAlign = getLabelAttr(labelStateModel, 'verticalAlign') || 'middle';
  196. state.x = r * dx + layout.cx;
  197. state.y = r * dy + layout.cy;
  198. var rotateType = getLabelAttr(labelStateModel, 'rotate');
  199. var rotate = 0;
  200. if (rotateType === 'radial') {
  201. rotate = -midAngle;
  202. if (rotate < -Math.PI / 2) {
  203. rotate += Math.PI;
  204. }
  205. } else if (rotateType === 'tangential') {
  206. rotate = Math.PI / 2 - midAngle;
  207. if (rotate > Math.PI / 2) {
  208. rotate -= Math.PI;
  209. } else if (rotate < -Math.PI / 2) {
  210. rotate += Math.PI;
  211. }
  212. } else if (zrUtil.isNumber(rotateType)) {
  213. rotate = rotateType * Math.PI / 180;
  214. }
  215. state.rotation = rotate;
  216. });
  217. function getLabelAttr(model, name) {
  218. var stateAttr = model.get(name);
  219. if (stateAttr == null) {
  220. return normalLabelModel.get(name);
  221. }
  222. return stateAttr;
  223. }
  224. label.dirtyStyle();
  225. };
  226. return SunburstPiece;
  227. }(graphic.Sector);
  228. export default SunburstPiece;