-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflorizon.js
141 lines (131 loc) · 4.13 KB
/
florizon.js
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
/* global jQuery */
(function($) {
"use strict";
function init(plot) {
function processOptions(plot) {//, options
plot.hooks.processRawData.unshift(processRawData);
}
function colorProgress(start, finish, steps) {
var progressions;
if (steps === 1) {
progressions = [finish];
} else {
var round = Math.round;
var performSteps = Math.max(steps + 1, 3);
progressions = [];
for(var i = 0; i < steps - 1; i++) {
var ratio = (i + performSteps - steps) / (performSteps - 1);
progressions.push([
round((finish[0] - start[0]) * ratio + start[0]),
round((finish[1] - start[1]) * ratio + start[1]),
round((finish[2] - start[2]) * ratio + start[2])
]);
}
progressions.push(finish);
}
return progressions.slice().map(function(components) {
return "rgb(" + components + ")";
});
}
function cachedColorProgress(start, finish) {
var cache = {};
return function(steps) {
if (!cache[steps]) {
cache[steps ] = colorProgress(start, finish, steps);
}
return cache[steps ];
};
}
var negativeColorProgress = cachedColorProgress([189, 215, 231], [8, 81, 156]);
var positiveColorProgress = cachedColorProgress([186, 228, 179], [0, 109, 44]);
function processRawData(plot, series, data) {//, datapoints
if (series.horizon && series.horizon.bands) {
var absolute = Math.abs;
var maximum = Math.max;
var floor = Math.floor;
var bands = 1 * series.horizon.bands;
if (isNaN(bands) || bands < 1) {
throw "Invalid value for bands: " + JSON.stringify(series.horizon.bands);
}
var max = data.reduce(function(a, b) {
return [null, maximum(absolute(a[1]), absolute(b[1]))];
})[1];
var positiveSeries = [];
var negativeSeries = [];
var negativeColors = negativeColorProgress(bands);
var positiveColors = positiveColorProgress(bands);
for(var i = 0; i < bands; i++) {
positiveSeries[i] = {
data: [],
horizon: false,
color: positiveColors[i],
yaxis: 2,
lines: {
show: true,
lineWidth: 0,
fill: true,
fillColor: positiveColors[i]
}
};
negativeSeries[i] = {
data: [],
horizon: false,
color: negativeColors[i],
yaxis: 2,
lines: {
show: true,
lineWidth: 0,
fill: true,
fillColor: negativeColors[i]
}
};
}
var sliceHeight = max / bands;
data.forEach(function(point) {
var time = point[0];
var value = point[1];
var nullPoint = [time, 0];
var maxPoint = [time, sliceHeight];
var filledBands = value > 0 ? positiveSeries : negativeSeries;
var emptyBands = value > 0 ? negativeSeries : positiveSeries;
value = absolute(value);
var prevBand = floor(value / sliceHeight) - 1;
var band;
for(band = 0; band <= prevBand; band++) {
filledBands[band].data.push(maxPoint);
emptyBands[band].data.push(nullPoint);
}
if (band < bands) {
filledBands[band].data.push([time, value - band * sliceHeight]);
emptyBands[band].data.push(nullPoint);
}
band++;
for (; band < bands; band++) {
filledBands[band].data.push(nullPoint);
emptyBands[band].data.push(nullPoint);
}
});
plot.setData([{
data: data,
yaxis: 1,
horizon: false,
lines: {
show: false
}
}].concat(positiveSeries, negativeSeries));
}
}
plot.hooks.processOptions.push(processOptions);
}
var options = {
series: {
horizon: false
}
};
$.plot.plugins.push({
init: init,
options: options,
name: "florizon",
version: "0.1"
});
})(jQuery);