CenteredSeries.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* *
  2. *
  3. * (c) 2010-2021 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import H from '../Core/Globals.js';
  12. import Series from '../Core/Series/Series.js';
  13. /**
  14. * @private
  15. * @interface Highcharts.RadianAngles
  16. */ /**
  17. * @name Highcharts.RadianAngles#end
  18. * @type {number}
  19. */ /**
  20. * @name Highcharts.RadianAngles#start
  21. * @type {number}
  22. */
  23. import U from '../Core/Utilities.js';
  24. var isNumber = U.isNumber, pick = U.pick, relativeLength = U.relativeLength;
  25. var deg2rad = H.deg2rad;
  26. /* eslint-disable valid-jsdoc */
  27. /**
  28. * @private
  29. * @mixin Highcharts.CenteredSeriesMixin
  30. */
  31. var centeredSeriesMixin = H.CenteredSeriesMixin = {
  32. /**
  33. * Get the center of the pie based on the size and center options relative
  34. * to the plot area. Borrowed by the polar and gauge series types.
  35. *
  36. * @private
  37. * @function Highcharts.CenteredSeriesMixin.getCenter
  38. *
  39. * @return {Array<number>}
  40. */
  41. getCenter: function () {
  42. var options = this.options, chart = this.chart, slicingRoom = 2 * (options.slicedOffset || 0), handleSlicingRoom, plotWidth = chart.plotWidth - 2 * slicingRoom, plotHeight = chart.plotHeight - 2 * slicingRoom, centerOption = options.center, smallestSize = Math.min(plotWidth, plotHeight), size = options.size, innerSize = options.innerSize || 0, positions, i, value;
  43. if (typeof size === 'string') {
  44. size = parseFloat(size);
  45. }
  46. if (typeof innerSize === 'string') {
  47. innerSize = parseFloat(innerSize);
  48. }
  49. positions = [
  50. pick(centerOption[0], '50%'),
  51. pick(centerOption[1], '50%'),
  52. // Prevent from negative values
  53. pick(size && size < 0 ? void 0 : options.size, '100%'),
  54. pick(innerSize && innerSize < 0 ? void 0 : options.innerSize || 0, '0%')
  55. ];
  56. // No need for inner size in angular (gauges) series but still required
  57. // for pie series
  58. if (chart.angular && !(this instanceof Series)) {
  59. positions[3] = 0;
  60. }
  61. for (i = 0; i < 4; ++i) {
  62. value = positions[i];
  63. handleSlicingRoom = i < 2 || (i === 2 && /%$/.test(value));
  64. // i == 0: centerX, relative to width
  65. // i == 1: centerY, relative to height
  66. // i == 2: size, relative to smallestSize
  67. // i == 3: innerSize, relative to size
  68. positions[i] = relativeLength(value, [plotWidth, plotHeight, smallestSize, positions[2]][i]) + (handleSlicingRoom ? slicingRoom : 0);
  69. }
  70. // innerSize cannot be larger than size (#3632)
  71. if (positions[3] > positions[2]) {
  72. positions[3] = positions[2];
  73. }
  74. return positions;
  75. },
  76. /**
  77. * getStartAndEndRadians - Calculates start and end angles in radians.
  78. * Used in series types such as pie and sunburst.
  79. *
  80. * @private
  81. * @function Highcharts.CenteredSeriesMixin.getStartAndEndRadians
  82. *
  83. * @param {number} [start]
  84. * Start angle in degrees.
  85. *
  86. * @param {number} [end]
  87. * Start angle in degrees.
  88. *
  89. * @return {Highcharts.RadianAngles}
  90. * Returns an object containing start and end angles as radians.
  91. */
  92. getStartAndEndRadians: function (start, end) {
  93. var startAngle = isNumber(start) ? start : 0, // must be a number
  94. endAngle = ((isNumber(end) && // must be a number
  95. end > startAngle && // must be larger than the start angle
  96. // difference must be less than 360 degrees
  97. (end - startAngle) < 360) ?
  98. end :
  99. startAngle + 360), correction = -90;
  100. return {
  101. start: deg2rad * (startAngle + correction),
  102. end: deg2rad * (endAngle + correction)
  103. };
  104. }
  105. };
  106. export default centeredSeriesMixin;