BellcurveSeries.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. import BaseSeries from '../Core/Series/Series.js';
  13. import DerivedSeriesMixin from '../Mixins/DerivedSeries.js';
  14. import U from '../Core/Utilities.js';
  15. var correctFloat = U.correctFloat, isNumber = U.isNumber, merge = U.merge;
  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. BaseSeries.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. * @see [fillColor](#plotOptions.bellcurve.fillColor)
  80. * @see [fillOpacity](#plotOptions.bellcurve.fillOpacity)
  81. *
  82. * @apioption plotOptions.bellcurve.color
  83. */
  84. /**
  85. * @see [color](#plotOptions.bellcurve.color)
  86. * @see [fillOpacity](#plotOptions.bellcurve.fillOpacity)
  87. *
  88. * @apioption plotOptions.bellcurve.fillColor
  89. */
  90. /**
  91. * @see [color](#plotOptions.bellcurve.color)
  92. * @see [fillColor](#plotOptions.bellcurve.fillColor)
  93. *
  94. * @default {highcharts} 0.75
  95. * @default {highstock} 0.75
  96. * @apioption plotOptions.bellcurve.fillOpacity
  97. */
  98. /**
  99. * This option allows to define the length of the bell curve. A unit of
  100. * the length of the bell curve is standard deviation.
  101. *
  102. * @sample highcharts/plotoptions/bellcurve-intervals-pointsininterval
  103. * Intervals and points in interval
  104. */
  105. intervals: 3,
  106. /**
  107. * Defines how many points should be plotted within 1 interval. See
  108. * `plotOptions.bellcurve.intervals`.
  109. *
  110. * @sample highcharts/plotoptions/bellcurve-intervals-pointsininterval
  111. * Intervals and points in interval
  112. */
  113. pointsInInterval: 3,
  114. marker: {
  115. enabled: false
  116. }
  117. }, merge(DerivedSeriesMixin, {
  118. setMean: function () {
  119. this.mean = correctFloat(mean(this.baseSeries.yData));
  120. },
  121. setStandardDeviation: function () {
  122. this.standardDeviation = correctFloat(standardDeviation(this.baseSeries.yData, this.mean));
  123. },
  124. setDerivedData: function () {
  125. if (this.baseSeries.yData.length > 1) {
  126. this.setMean();
  127. this.setStandardDeviation();
  128. this.setData(this.derivedData(this.mean, this.standardDeviation), false);
  129. }
  130. return (void 0);
  131. },
  132. derivedData: function (mean, standardDeviation) {
  133. var intervals = this.options.intervals, pointsInInterval = this.options.pointsInInterval, x = mean - intervals * standardDeviation, stop = intervals * pointsInInterval * 2 + 1, increment = standardDeviation / pointsInInterval, data = [], i;
  134. for (i = 0; i < stop; i++) {
  135. data.push([x, normalDensity(x, mean, standardDeviation)]);
  136. x += increment;
  137. }
  138. return data;
  139. }
  140. }));
  141. /**
  142. * A `bellcurve` series. If the [type](#series.bellcurve.type) option is not
  143. * specified, it is inherited from [chart.type](#chart.type).
  144. *
  145. * For options that apply to multiple series, it is recommended to add
  146. * them to the [plotOptions.series](#plotOptions.series) options structure.
  147. * To apply to all series of this specific type, apply it to
  148. * [plotOptions.bellcurve](#plotOptions.bellcurve).
  149. *
  150. * @extends series,plotOptions.bellcurve
  151. * @since 6.0.0
  152. * @product highcharts
  153. * @excluding dataParser, dataURL, data, boostThreshold, boostBlending
  154. * @requires modules/bellcurve
  155. * @apioption series.bellcurve
  156. */
  157. /**
  158. * An integer identifying the index to use for the base series, or a string
  159. * representing the id of the series.
  160. *
  161. * @type {number|string}
  162. * @apioption series.bellcurve.baseSeries
  163. */
  164. /**
  165. * @see [fillColor](#series.bellcurve.fillColor)
  166. * @see [fillOpacity](#series.bellcurve.fillOpacity)
  167. *
  168. * @apioption series.bellcurve.color
  169. */
  170. /**
  171. * @see [color](#series.bellcurve.color)
  172. * @see [fillOpacity](#series.bellcurve.fillOpacity)
  173. *
  174. * @apioption series.bellcurve.fillColor
  175. */
  176. /**
  177. * @see [color](#series.bellcurve.color)
  178. * @see [fillColor](#series.bellcurve.fillColor)
  179. *
  180. * @default {highcharts} 0.75
  181. * @default {highstock} 0.75
  182. * @apioption series.bellcurve.fillOpacity
  183. */
  184. ''; // adds doclets above to transpiled file