-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathDataProcessor.js
More file actions
91 lines (75 loc) · 2.59 KB
/
DataProcessor.js
File metadata and controls
91 lines (75 loc) · 2.59 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
export default class DataProcessor {
static dataToPoints(data, limit, width = 1, height = 1, margin = 0, max = this.max(data), min = this.min(data)) {
const len = data.length;
if (limit && limit < len) {
data = data.slice(len - limit);
}
const vfactor = (height - margin * 2) / ((max - min) || 2);
const hfactor = (width - margin * 2) / ((limit || len) - (len > 1 ? 1 : 0));
return data.map((d, i) => {
return {
x: i * hfactor + margin,
y: this.isGapValue(d) ? d : ((max === min ? 1 : (max - d)) * vfactor + margin)
}
});
}
static nonGapValues(data) {
return data.filter(d => !this.isGapValue(d));
}
static max(data) {
return Math.max.apply(Math, this.nonGapValues(data));
}
static min(data) {
return Math.min.apply(Math, this.nonGapValues(data));
}
static mean(data) {
data = this.nonGapValues(data);
return (this.max(data) - this.min(data)) / 2;
}
static avg(data) {
data = this.nonGapValues(data);
return data.reduce((a, b) => a + b) / data.length;
}
static median(data) {
data = this.nonGapValues(data);
return data.sort((a,b) => a - b)[Math.floor(data.length / 2)];
}
static variance(data) {
data = this.nonGapValues(data);
const mean = this.mean(data);
const sq = data.map(n => Math.pow(n - mean, 2));
return this.mean(sq);
}
static stdev(data) {
data = this.nonGapValues(data);
const mean = this.mean(data);
const sqDiff = data.map(n => Math.pow(n - mean, 2));
const avgSqDiff = this.avg(sqDiff);
return Math.sqrt(avgSqDiff);
}
static calculateFromData(data, calculationType) {
return this[calculationType].call(this, data);
}
static pointsToSegments(points) {
let segment, segments = [];
const newSegment = (isGap) => ({points: [], isGap});
for (let point of points) {
if (!segment)
segment = newSegment(false);
const isGapHere = this.isGapValue(point.y);
if (segment.isGap != isGapHere) {
if (segment.points.length)
segments.push(segment);
segment = newSegment(isGapHere);
}
segment.points.push(point);
}
if (segment && segment.points.length)
segments.push(segment);
return segments;
}
static isGapValue(y)
{
return !Number.isFinite(y);
}
}