Skip to content

Commit cf58396

Browse files
committed
Better axis ticks for base 10
1 parent 96b3947 commit cf58396

File tree

1 file changed

+100
-79
lines changed

1 file changed

+100
-79
lines changed

jquery.flot.byte.js

+100-79
Original file line numberDiff line numberDiff line change
@@ -13,86 +13,101 @@
1313
$.each(plot.getAxes(), function (axisName, axis) {
1414
var opts = axis.options;
1515

16-
if (typeof opts.base === 'number' && opts.base === 10) {
17-
// Gigabytes, Terabytes
18-
var EXTENSIONS = [
19-
'',
20-
'k',
21-
'M',
22-
'G',
23-
'T',
24-
'P',
25-
'E',
26-
'Z',
27-
'Y',
28-
];
29-
var STEP_SIZE = 1000;
30-
31-
} else {
32-
// Gibibytes, Tebibytes
33-
var EXTENSIONS = [
34-
'',
35-
'Ki',
36-
'Mi',
37-
'Gi',
38-
'Ti',
39-
'Pi',
40-
'Ei',
41-
'Zi',
42-
'Yi',
43-
];
44-
var STEP_SIZE = 1024;
45-
}
46-
47-
16+
4817
if (opts.mode === 'byte' || opts.mode === 'byteRate' || opts.mode === 'bitRate') {
49-
axis.tickGenerator = function (axis) {
50-
var returnTicks = [],
51-
tickSize = 2,
52-
delta = axis.delta,
53-
steps = 0,
54-
tickMin = 0,
55-
tickVal,
56-
tickCount = 0;
57-
58-
//Enforce maximum tick Decimals
59-
if (typeof opts.tickDecimals === 'number') {
60-
axis.tickDecimals = opts.tickDecimals;
61-
} else {
62-
axis.tickDecimals = 2;
63-
}
64-
65-
//Count the steps
66-
while (Math.abs(delta) >= STEP_SIZE) {
67-
steps++;
68-
delta /= STEP_SIZE;
69-
}
70-
71-
//Set the tick size relative to the remaining delta
72-
while (tickSize <= STEP_SIZE) {
73-
if (delta <= tickSize) {
74-
break;
18+
if (typeof opts.base === 'number' && opts.base === 10) {
19+
// Gigabytes, Terabytes
20+
var EXTENSIONS = [
21+
'',
22+
'k',
23+
'M',
24+
'G',
25+
'T',
26+
'P',
27+
'E',
28+
'Z',
29+
'Y',
30+
];
31+
var STEP_SIZE = 1000;
32+
33+
axis.tickGenerator = function (axis) {
34+
var ticks = [],
35+
start = floorInBase(axis.min, axis.tickSize),
36+
i = 0,
37+
v = Number.NaN,
38+
prev;
39+
40+
do {
41+
prev = v;
42+
v = start + i * axis.tickSize;
43+
ticks.push(v);
44+
++i;
45+
} while (v < axis.max && v !== prev);
46+
return ticks;
47+
};
48+
} else {
49+
// Gibibytes, Tebibytes
50+
var EXTENSIONS = [
51+
'',
52+
'Ki',
53+
'Mi',
54+
'Gi',
55+
'Ti',
56+
'Pi',
57+
'Ei',
58+
'Zi',
59+
'Yi',
60+
];
61+
var STEP_SIZE = 1024;
62+
63+
axis.tickGenerator = function (axis) {
64+
var returnTicks = [],
65+
tickSize = 2,
66+
delta = axis.delta,
67+
steps = 0,
68+
tickMin = 0,
69+
tickVal,
70+
tickCount = 0;
71+
72+
//Enforce maximum tick Decimals
73+
if (typeof opts.tickDecimals === 'number') {
74+
axis.tickDecimals = opts.tickDecimals;
75+
} else {
76+
axis.tickDecimals = 2;
7577
}
76-
tickSize *= 2;
77-
}
78-
79-
//Tell flot the tickSize we've calculated
80-
if (typeof opts.minTickSize !== 'undefined' && tickSize < opts.minTickSize) {
81-
axis.tickSize = opts.minTickSize;
82-
} else {
83-
axis.tickSize = tickSize * Math.pow(STEP_SIZE, steps);
84-
}
85-
86-
//Calculate the new ticks
87-
tickMin = floorInBase(axis.min, axis.tickSize);
88-
do {
89-
tickVal = tickMin + (tickCount++) * axis.tickSize;
90-
returnTicks.push(tickVal);
91-
} while (tickVal < axis.max);
92-
93-
return returnTicks;
94-
};
95-
78+
79+
//Count the steps
80+
while (Math.abs(delta) >= STEP_SIZE) {
81+
steps++;
82+
delta /= STEP_SIZE;
83+
}
84+
85+
//Set the tick size relative to the remaining delta
86+
while (tickSize <= STEP_SIZE) {
87+
if (delta <= tickSize) {
88+
break;
89+
}
90+
tickSize *= 2;
91+
}
92+
93+
//Tell flot the tickSize we've calculated
94+
if (typeof opts.minTickSize !== 'undefined' && tickSize < opts.minTickSize) {
95+
axis.tickSize = opts.minTickSize;
96+
} else {
97+
axis.tickSize = tickSize * Math.pow(STEP_SIZE, steps);
98+
}
99+
100+
//Calculate the new ticks
101+
tickMin = floorInBase(axis.min, axis.tickSize);
102+
do {
103+
tickVal = tickMin + (tickCount++) * axis.tickSize;
104+
returnTicks.push(tickVal);
105+
} while (tickVal < axis.max);
106+
107+
return returnTicks;
108+
};
109+
}
110+
96111
axis.tickFormatter = function (size, axis) {
97112
var steps = 0;
98113

@@ -110,7 +125,13 @@
110125
ext += 'B';
111126
}
112127

113-
return (size.toFixed(axis.tickDecimals) + ' ' + ext);
128+
let out = size.toFixed(axis.tickDecimals);
129+
if (out.endsWith('.00')) {
130+
out = out.slice(0, -3);
131+
}
132+
133+
return out+ ' ' + ext;
134+
114135
};
115136
}
116137
});

0 commit comments

Comments
 (0)