BellcurveSeries.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* *
  2. *
  3. * (c) 2010-2020 Highsoft AS
  4. *
  5. * Author: Sebastian Domas
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import U from '../Core/Utilities.js';
  14. var correctFloat = U.correctFloat, isNumber = U.isNumber, merge = U.merge, seriesType = U.seriesType;
  15. import derivedSeriesMixin from '../Mixins/DerivedSeries.js';
  16. /* ************************************************************************** *
  17. * BELL CURVE *
  18. * ************************************************************************** */
  19. /* eslint-disable valid-jsdoc */
  20. /**
  21. * @private
  22. */
  23. function mean(data) {
  24. var length = data.length, sum = data.reduce(function (sum, value) {
  25. return (sum += value);
  26. }, 0);
  27. return length > 0 && sum / length;
  28. }
  29. /**
  30. * @private
  31. */
  32. function standardDeviation(data, average) {
  33. var len = data.length, sum;
  34. average = isNumber(average) ? average : mean(data);
  35. sum = data.reduce(function (sum, value) {
  36. var diff = value - average;
  37. return (sum += diff * diff);
  38. }, 0);
  39. return len > 1 && Math.sqrt(sum / (len - 1));
  40. }
  41. /**
  42. * @private
  43. */
  44. function normalDensity(x, mean, standardDeviation) {
  45. var translation = x - mean;
  46. return Math.exp(-(translation * translation) /
  47. (2 * standardDeviation * standardDeviation)) / (standardDeviation * Math.sqrt(2 * Math.PI));
  48. }
  49. /* eslint-enable valid-jsdoc */
  50. /**
  51. * Bell curve class
  52. *
  53. * @private
  54. * @class
  55. * @name Highcharts.seriesTypes.bellcurve
  56. *
  57. * @augments Highcharts.Series
  58. */
  59. seriesType('bellcurve', 'areaspline'
  60. /**
  61. * A bell curve is an areaspline series which represents the probability
  62. * density function of the normal distribution. It calculates mean and
  63. * standard deviation of the base series data and plots the curve according
  64. * to the calculated parameters.
  65. *
  66. * @sample {highcharts} highcharts/demo/bellcurve/
  67. * Bell curve
  68. *
  69. * @extends plotOptions.areaspline
  70. * @since 6.0.0
  71. * @product highcharts
  72. * @excluding boostThreshold, connectNulls, dragDrop, stacking,
  73. * pointInterval, pointIntervalUnit
  74. * @requires modules/bellcurve
  75. * @optionparent plotOptions.bellcurve
  76. */
  77. , {
  78. /**
  79. * This option allows to define the length of the bell curve. A unit of
  80. * the length of the bell curve is standard deviation.
  81. *
  82. * @sample highcharts/plotoptions/bellcurve-intervals-pointsininterval
  83. * Intervals and points in interval
  84. */
  85. intervals: 3,
  86. /**
  87. * Defines how many points should be plotted within 1 interval. See
  88. * `plotOptions.bellcurve.intervals`.
  89. *
  90. * @sample highcharts/plotoptions/bellcurve-intervals-pointsininterval
  91. * Intervals and points in interval
  92. */
  93. pointsInInterval: 3,
  94. marker: {
  95. enabled: false
  96. }
  97. }, merge(derivedSeriesMixin, {
  98. setMean: function () {
  99. this.mean = correctFloat(mean(this.baseSeries.yData));
  100. },
  101. setStandardDeviation: function () {
  102. this.standardDeviation = correctFloat(standardDeviation(this.baseSeries.yData, this.mean));
  103. },
  104. setDerivedData: function () {
  105. if (this.baseSeries.yData.length > 1) {
  106. this.setMean();
  107. this.setStandardDeviation();
  108. this.setData(this.derivedData(this.mean, this.standardDeviation), false);
  109. }
  110. return (void 0);
  111. },
  112. derivedData: function (mean, standardDeviation) {
  113. var intervals = this.options.intervals, pointsInInterval = this.options.pointsInInterval, x = mean - intervals * standardDeviation, stop = intervals * pointsInInterval * 2 + 1, increment = standardDeviation / pointsInInterval, data = [], i;
  114. for (i = 0; i < stop; i++) {
  115. data.push([x, normalDensity(x, mean, standardDeviation)]);
  116. x += increment;
  117. }
  118. return data;
  119. }
  120. }));
  121. /**
  122. * A `bellcurve` series. If the [type](#series.bellcurve.type) option is not
  123. * specified, it is inherited from [chart.type](#chart.type).
  124. *
  125. * For options that apply to multiple series, it is recommended to add
  126. * them to the [plotOptions.series](#plotOptions.series) options structure.
  127. * To apply to all series of this specific type, apply it to
  128. * [plotOptions.bellcurve](#plotOptions.bellcurve).
  129. *
  130. * @extends series,plotOptions.bellcurve
  131. * @since 6.0.0
  132. * @product highcharts
  133. * @excluding dataParser, dataURL, data, boostThreshold, boostBlending
  134. * @requires modules/bellcurve
  135. * @apioption series.bellcurve
  136. */
  137. /**
  138. * An integer identifying the index to use for the base series, or a string
  139. * representing the id of the series.
  140. *
  141. * @type {number|string}
  142. * @apioption series.bellcurve.baseSeries
  143. */
  144. ''; // adds doclets above to transpiled file