variable-pie.src.js 19 KB

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