pareto.src.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**
  2. * @license Highcharts JS v6.1.0 (2018-04-13)
  3. *
  4. * Pareto series type for Highcharts
  5. *
  6. * (c) 2010-2017 Sebastian Bochan
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. module.exports = factory;
  14. } else {
  15. factory(Highcharts);
  16. }
  17. }(function (Highcharts) {
  18. var derivedSeriesMixin = (function (H) {
  19. var each = H.each,
  20. Series = H.Series,
  21. addEvent = H.addEvent,
  22. noop = H.noop;
  23. /* ***************************************************************************
  24. *
  25. * DERIVED SERIES MIXIN
  26. *
  27. **************************************************************************** */
  28. /**
  29. * Provides methods for auto setting/updating series data based on the based
  30. * series data.
  31. *
  32. * @mixin
  33. **/
  34. var derivedSeriesMixin = {
  35. /**
  36. * Initialise series
  37. *
  38. * returns {undefined}
  39. **/
  40. init: function () {
  41. Series.prototype.init.apply(this, arguments);
  42. this.initialised = false;
  43. this.baseSeries = null;
  44. this.eventRemovers = [];
  45. this.addEvents();
  46. },
  47. /**
  48. * Method to be implemented - inside the method the series has already access
  49. * to the base series via m `this.baseSeries` and the bases data is
  50. * initialised. It should return data in the format accepted by
  51. * `Series.setData()` method
  52. *
  53. * @returns {Array} - an array of data
  54. **/
  55. setDerivedData: noop,
  56. /**
  57. * Sets base series for the series
  58. *
  59. * returns {undefined}
  60. **/
  61. setBaseSeries: function () {
  62. var chart = this.chart,
  63. baseSeriesOptions = this.options.baseSeries,
  64. baseSeries =
  65. baseSeriesOptions &&
  66. (chart.series[baseSeriesOptions] || chart.get(baseSeriesOptions));
  67. this.baseSeries = baseSeries || null;
  68. },
  69. /**
  70. * Adds events for the series
  71. *
  72. * @returns {undefined}
  73. **/
  74. addEvents: function () {
  75. var derivedSeries = this,
  76. chartSeriesLinked;
  77. chartSeriesLinked = addEvent(
  78. this.chart,
  79. 'afterLinkSeries',
  80. function () {
  81. derivedSeries.setBaseSeries();
  82. if (derivedSeries.baseSeries && !derivedSeries.initialised) {
  83. derivedSeries.setDerivedData();
  84. derivedSeries.addBaseSeriesEvents();
  85. derivedSeries.initialised = true;
  86. }
  87. }
  88. );
  89. this.eventRemovers.push(
  90. chartSeriesLinked
  91. );
  92. },
  93. /**
  94. * Adds events to the base series - it required for recalculating the data in
  95. * the series if the base series is updated / removed / etc.
  96. *
  97. * @returns {undefined}
  98. **/
  99. addBaseSeriesEvents: function () {
  100. var derivedSeries = this,
  101. updatedDataRemover,
  102. destroyRemover;
  103. updatedDataRemover = addEvent(
  104. derivedSeries.baseSeries,
  105. 'updatedData',
  106. function () {
  107. derivedSeries.setDerivedData();
  108. }
  109. );
  110. destroyRemover = addEvent(
  111. derivedSeries.baseSeries,
  112. 'destroy',
  113. function () {
  114. derivedSeries.baseSeries = null;
  115. derivedSeries.initialised = false;
  116. }
  117. );
  118. derivedSeries.eventRemovers.push(
  119. updatedDataRemover,
  120. destroyRemover
  121. );
  122. },
  123. /**
  124. * Destroys the series
  125. *
  126. * @returns {undefined}
  127. **/
  128. destroy: function () {
  129. each(this.eventRemovers, function (remover) {
  130. remover();
  131. });
  132. Series.prototype.destroy.apply(this, arguments);
  133. }
  134. };
  135. return derivedSeriesMixin;
  136. }(Highcharts));
  137. (function (H, derivedSeriesMixin) {
  138. /**
  139. * (c) 2010-2017 Sebastian Bochan
  140. *
  141. * License: www.highcharts.com/license
  142. */
  143. var each = H.each,
  144. correctFloat = H.correctFloat,
  145. seriesType = H.seriesType,
  146. merge = H.merge;
  147. /**
  148. * The pareto series type.
  149. *
  150. * @constructor seriesTypes.pareto
  151. * @augments seriesTypes.line
  152. */
  153. /**
  154. * A pareto diagram is a type of chart that contains both bars and a line graph,
  155. * where individual values are represented in descending order by bars,
  156. * and the cumulative total is represented by the line.
  157. *
  158. * @extends {plotOptions.line}
  159. * @product highcharts
  160. * @sample {highcharts} highcharts/demo/pareto/
  161. * Pareto diagram
  162. * @since 6.0.0
  163. * @excluding allAreas,boostThreshold,borderColor,borderRadius,
  164. * borderWidth,crisp,colorAxis,depth,data,edgeColor,edgeWidth,
  165. * findNearestPointBy,gapSize,gapUnit,grouping,groupPadding,
  166. * groupZPadding,maxPointWidth,keys,negativeColor,pointInterval,
  167. * pointIntervalUnit,pointPadding,pointPlacement,pointRange,pointStart,
  168. * pointWidth,shadow,step,softThreshold,
  169. * stacking,threshold,zoneAxis,zones
  170. * @optionparent plotOptions.pareto
  171. */
  172. seriesType('pareto', 'line', {
  173. /**
  174. * Higher zIndex than column series to draw line above shapes.
  175. */
  176. zIndex: 3
  177. }, merge(derivedSeriesMixin, {
  178. /**
  179. * calculate sum and return percent points
  180. *
  181. * @param {Object} series
  182. * @return {Array} Returns array of points [x,y]
  183. */
  184. setDerivedData: function () {
  185. if (this.baseSeries.yData.length > 1) {
  186. var xValues = this.baseSeries.xData,
  187. yValues = this.baseSeries.yData,
  188. sum = this.sumPointsPercents(yValues, xValues, null, true);
  189. this.setData(
  190. this.sumPointsPercents(yValues, xValues, sum, false),
  191. false
  192. );
  193. }
  194. },
  195. /**
  196. * calculate y sum and each percent point
  197. *
  198. * @param {Array} yValues y values
  199. * @param {Array} xValues x values
  200. * @param {Number} sum of all y values
  201. * @param {Boolean} isSum declares if calculate sum of all points
  202. * @return {Array} Returns sum of points or array of points [x,y]
  203. */
  204. sumPointsPercents: function (yValues, xValues, sum, isSum) {
  205. var sumY = 0,
  206. sumPercent = 0,
  207. percentPoints = [],
  208. percentPoint;
  209. each(yValues, function (point, i) {
  210. if (point !== null) {
  211. if (isSum) {
  212. sumY += point;
  213. } else {
  214. percentPoint = (point / sum) * 100;
  215. percentPoints.push(
  216. [xValues[i], correctFloat(sumPercent + percentPoint)]
  217. );
  218. sumPercent += percentPoint;
  219. }
  220. }
  221. });
  222. return isSum ? sumY : percentPoints;
  223. }
  224. }));
  225. /**
  226. * A `pareto` series. If the [type](#series.pareto.type) option is not
  227. * specified, it is inherited from [chart.type](#chart.type).
  228. *
  229. * @type {Object}
  230. * @since 6.0.0
  231. * @extends series,plotOptions.pareto
  232. * @excluding data,dataParser,dataURL
  233. * @product highcharts
  234. * @apioption series.pareto
  235. */
  236. /**
  237. * An integer identifying the index to use for the base series, or a string
  238. * representing the id of the series.
  239. *
  240. * @type {Number|String}
  241. * @default undefined
  242. * @apioption series.pareto.baseSeries
  243. */
  244. /**
  245. * An array of data points for the series. For the `pareto` series type,
  246. * points are calculated dynamically.
  247. *
  248. * @type {Array<Object|Array>}
  249. * @since 6.0.0
  250. * @extends series.column.data
  251. * @product highcharts
  252. * @apioption series.pareto.data
  253. */
  254. }(Highcharts, derivedSeriesMixin));
  255. }));