bellcurve.src.js 4.8 KB

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