centered-series.js 3.3 KB

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