SunburstPiece.js 9.1 KB

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