forked from sergachev/flot-downsample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.flot.downsample.js
More file actions
157 lines (127 loc) · 5.36 KB
/
jquery.flot.downsample.js
File metadata and controls
157 lines (127 loc) · 5.36 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
/*
* The MIT License
Copyright (c) 2013 by Sveinn Steinarsson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
(function ($) {
"use strict";
var floor = Math.floor,
abs = Math.abs;
function splitIntoContinuousSeries(seriesData) {
var segments = [];
var currentSegment = [];
for (var i = 0; i < seriesData.length; i++) {
var point = seriesData[i];
if (point) {
currentSegment.push(point);
} else {
segments.push(currentSegment);
segments.push([null]);
currentSegment = [];
}
}
segments.push(currentSegment);
return segments;
}
function largestTriangleThreeBuckets(data, threshold) {
var data_length = data.length;
if (threshold >= data_length || threshold === 0) {
return data; // Nothing to do
}
var sampled = [],
sampled_index = 0;
// Bucket size. Leave room for start and end data points
var every = (data_length - 2) / (threshold - 2);
var a = 0, // Initially a is the first point in the triangle
max_area_point,
max_area,
area,
next_a;
sampled[ sampled_index++ ] = data[ a ]; // Always add the first point
for (var i = 0; i < threshold - 2; i++) {
// Calculate point average for next bucket (containing c)
var avg_x = 0,
avg_y = 0,
avg_range_start = floor( ( i + 1 ) * every ) + 1,
avg_range_end = floor( ( i + 2 ) * every ) + 1;
avg_range_end = avg_range_end < data_length ? avg_range_end : data_length;
var avg_range_length = avg_range_end - avg_range_start;
for ( ; avg_range_start<avg_range_end; avg_range_start++ ) {
avg_x += data[ avg_range_start ][ 0 ] * 1; // * 1 enforces Number (value may be Date)
avg_y += data[ avg_range_start ][ 1 ] * 1;
}
avg_x /= avg_range_length;
avg_y /= avg_range_length;
// Get the range for this bucket
var range_offs = floor( (i + 0) * every ) + 1,
range_to = Math.min(floor( (i + 1) * every ) + 1, data.length);
// Point a
var point_a_x = data[ a ][ 0 ] * 1, // enforce Number (value may be Date)
point_a_y = data[ a ][ 1 ] * 1;
max_area = area = -1;
for ( ; range_offs < range_to; range_offs++ ) {
// Calculate triangle area over three buckets
area = abs( ( point_a_x - avg_x ) * ( data[ range_offs ][ 1 ] - point_a_y ) -
( point_a_x - data[ range_offs ][ 0 ] ) * ( avg_y - point_a_y )
) * 0.5;
if ( area > max_area ) {
max_area = area;
max_area_point = data[ range_offs ];
next_a = range_offs; // Next a is this b
}
}
sampled[ sampled_index++ ] = max_area_point; // Pick this point from the bucket
a = next_a; // This a is the next a (chosen b)
}
sampled[ sampled_index++ ] = data[ data_length - 1 ]; // Always add last
return sampled;
}
function processRawData ( plot, series ) {
if (series.downsample.ratio <= 1) {
return;
}
var segments = splitIntoContinuousSeries(series.data);
var data = [];
for (var i = 0; i < segments.length; i++) {
var segment = segments[i];
if (segment && segment[0] !== null) {
// Use 3 as minimum segmentThreshold -- smaller threshold results in no downsampling
var segmentThreshold = Math.max(3, Math.ceil(segment.length /
series.downsample.ratio));
segment = largestTriangleThreeBuckets(segment, segmentThreshold);
}
data = data.concat(segment);
}
series.data = data;
}
var options = {
series: {
downsample: {
ratio: 0 // A value <= 1 disables downsampling
}
}
};
function init(plot) {
plot.hooks.processRawData.push(processRawData);
}
$.plot.plugins.push({
init: init,
options: options,
name: "downsample",
version: "1.0"
});
})(jQuery);