SunburstPiece.js 9.3 KB

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