bellcurve.src.js 4.7 KB

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