bellcurve.src.js
4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* @license @product.name@ JS v@product.version@ (@product.date@)
*
* (c) 2010-2017 Highsoft AS
* Author: Sebastian Domas
*
* License: www.highcharts.com/license
*/
'use strict';
import H from '../parts/Globals.js';
import '../parts/Utilities.js';
import derivedSeriesMixin from '../mixins/derived-series.js';
var seriesType = H.seriesType,
correctFloat = H.correctFloat,
isNumber = H.isNumber,
merge = H.merge,
reduce = H.reduce;
/** ****************************************************************************
*
* BELL CURVE
*
******************************************************************************/
function mean(data) {
var length = data.length,
sum = reduce(data, function (sum, value) {
return (sum += value);
}, 0);
return length > 0 && sum / length;
}
function standardDeviation(data, average) {
var len = data.length,
sum;
average = isNumber(average) ? average : mean(data);
sum = reduce(data, function (sum, value) {
var diff = value - average;
return (sum += diff * diff);
}, 0);
return len > 1 && Math.sqrt(sum / (len - 1));
}
function normalDensity(x, mean, standardDeviation) {
var translation = x - mean;
return Math.exp(
-(translation * translation) /
(2 * standardDeviation * standardDeviation)
) / (standardDeviation * Math.sqrt(2 * Math.PI));
}
/**
* Bell curve class
*
* @constructor seriesTypes.bellcurve
* @augments seriesTypes.areaspline
* @mixes DerivedSeriesMixin
**/
/**
* A bell curve is an areaspline series which represents the probability density
* function of the normal distribution. It calculates mean and standard
* deviation of the base series data and plots the curve according to the
* calculated parameters.
*
* @product highcharts
* @sample {highcharts} highcharts/demo/bellcurve/ Bell curve
* @since 6.0.0
* @extends plotOptions.areaspline
* @excluding boostThreshold,connectNulls,stacking,pointInterval,
* pointIntervalUnit
* @optionparent plotOptions.bellcurve
**/
seriesType('bellcurve', 'areaspline', {
/**
* This option allows to define the length of the bell curve. A unit of the
* length of the bell curve is standard deviation.
*
* @sample highcharts/plotoptions/bellcurve-intervals-pointsininterval
* Intervals and points in interval
*/
intervals: 3,
/**
* Defines how many points should be plotted within 1 interval. See
* `plotOptions.bellcurve.intervals`.
*
* @sample highcharts/plotoptions/bellcurve-intervals-pointsininterval
* Intervals and points in interval
*/
pointsInInterval: 3,
marker: {
enabled: false
}
}, merge(derivedSeriesMixin, {
setMean: function () {
this.mean = correctFloat(mean(this.baseSeries.yData));
},
setStandardDeviation: function () {
this.standardDeviation = correctFloat(
standardDeviation(this.baseSeries.yData, this.mean)
);
},
setDerivedData: function () {
if (this.baseSeries.yData.length > 1) {
this.setMean();
this.setStandardDeviation();
this.setData(
this.derivedData(this.mean, this.standardDeviation), false
);
}
},
derivedData: function (mean, standardDeviation) {
var intervals = this.options.intervals,
pointsInInterval = this.options.pointsInInterval,
x = mean - intervals * standardDeviation,
stop = intervals * pointsInInterval * 2 + 1,
increment = standardDeviation / pointsInInterval,
data = [],
i;
for (i = 0; i < stop; i++) {
data.push([x, normalDensity(x, mean, standardDeviation)]);
x += increment;
}
return data;
}
}));
/**
* A `bellcurve` series. If the [type](#series.bellcurve.type) option is not
* specified, it is inherited from [chart.type](#chart.type).
*
* For options that apply to multiple series, it is recommended to add
* them to the [plotOptions.series](#plotOptions.series) options structure.
* To apply to all series of this specific type, apply it to
* [plotOptions.bellcurve](#plotOptions.bellcurve).
*
* @type {Object}
* @since 6.0.0
* @extends series,plotOptions.bellcurve
* @excluding dataParser,dataURL,data
* @product highcharts
* @apioption series.bellcurve
*/
/**
* An integer identifying the index to use for the base series, or a string
* representing the id of the series.
*
* @type {Number|String}
* @default undefined
* @apioption series.bellcurve.baseSeries
*/
/**
* An array of data points for the series. For the `bellcurve` series type,
* points are calculated dynamically.
*
* @type {Array<Object|Array>}
* @since 6.0.0
* @extends series.areaspline.data
* @product highcharts
* @apioption series.bellcurve.data
*/