variable-pie.src.js 20 KB

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