Skip to content

Commit

Permalink
yaxis tick formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Jan 13, 2017
1 parent 840df9a commit af24338
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/core_plugins/timelion/public/panels/timechart/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = function timechartFn(Private, config, $rootScope, timefilter, $
render: function ($scope, $elem) {
const template = '<div class="chart-top-title"></div><div class="chart-canvas"></div>';
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'));

// TODO: I wonder if we should supply our own moment that sets this every time?
Expand Down Expand Up @@ -147,7 +148,11 @@ module.exports = function timechartFn(Private, config, $rootScope, timefilter, $
}

if (y != null) {
legendValueNumbers.eq(i).text('(' + y.toFixed(precision) + ')');
let label = y.toFixed(precision);
if (series.yaxis.tickFormatter) {
label = series.yaxis.tickFormatter(label, series.yaxis);
}
legendValueNumbers.eq(i).text(`(${label})`);
} else {
legendValueNumbers.eq(i).empty();
}
Expand Down Expand Up @@ -219,6 +224,12 @@ 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]];
}
});
}

return series;
Expand Down
53 changes: 53 additions & 0 deletions src/core_plugins/timelion/public/services/tick_formatters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
define(function (require) {

return function ticketFormatters() {
var formatters = {
'bits': function (val, axis) {
var labels = ['b','kb','mb','gb','tb','pb'];
var index = 0;
while (val > 1000 && index < labels.length) {
val /= 1000;
index++;
}
return (Math.round(val * 100) / 100) + labels[index];
},
'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) {
val /= 1000;
index++;
}
return (Math.round(val * 100) / 100) + labels[index];
},
'bytes': function (val, axis) {
var labels = ['B','KB','MB','GB','TB','PB'];
var index = 0;
while (val > 1024 && index < labels.length) {
val /= 1024;
index++;
}
return (Math.round(val * 100) / 100) + labels[index];
},
'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) {
val /= 1024;
index++;
}
return (Math.round(val * 100) / 100) + labels[index];
},
'currency': function (val, axis) {
return val.toLocaleString('en', { style: 'currency', currency: axis.options.units[1] });
},
'custom': function (val, axis) {
var prefix = axis.options._units[1] || '';
var suffix = axis.options._units[2] || '';
return prefix + val + suffix;
}
};

return formatters;
};
});
24 changes: 22 additions & 2 deletions src/core_plugins/timelion/server/series_functions/yaxis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import _ from 'lodash';
const alter = require('../lib/alter.js');

const Chainable = require('../lib/classes/chainable');
const tickFormatters = {
'bits':'bits',
'bits/s':'bits/s',
'bytes':'bytes',
'bytes/s':'bytes/s',
'currency':'currency(:prefix)',
'custom':'custom(:prefix:suffix)'
};
module.exports = new Chainable('yaxis', {
args: [
{
Expand Down Expand Up @@ -37,10 +46,15 @@ module.exports = new Chainable('yaxis', {
types: ['string', 'null'],
help: 'Color of axis label'
},
{
name: 'units',
types: ['string', 'null'],
help: 'The function to use for formatting y-axis labels. One of: ' + _.values(tickFormatters).join(', ')
},
],
help: 'Configures a variety of y-axis options, the most important likely being the ability to add an Nth (eg 2nd) y-axis',
fn: function yaxisFn(args) {
return alter(args, function (eachSeries, yaxis, min, max, position, label, color) {
return alter(args, function (eachSeries, yaxis, min, max, position, label, color, units) {
yaxis = yaxis || 1;

eachSeries.yaxis = yaxis;
Expand All @@ -58,7 +72,13 @@ module.exports = new Chainable('yaxis', {
myAxis.axisLabelColour = color;
myAxis.axisLabelUseCanvas = true;


if (units) {
var unitTokens = units.split(':');
if (!tickFormatters[unitTokens[0]]) {
throw new Error (`${units} is not a supported unit type.`);
}
myAxis.units = unitTokens;
}

return eachSeries;
});
Expand Down

0 comments on commit af24338

Please sign in to comment.