Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional thousands separators to Gauge control #37

Merged
merged 2 commits into from
Aug 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/riemann/dash/public/format.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
var format = (function() {

var formatFloat = function(number, precision) {
var formatFloat = function(number, precision, commas) {
precision = precision || 2;
var base = Math.pow(10, precision);
return Math.round(number * base) / base;
var val = Math.round(number * base) / base;

if(!commas) {
return val;
}

var parts = (val + '').split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}

var metricTemplate = _.template('<div class="bar {{state}}" style="width: {{percent}}%">{{metric}}</div>');
Expand Down
10 changes: 7 additions & 3 deletions lib/riemann/dash/public/views/gauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
view.View.call(this, json);
this.query = json.query;
this.title = json.title;
this.commaSeperateThousands = json.commaSeperateThousands;
this.clickFocusable = true;
this.el.addClass('gauge');
this.el.append(
Expand All @@ -23,7 +24,7 @@
var value = this.el.find('.value');
this.sub = subs.subscribe(this.query, function(e) {
me.box.attr('class', 'box state ' + e.state);
value.text(format.float(e.metric));
value.text(format.float(e.metric, 2, me.commaSeperateThousands));
value.attr('title', e.description);

// The first time, do a full-height reflow.
Expand All @@ -45,15 +46,18 @@
return $.extend(view.View.prototype.json.call(this), {
type: 'Gauge',
title: this.title,
query: this.query
query: this.query,
commaSeperateThousands: this.commaSeperateThousands
});
}

var editTemplate = _.template(
"<label for='title'>Title</label>" +
"<input type='text' name='title' value='{{title}}' /><br />" +
"<label for='query'>Query</label>" +
'<textarea type="text" name="query" class="query">{{query}}</textarea>' );
'<textarea type="text" name="query" class="query">{{query}}</textarea>' +
"<label for='commaSeperateThousands'>Comma Seperate Thousands</label>" +
"<input type='checkbox' name='commaSeperateThousands' {% if(commaSeperateThousands) { %} checked='checked' {% } %} />" );

Gauge.prototype.editForm = function() {
return editTemplate(this);
Expand Down