VariablePieSeries.js 15 KB

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