Skip to content

Commit

Permalink
fixing based on @rashidkpc review
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Jan 13, 2017
1 parent 6100b5f commit 2b07d6a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
41 changes: 41 additions & 0 deletions src/core_plugins/timelion/public/panels/timechart/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,43 @@ module.exports = function timechartFn(Private, config, $rootScope, timefilter, $
});
}

function floorInBase(n, base) {
return base * Math.floor(n / base);
}

function generateTicks(axis) {
const returnTicks = [];
let tickSize = 2;
let delta = axis.delta;
let steps = 0;
let tickVal;
let tickCount = 0;

//Count the steps
while (Math.abs(delta) >= 1024) {
steps++;
delta /= 1024;
}

//Set the tick size relative to the remaining delta
while (tickSize <= 1024) {
if (delta <= tickSize) {
break;
}
tickSize *= 2;
}
axis.tickSize = tickSize * Math.pow(1024,steps);

//Calculate the new ticks
const tickMin = floorInBase(axis.min, axis.tickSize);
do {
tickVal = tickMin + (tickCount++) * axis.tickSize;
returnTicks.push(tickVal);
} while (tickVal < axis.max);

return returnTicks;
}

let legendScope = $scope.$new();
function drawPlot(plotConfig) {

Expand Down Expand Up @@ -228,6 +265,10 @@ module.exports = function timechartFn(Private, config, $rootScope, timefilter, $
_.forEach(options.yaxes, yaxis => {
if (yaxis.units) {
yaxis.tickFormatter = tickFormatters[yaxis.units[0]];
const byteModes = ['bits', 'bits/s', 'bytes', 'bytes/s'];
if (byteModes.includes(yaxis.units[0])) {
yaxis.tickGenerator = generateTicks(yaxis);
}
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/core_plugins/timelion/public/services/tick_formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ define(function (require) {
'bits': function (val, axis) {
var labels = ['b','kb','mb','gb','tb','pb'];
var index = 0;
while (val > 1000 && index < labels.length) {
while (val >= 1000 && index < labels.length) {
val /= 1000;
index++;
}
Expand All @@ -14,7 +14,7 @@ define(function (require) {
'bits/s': function (val, axis) {
var labels = ['b/s','kb/s','mb/s','gb/s','tb/s','pb/s'];
var index = 0;
while (val > 1000 && index < labels.length) {
while (val >= 1000 && index < labels.length) {
val /= 1000;
index++;
}
Expand All @@ -23,7 +23,7 @@ define(function (require) {
'bytes': function (val, axis) {
var labels = ['B','KB','MB','GB','TB','PB'];
var index = 0;
while (val > 1024 && index < labels.length) {
while (val >= 1024 && index < labels.length) {
val /= 1024;
index++;
}
Expand All @@ -32,7 +32,7 @@ define(function (require) {
'bytes/s': function (val, axis) {
var labels = ['B/s','KB/s','MB/s','GB/s','TB/s','PB/s'];
var index = 0;
while (val > 1024 && index < labels.length) {
while (val >= 1024 && index < labels.length) {
val /= 1024;
index++;
}
Expand Down

0 comments on commit 2b07d6a

Please sign in to comment.