From 6f0ce35ca271691082e8377ae2c9f43a11f9d2ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sat, 15 Feb 2014 10:15:05 +0100 Subject: [PATCH] Closes #97, legend values now use selected y axis formater for precision and formating --- src/app/directives/grafanaGraph.js | 52 ++++++++++++++------------- src/app/panels/graphite/timeSeries.js | 35 +++++++++++++++--- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/app/directives/grafanaGraph.js b/src/app/directives/grafanaGraph.js index 8148368f508..12b2ea58677 100644 --- a/src/app/directives/grafanaGraph.js +++ b/src/app/directives/grafanaGraph.js @@ -132,35 +132,13 @@ function (angular, $, kbn, moment, _) { } }; - if (panel.grid.threshold1) { - var limit1 = panel.grid.thresholdLine ? panel.grid.threshold1 : (panel.grid.threshold2 || null); - options.grid.markings = []; - options.grid.markings.push({ - yaxis: { from: panel.grid.threshold1, to: limit1 }, - color: panel.grid.threshold1Color - }); - - if (panel.grid.threshold2) { - var limit2; - if (panel.grid.thresholdLine) { - limit2 = panel.grid.threshold2; - } else { - limit2 = panel.grid.threshold1 > panel.grid.threshold2 ? -Infinity : +Infinity; - } - options.grid.markings.push({ - yaxis: { from: panel.grid.threshold2, to: limit2 }, - color: panel.grid.threshold2Color - }); - } - } - - addAnnotations(options); - for (var i = 0; i < data.length; i++) { - var _d = data[i].getFlotPairs(panel.nullPointMode); + var _d = data[i].getFlotPairs(panel.nullPointMode, panel.y_formats); data[i].data = _d; } + addGridThresholds(options, panel); + addAnnotations(options); configureAxisOptions(data, options); plot = $.plot(elem, data, options); @@ -210,6 +188,30 @@ function (angular, $, kbn, moment, _) { elem.html(''); } + function addGridThresholds(options, panel) { + if (panel.grid.threshold1) { + var limit1 = panel.grid.thresholdLine ? panel.grid.threshold1 : (panel.grid.threshold2 || null); + options.grid.markings = []; + options.grid.markings.push({ + yaxis: { from: panel.grid.threshold1, to: limit1 }, + color: panel.grid.threshold1Color + }); + + if (panel.grid.threshold2) { + var limit2; + if (panel.grid.thresholdLine) { + limit2 = panel.grid.threshold2; + } else { + limit2 = panel.grid.threshold1 > panel.grid.threshold2 ? -Infinity : +Infinity; + } + options.grid.markings.push({ + yaxis: { from: panel.grid.threshold2, to: limit2 }, + color: panel.grid.threshold2Color + }); + } + } + } + function addAnnotations(options) { if(!data.annotations || data.annotations.length === 0) { return; diff --git a/src/app/panels/graphite/timeSeries.js b/src/app/panels/graphite/timeSeries.js index 5dfad4f115a..03f8ae08066 100644 --- a/src/app/panels/graphite/timeSeries.js +++ b/src/app/panels/graphite/timeSeries.js @@ -1,7 +1,8 @@ define([ - 'underscore' + 'underscore', + 'kbn' ], -function (_) { +function (_, kbn) { 'use strict'; var ts = {}; @@ -12,7 +13,7 @@ function (_) { this.label = opts.info.alias; }; - ts.ZeroFilled.prototype.getFlotPairs = function (fillStyle) { + ts.ZeroFilled.prototype.getFlotPairs = function (fillStyle, yFormats) { var result = []; this.color = this.info.color; @@ -50,13 +51,39 @@ function (_) { }, this); if (result.length) { - this.info.avg = (this.info.total / result.length).toFixed(2); + this.info.avg = (this.info.total / result.length); this.info.current = result[result.length-1][1]; + + var formater = getFormater(yFormats[this.yaxis - 1]); + this.info.avg = formater(this.info.avg); + this.info.current = formater(this.info.current); + this.info.min = formater(this.info.min); + this.info.max = formater(this.info.max); + this.info.total = formater(this.info.total); } return result; }; + function getFormater(yformat) { + switch(yformat) { + case 'bytes': + return kbn.byteFormat; + case 'short': + return kbn.shortFormat; + case 'ms': + return kbn.msFormat; + default: + return function(val) { + if (val % 1 === 0) { + return val; + } + else { + return val.toFixed(2); + } + }; + } + } return ts; }); \ No newline at end of file