Skip to content

Commit

Permalink
updating based on rashids comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Jan 13, 2017
1 parent 2b07d6a commit 8bc012e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 51 deletions.
58 changes: 11 additions & 47 deletions src/core_plugins/timelion/public/panels/timechart/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function timechartFn(Private, config, $rootScope, timefilter, $
const timezone = Private(require('plugins/timelion/services/timezone'))();
const tickFormatters = require('plugins/timelion/services/tick_formatters')();
const getxAxisFormatter = Private(require('plugins/timelion/panels/timechart/xaxis_formatter'));
const generateTicks = Private(require('plugins/timelion/panels/timechart/tick_generator'));

// TODO: I wonder if we should supply our own moment that sets this every time?
// could just use angular's injection to provide a moment service?
Expand Down Expand Up @@ -165,43 +166,6 @@ 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 @@ -261,21 +225,21 @@ module.exports = function timechartFn(Private, config, $rootScope, timefilter, $
if (objVal == null) return srcVal;
if (srcVal == null) return objVal;
});

_.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);
}
}
});
}

return series;
});

options.yaxes.forEach(yaxis => {
if (yaxis && yaxis.units) {
yaxis.tickFormatter = tickFormatters[yaxis.units.type];
const byteModes = ['bytes', 'bytes/s'];
if (byteModes.includes(yaxis.units.type)) {
yaxis.tickGenerator = generateTicks(yaxis);
}
}
});

try {
$scope.plot = $.plot(canvasElem, _.compact(series), options);
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = function generateTicksProvider() {

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;
};

return function (axis) {
return generateTicks(axis);
};
};
6 changes: 3 additions & 3 deletions src/core_plugins/timelion/public/services/tick_formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ define(function (require) {
return (Math.round(val * 100) / 100) + labels[index];
},
'currency': function (val, axis) {
return val.toLocaleString('en', { style: 'currency', currency: axis.options.units[1] });
return val.toLocaleString('en', { style: 'currency', currency: axis.options.prefix || 'USD' });
},
'custom': function (val, axis) {
var prefix = axis.options.units[1] || '';
var suffix = axis.options.units[2] || '';
var prefix = axis.options.units.prefix;
var suffix = axis.options.units.suffix;
return prefix + val + suffix;
}
};
Expand Down
6 changes: 5 additions & 1 deletion src/core_plugins/timelion/server/series_functions/yaxis.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ module.exports = new Chainable('yaxis', {
if (!tickFormatters[unitTokens[0]]) {
throw new Error (`${units} is not a supported unit type.`);
}
myAxis.units = unitTokens;
myAxis.units = {
type: unitTokens[0],
prefix: unitTokens[1] || '',
suffix: unitTokens[2] || ''
};
}

return eachSeries;
Expand Down

0 comments on commit 8bc012e

Please sign in to comment.