variable-pie.src.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /**
  2. * @license Highcharts JS v8.2.2 (2020-10-22)
  3. *
  4. * Variable Pie module for Highcharts
  5. *
  6. * (c) 2010-2019 Grzegorz Blachliński
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/variable-pie', ['highcharts'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Series/VariablePieSeries.js', [_modules['Core/Series/Series.js'], _modules['Core/Utilities.js']], function (BaseSeries, U) {
  32. /* *
  33. *
  34. * Variable Pie module for Highcharts
  35. *
  36. * (c) 2010-2017 Grzegorz Blachliński
  37. *
  38. * License: www.highcharts.com/license
  39. *
  40. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  41. *
  42. * */
  43. var seriesTypes = BaseSeries.seriesTypes;
  44. var arrayMax = U.arrayMax,
  45. arrayMin = U.arrayMin,
  46. clamp = U.clamp,
  47. fireEvent = U.fireEvent,
  48. pick = U.pick;
  49. var pieProto = seriesTypes.pie.prototype;
  50. /**
  51. * @typedef {"area"|"radius"} Highcharts.VariablePieSizeByValue
  52. */
  53. ''; // detach doclets above
  54. /**
  55. * The variablepie series type.
  56. *
  57. * @private
  58. * @class
  59. * @name Highcharts.seriesTypes.variablepie
  60. *
  61. * @augments Highcharts.Series
  62. */
  63. BaseSeries.seriesType('variablepie', 'pie',
  64. /**
  65. * A variable pie series is a two dimensional series type, where each point
  66. * renders an Y and Z value. Each point is drawn as a pie slice where the
  67. * size (arc) of the slice relates to the Y value and the radius of pie
  68. * slice relates to the Z value.
  69. *
  70. * @sample {highcharts} highcharts/demo/variable-radius-pie/
  71. * Variable-radius pie chart
  72. *
  73. * @extends plotOptions.pie
  74. * @excluding dragDrop
  75. * @since 6.0.0
  76. * @product highcharts
  77. * @requires modules/variable-pie.js
  78. * @optionparent plotOptions.variablepie
  79. */
  80. {
  81. /**
  82. * The minimum size of the points' radius related to chart's `plotArea`.
  83. * If a number is set, it applies in pixels.
  84. *
  85. * @sample {highcharts} highcharts/variable-radius-pie/min-max-point-size/
  86. * Example of minPointSize and maxPointSize
  87. * @sample {highcharts} highcharts/variable-radius-pie/min-point-size-100/
  88. * minPointSize set to 100
  89. *
  90. * @type {number|string}
  91. * @since 6.0.0
  92. */
  93. minPointSize: '10%',
  94. /**
  95. * The maximum size of the points' radius related to chart's `plotArea`.
  96. * If a number is set, it applies in pixels.
  97. *
  98. * @sample {highcharts} highcharts/variable-radius-pie/min-max-point-size/
  99. * Example of minPointSize and maxPointSize
  100. *
  101. * @type {number|string}
  102. * @since 6.0.0
  103. */
  104. maxPointSize: '100%',
  105. /**
  106. * The minimum possible z value for the point's radius calculation. If
  107. * the point's Z value is smaller than zMin, the slice will be drawn
  108. * according to the zMin value.
  109. *
  110. * @sample {highcharts} highcharts/variable-radius-pie/zmin-5/
  111. * zMin set to 5, smaller z values are treated as 5
  112. * @sample {highcharts} highcharts/variable-radius-pie/zmin-zmax/
  113. * Series limited by both zMin and zMax
  114. *
  115. * @type {number}
  116. * @since 6.0.0
  117. */
  118. zMin: void 0,
  119. /**
  120. * The maximum possible z value for the point's radius calculation. If
  121. * the point's Z value is bigger than zMax, the slice will be drawn
  122. * according to the zMax value
  123. *
  124. * @sample {highcharts} highcharts/variable-radius-pie/zmin-zmax/
  125. * Series limited by both zMin and zMax
  126. *
  127. * @type {number}
  128. * @since 6.0.0
  129. */
  130. zMax: void 0,
  131. /**
  132. * Whether the pie slice's value should be represented by the area or
  133. * the radius of the slice. Can be either `area` or `radius`. The
  134. * default, `area`, corresponds best to the human perception of the size
  135. * of each pie slice.
  136. *
  137. * @sample {highcharts} highcharts/variable-radius-pie/sizeby/
  138. * Difference between area and radius sizeBy
  139. *
  140. * @type {Highcharts.VariablePieSizeByValue}
  141. * @since 6.0.0
  142. */
  143. sizeBy: 'area',
  144. tooltip: {
  145. pointFormat: '<span style="color:{point.color}">\u25CF</span> {series.name}<br/>Value: {point.y}<br/>Size: {point.z}<br/>'
  146. }
  147. }, {
  148. pointArrayMap: ['y', 'z'],
  149. parallelArrays: ['x', 'y', 'z'],
  150. // It is needed to null series.center on chart redraw. Probably good
  151. // idea will be to add this option in directly in pie series.
  152. redraw: function () {
  153. this.center = null;
  154. pieProto.redraw.call(this, arguments);
  155. },
  156. // For arrayMin and arrayMax calculations array shouldn't have
  157. // null/undefined/string values. In this case it is needed to check if
  158. // points Z value is a Number.
  159. zValEval: function (zVal) {
  160. if (typeof zVal === 'number' && !isNaN(zVal)) {
  161. return true;
  162. }
  163. return null;
  164. },
  165. // Before standard translate method for pie chart it is needed to
  166. // calculate min/max radius of each pie slice based on its Z value.
  167. calculateExtremes: function () {
  168. var series = this,
  169. chart = series.chart,
  170. plotWidth = chart.plotWidth,
  171. plotHeight = chart.plotHeight,
  172. seriesOptions = series.options,
  173. slicingRoom = 2 * (seriesOptions.slicedOffset || 0),
  174. zMin,
  175. zMax,
  176. zData = series.zData,
  177. smallestSize = Math.min(plotWidth,
  178. plotHeight) - slicingRoom,
  179. // Min and max size of pie slice:
  180. extremes = {},
  181. // In pie charts size of a pie is changed to make space for
  182. // dataLabels, then series.center is changing.
  183. positions = series.center || series.getCenter();
  184. ['minPointSize', 'maxPointSize'].forEach(function (prop) {
  185. var length = seriesOptions[prop],
  186. isPercent = /%$/.test(length);
  187. length = parseInt(length, 10);
  188. extremes[prop] = isPercent ?
  189. smallestSize * length / 100 :
  190. length * 2; // Because it should be radius, not diameter.
  191. });
  192. series.minPxSize = positions[3] + extremes.minPointSize;
  193. series.maxPxSize = clamp(positions[2], positions[3] + extremes.minPointSize, extremes.maxPointSize);
  194. if (zData.length) {
  195. zMin = pick(seriesOptions.zMin, arrayMin(zData.filter(series.zValEval)));
  196. zMax = pick(seriesOptions.zMax, arrayMax(zData.filter(series.zValEval)));
  197. this.getRadii(zMin, zMax, series.minPxSize, series.maxPxSize);
  198. }
  199. },
  200. /* eslint-disable valid-jsdoc */
  201. /**
  202. * Finding radius of series points based on their Z value and min/max Z
  203. * value for all series.
  204. *
  205. * @private
  206. * @function Highcharts.Series#getRadii
  207. *
  208. * @param {number} zMin
  209. * Min threshold for Z value. If point's Z value is smaller that
  210. * zMin, point will have the smallest possible radius.
  211. *
  212. * @param {number} zMax
  213. * Max threshold for Z value. If point's Z value is bigger that
  214. * zMax, point will have the biggest possible radius.
  215. *
  216. * @param {number} minSize
  217. * Minimal pixel size possible for radius.
  218. *
  219. * @param {numbner} maxSize
  220. * Minimal pixel size possible for radius.
  221. *
  222. * @return {void}
  223. */
  224. getRadii: function (zMin, zMax, minSize, maxSize) {
  225. var i = 0,
  226. pos,
  227. zData = this.zData,
  228. len = zData.length,
  229. radii = [],
  230. options = this.options,
  231. sizeByArea = options.sizeBy !== 'radius',
  232. zRange = zMax - zMin,
  233. value,
  234. radius;
  235. // Calculate radius for all pie slice's based on their Z values
  236. for (i; i < len; i++) {
  237. // if zData[i] is null/undefined/string we need to take zMin for
  238. // smallest radius.
  239. value = this.zValEval(zData[i]) ? zData[i] : zMin;
  240. if (value <= zMin) {
  241. radius = minSize / 2;
  242. }
  243. else if (value >= zMax) {
  244. radius = maxSize / 2;
  245. }
  246. else {
  247. // Relative size, a number between 0 and 1
  248. pos = zRange > 0 ? (value - zMin) / zRange : 0.5;
  249. if (sizeByArea) {
  250. pos = Math.sqrt(pos);
  251. }
  252. radius = Math.ceil(minSize + pos * (maxSize - minSize)) / 2;
  253. }
  254. radii.push(radius);
  255. }
  256. this.radii = radii;
  257. },
  258. /* eslint-enable valid-jsdoc */
  259. // Extend translate by updating radius for each pie slice instead of
  260. // using one global radius.
  261. translate: function (positions) {
  262. this.generatePoints();
  263. var series = this,
  264. cumulative = 0,
  265. precision = 1000, // issue #172
  266. options = series.options,
  267. slicedOffset = options.slicedOffset,
  268. connectorOffset = slicedOffset + (options.borderWidth || 0),
  269. finalConnectorOffset,
  270. start,
  271. end,
  272. angle,
  273. startAngle = options.startAngle || 0,
  274. startAngleRad = Math.PI / 180 * (startAngle - 90),
  275. endAngleRad = Math.PI / 180 * (pick(options.endAngle,
  276. startAngle + 360) - 90),
  277. circ = endAngleRad - startAngleRad, // 2 * Math.PI,
  278. points = series.points,
  279. // the x component of the radius vector for a given point
  280. radiusX,
  281. radiusY,
  282. labelDistance = options.dataLabels.distance,
  283. ignoreHiddenPoint = options.ignoreHiddenPoint,
  284. i,
  285. len = points.length,
  286. point,
  287. pointRadii,
  288. pointRadiusX,
  289. pointRadiusY;
  290. series.startAngleRad = startAngleRad;
  291. series.endAngleRad = endAngleRad;
  292. // Use calculateExtremes to get series.radii array.
  293. series.calculateExtremes();
  294. // Get positions - either an integer or a percentage string must be
  295. // given. If positions are passed as a parameter, we're in a
  296. // recursive loop for adjusting space for data labels.
  297. if (!positions) {
  298. series.center = positions = series.getCenter();
  299. }
  300. // Calculate the geometry for each point
  301. for (i = 0; i < len; i++) {
  302. point = points[i];
  303. pointRadii = series.radii[i];
  304. // Used for distance calculation for specific point.
  305. point.labelDistance = pick(point.options.dataLabels &&
  306. point.options.dataLabels.distance, labelDistance);
  307. // Saved for later dataLabels distance calculation.
  308. series.maxLabelDistance = Math.max(series.maxLabelDistance || 0, point.labelDistance);
  309. // set start and end angle
  310. start = startAngleRad + (cumulative * circ);
  311. if (!ignoreHiddenPoint || point.visible) {
  312. cumulative += point.percentage / 100;
  313. }
  314. end = startAngleRad + (cumulative * circ);
  315. // set the shape
  316. point.shapeType = 'arc';
  317. point.shapeArgs = {
  318. x: positions[0],
  319. y: positions[1],
  320. r: pointRadii,
  321. innerR: positions[3] / 2,
  322. start: Math.round(start * precision) / precision,
  323. end: Math.round(end * precision) / precision
  324. };
  325. // The angle must stay within -90 and 270 (#2645)
  326. angle = (end + start) / 2;
  327. if (angle > 1.5 * Math.PI) {
  328. angle -= 2 * Math.PI;
  329. }
  330. else if (angle < -Math.PI / 2) {
  331. angle += 2 * Math.PI;
  332. }
  333. // Center for the sliced out slice
  334. point.slicedTranslation = {
  335. translateX: Math.round(Math.cos(angle) * slicedOffset),
  336. translateY: Math.round(Math.sin(angle) * slicedOffset)
  337. };
  338. // set the anchor point for tooltips
  339. radiusX = Math.cos(angle) * positions[2] / 2;
  340. radiusY = Math.sin(angle) * positions[2] / 2;
  341. pointRadiusX = Math.cos(angle) * pointRadii;
  342. pointRadiusY = Math.sin(angle) * pointRadii;
  343. point.tooltipPos = [
  344. positions[0] + radiusX * 0.7,
  345. positions[1] + radiusY * 0.7
  346. ];
  347. point.half = angle < -Math.PI / 2 || angle > Math.PI / 2 ?
  348. 1 :
  349. 0;
  350. point.angle = angle;
  351. // Set the anchor point for data labels. Use point.labelDistance
  352. // instead of labelDistance // #1174
  353. // finalConnectorOffset - not override connectorOffset value.
  354. finalConnectorOffset = Math.min(connectorOffset, point.labelDistance / 5); // #1678
  355. point.labelPosition = {
  356. natural: {
  357. // initial position of the data label - it's utilized
  358. // for finding the final position for the label
  359. x: positions[0] + pointRadiusX +
  360. Math.cos(angle) * point.labelDistance,
  361. y: positions[1] + pointRadiusY +
  362. Math.sin(angle) * point.labelDistance
  363. },
  364. 'final': {
  365. // used for generating connector path -
  366. // initialized later in drawDataLabels function
  367. // x: undefined,
  368. // y: undefined
  369. },
  370. // left - pie on the left side of the data label
  371. // right - pie on the right side of the data label
  372. alignment: point.half ? 'right' : 'left',
  373. connectorPosition: {
  374. breakAt: {
  375. x: positions[0] + pointRadiusX +
  376. Math.cos(angle) * finalConnectorOffset,
  377. y: positions[1] + pointRadiusY +
  378. Math.sin(angle) * finalConnectorOffset
  379. },
  380. touchingSliceAt: {
  381. x: positions[0] + pointRadiusX,
  382. y: positions[1] + pointRadiusY
  383. }
  384. }
  385. };
  386. }
  387. fireEvent(series, 'afterTranslate');
  388. }
  389. });
  390. /**
  391. * A `variablepie` series. If the [type](#series.variablepie.type) option is not
  392. * specified, it is inherited from [chart.type](#chart.type).
  393. *
  394. * @extends series,plotOptions.variablepie
  395. * @excluding dataParser, dataURL, stack, xAxis, yAxis, dataSorting,
  396. * boostThreshold, boostBlending
  397. * @product highcharts
  398. * @requires modules/variable-pie.js
  399. * @apioption series.variablepie
  400. */
  401. /**
  402. * An array of data points for the series. For the `variablepie` series type,
  403. * points can be given in the following ways:
  404. *
  405. * 1. An array of arrays with 2 values. In this case, the numerical values will
  406. * be interpreted as `y, z` options. Example:
  407. * ```js
  408. * data: [
  409. * [40, 75],
  410. * [50, 50],
  411. * [60, 40]
  412. * ]
  413. * ```
  414. *
  415. * 2. An array of objects with named values. The following snippet shows only a
  416. * few settings, see the complete options set below. If the total number of
  417. * data points exceeds the series'
  418. * [turboThreshold](#series.variablepie.turboThreshold), this option is not
  419. * available.
  420. * ```js
  421. * data: [{
  422. * y: 1,
  423. * z: 4,
  424. * name: "Point2",
  425. * color: "#00FF00"
  426. * }, {
  427. * y: 7,
  428. * z: 10,
  429. * name: "Point1",
  430. * color: "#FF00FF"
  431. * }]
  432. * ```
  433. *
  434. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  435. * Arrays of numeric x and y
  436. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  437. * Arrays of datetime x and y
  438. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  439. * Arrays of point.name and y
  440. * @sample {highcharts} highcharts/series/data-array-of-objects/
  441. * Config objects
  442. *
  443. * @type {Array<Array<(number|string),number>|*>}
  444. * @extends series.pie.data
  445. * @excluding marker, x
  446. * @product highcharts
  447. * @apioption series.variablepie.data
  448. */
  449. ''; // adds doclets above to transpiled file
  450. });
  451. _registerModule(_modules, 'masters/modules/variable-pie.src.js', [], function () {
  452. });
  453. }));