This repository has been archived by the owner on Jul 20, 2023. It is now read-only.
forked from whatbox/jquery.flot.byte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.flot.byte.js
147 lines (126 loc) · 5.42 KB
/
jquery.flot.byte.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
142
143
144
145
146
147
(function ($) {
'use strict';
var options = {};
//Round to nearby lower multiple of base
function floorInBase(n, base) {
return base * Math.floor(n / base);
}
function init(plot) {
plot.hooks.processDatapoints.push(function (plot) {
$.each(plot.getAxes(), function (axisName, axis) {
var opts = axis.options;
if (opts.mode === 'byte' || opts.mode === 'byteRate' || opts.mode === 'bitRate') {
if (typeof opts.base === 'number' && opts.base === 10) {
// Gigabytes, Terabytes
var EXTENSIONS = [
'',
'k',
'M',
'G',
'T',
'P',
'E',
'Z',
'Y',
];
var STEP_SIZE = 1000;
axis.tickGenerator = function (axis) {
var ticks = [],
start = floorInBase(axis.min, axis.tickSize),
i = 0,
v = Number.NaN,
prev;
do {
prev = v;
v = start + i * axis.tickSize;
ticks.push(v);
++i;
} while (v < axis.max && v !== prev);
return ticks;
};
} else {
// Gibibytes, Tebibytes
var EXTENSIONS = [
'',
'Ki',
'Mi',
'Gi',
'Ti',
'Pi',
'Ei',
'Zi',
'Yi',
];
var STEP_SIZE = 1024;
axis.tickGenerator = function (axis) {
var returnTicks = [],
tickSize = 2,
delta = axis.delta,
steps = 0,
tickMin = 0,
tickVal,
tickCount = 0;
//Enforce maximum tick Decimals
if (typeof opts.tickDecimals === 'number') {
axis.tickDecimals = opts.tickDecimals;
} else {
axis.tickDecimals = 2;
}
//Count the steps
while (Math.abs(delta) >= STEP_SIZE) {
steps++;
delta /= STEP_SIZE;
}
//Set the tick size relative to the remaining delta
while (tickSize <= STEP_SIZE) {
if (delta <= tickSize) {
break;
}
tickSize *= 2;
}
//Tell flot the tickSize we've calculated
if (typeof opts.minTickSize !== 'undefined' && tickSize < opts.minTickSize) {
axis.tickSize = opts.minTickSize;
} else {
axis.tickSize = tickSize * Math.pow(STEP_SIZE, steps);
}
//Calculate the new ticks
tickMin = floorInBase(axis.min, axis.tickSize);
do {
tickVal = tickMin + (tickCount++) * axis.tickSize;
returnTicks.push(tickVal);
} while (tickVal < axis.max);
return returnTicks;
};
}
axis.tickFormatter = function (size, axis) {
var steps = 0;
while (Math.abs(size) >= STEP_SIZE) {
steps++;
size /= STEP_SIZE;
}
var ext = ' ' + EXTENSIONS[steps];
if (opts.mode === 'byteRate') {
ext += 'B/s';
} else if (opts.mode === 'bitRate') {
ext += 'bps';
} else {
ext += 'B';
}
let out = size.toFixed(axis.tickDecimals);
if (out.endsWith('.00')) {
out = out.slice(0, -3);
}
return out+ ' ' + ext;
};
}
});
});
}
$.plot.plugins.push({
init: init,
options: options,
name: 'byte',
version: '0.1'
});
})(jQuery);