Pie3DSeries.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * 3D pie series
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import H from '../Core/Globals.js';
  14. import U from '../Core/Utilities.js';
  15. var pick = U.pick, wrap = U.wrap;
  16. var deg2rad = H.deg2rad, seriesTypes = H.seriesTypes, svg = H.svg;
  17. /**
  18. * The thickness of a 3D pie.
  19. *
  20. * @type {number}
  21. * @default 0
  22. * @since 4.0
  23. * @product highcharts
  24. * @requires highcharts-3d
  25. * @apioption plotOptions.pie.depth
  26. */
  27. /* eslint-disable no-invalid-this */
  28. wrap(seriesTypes.pie.prototype, 'translate', function (proceed) {
  29. proceed.apply(this, [].slice.call(arguments, 1));
  30. // Do not do this if the chart is not 3D
  31. if (!this.chart.is3d()) {
  32. return;
  33. }
  34. var series = this, seriesOptions = series.options, depth = seriesOptions.depth || 0, options3d = series.chart.options.chart.options3d, alpha = options3d.alpha, beta = options3d.beta, z = seriesOptions.stacking ?
  35. (seriesOptions.stack || 0) * depth :
  36. series._i * depth;
  37. z += depth / 2;
  38. if (seriesOptions.grouping !== false) {
  39. z = 0;
  40. }
  41. series.data.forEach(function (point) {
  42. var shapeArgs = point.shapeArgs, angle;
  43. point.shapeType = 'arc3d';
  44. shapeArgs.z = z;
  45. shapeArgs.depth = depth * 0.75;
  46. shapeArgs.alpha = alpha;
  47. shapeArgs.beta = beta;
  48. shapeArgs.center = series.center;
  49. angle = (shapeArgs.end + shapeArgs.start) / 2;
  50. point.slicedTranslation = {
  51. translateX: Math.round(Math.cos(angle) *
  52. seriesOptions.slicedOffset *
  53. Math.cos(alpha * deg2rad)),
  54. translateY: Math.round(Math.sin(angle) *
  55. seriesOptions.slicedOffset *
  56. Math.cos(alpha * deg2rad))
  57. };
  58. });
  59. });
  60. wrap(seriesTypes.pie.prototype.pointClass.prototype, 'haloPath', function (proceed) {
  61. var args = arguments;
  62. return this.series.chart.is3d() ? [] : proceed.call(this, args[1]);
  63. });
  64. wrap(seriesTypes.pie.prototype, 'pointAttribs', function (proceed, point, state) {
  65. var attr = proceed.call(this, point, state), options = this.options;
  66. if (this.chart.is3d() && !this.chart.styledMode) {
  67. attr.stroke = options.edgeColor || point.color || this.color;
  68. attr['stroke-width'] = pick(options.edgeWidth, 1);
  69. }
  70. return attr;
  71. });
  72. wrap(seriesTypes.pie.prototype, 'drawDataLabels', function (proceed) {
  73. if (this.chart.is3d()) {
  74. var series = this, chart = series.chart, options3d = chart.options.chart.options3d;
  75. series.data.forEach(function (point) {
  76. var shapeArgs = point.shapeArgs, r = shapeArgs.r,
  77. // #3240 issue with datalabels for 0 and null values
  78. a1 = (shapeArgs.alpha || options3d.alpha) * deg2rad, b1 = (shapeArgs.beta || options3d.beta) * deg2rad, a2 = (shapeArgs.start + shapeArgs.end) / 2, labelPosition = point.labelPosition, connectorPosition = labelPosition.connectorPosition, yOffset = (-r * (1 - Math.cos(a1)) * Math.sin(a2)), xOffset = r * (Math.cos(b1) - 1) * Math.cos(a2);
  79. // Apply perspective on label positions
  80. [
  81. labelPosition.natural,
  82. connectorPosition.breakAt,
  83. connectorPosition.touchingSliceAt
  84. ].forEach(function (coordinates) {
  85. coordinates.x += xOffset;
  86. coordinates.y += yOffset;
  87. });
  88. });
  89. }
  90. proceed.apply(this, [].slice.call(arguments, 1));
  91. });
  92. wrap(seriesTypes.pie.prototype, 'addPoint', function (proceed) {
  93. proceed.apply(this, [].slice.call(arguments, 1));
  94. if (this.chart.is3d()) {
  95. // destroy (and rebuild) everything!!!
  96. this.update(this.userOptions, true); // #3845 pass the old options
  97. }
  98. });
  99. wrap(seriesTypes.pie.prototype, 'animate', function (proceed) {
  100. if (!this.chart.is3d()) {
  101. proceed.apply(this, [].slice.call(arguments, 1));
  102. }
  103. else {
  104. var args = arguments, init = args[1], animation = this.options.animation, attribs, center = this.center, group = this.group, markerGroup = this.markerGroup;
  105. if (svg) { // VML is too slow anyway
  106. if (animation === true) {
  107. animation = {};
  108. }
  109. // Initialize the animation
  110. if (init) {
  111. // Scale down the group and place it in the center
  112. group.oldtranslateX = pick(group.oldtranslateX, group.translateX);
  113. group.oldtranslateY = pick(group.oldtranslateY, group.translateY);
  114. attribs = {
  115. translateX: center[0],
  116. translateY: center[1],
  117. scaleX: 0.001,
  118. scaleY: 0.001
  119. };
  120. group.attr(attribs);
  121. if (markerGroup) {
  122. markerGroup.attrSetters = group.attrSetters;
  123. markerGroup.attr(attribs);
  124. }
  125. // Run the animation
  126. }
  127. else {
  128. attribs = {
  129. translateX: group.oldtranslateX,
  130. translateY: group.oldtranslateY,
  131. scaleX: 1,
  132. scaleY: 1
  133. };
  134. group.animate(attribs, animation);
  135. if (markerGroup) {
  136. markerGroup.animate(attribs, animation);
  137. }
  138. }
  139. }
  140. }
  141. });