|
@@ -6,32 +6,59 @@ import moment = require('moment');
|
|
|
|
|
|
|
|
export class TableRenderer {
|
|
export class TableRenderer {
|
|
|
formaters: any[];
|
|
formaters: any[];
|
|
|
|
|
+ colorState: any;
|
|
|
|
|
|
|
|
constructor(private panel, private table, private timezone) {
|
|
constructor(private panel, private table, private timezone) {
|
|
|
this.formaters = [];
|
|
this.formaters = [];
|
|
|
|
|
+ this.colorState = {};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- createColumnFormater(style) {
|
|
|
|
|
- return (v) => {
|
|
|
|
|
- if (v === null || v === void 0) {
|
|
|
|
|
- return '-';
|
|
|
|
|
- }
|
|
|
|
|
- if (_.isString(v) || !style) {
|
|
|
|
|
- return v;
|
|
|
|
|
|
|
+ getColorForValue(value, style) {
|
|
|
|
|
+ if (!style.thresholds) { return null; }
|
|
|
|
|
+
|
|
|
|
|
+ for (var i = style.thresholds.length - 1; i >= 0 ; i--) {
|
|
|
|
|
+ if (value >= style.thresholds[i]) {
|
|
|
|
|
+ return style.colors[i];
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ createColumnFormater(style) {
|
|
|
|
|
+ if (!style) {
|
|
|
|
|
+ return v => v;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (style.type === 'date') {
|
|
|
|
|
|
|
+ if (style.type === 'date') {
|
|
|
|
|
+ return v => {
|
|
|
if (_.isArray(v)) { v = v[0]; }
|
|
if (_.isArray(v)) { v = v[0]; }
|
|
|
var date = moment(v);
|
|
var date = moment(v);
|
|
|
if (this.timezone === 'utc') {
|
|
if (this.timezone === 'utc') {
|
|
|
date = date.utc();
|
|
date = date.utc();
|
|
|
}
|
|
}
|
|
|
return date.format(style.dateFormat);
|
|
return date.format(style.dateFormat);
|
|
|
- }
|
|
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (_.isNumber(v) && style.type === 'number') {
|
|
|
|
|
- let valueFormater = kbn.valueFormats[style.unit];
|
|
|
|
|
- return valueFormater(v, style.decimals);
|
|
|
|
|
|
|
+ if (style.type === 'number') {
|
|
|
|
|
+ let valueFormater = kbn.valueFormats[style.unit];
|
|
|
|
|
+
|
|
|
|
|
+ return v => {
|
|
|
|
|
+ if (v === null || v === void 0) {
|
|
|
|
|
+ return '-';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (style.colorMode) {
|
|
|
|
|
+ this.colorState[style.colorMode] = this.getColorForValue(v, style);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return valueFormater(v, style.decimals, null);
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return v => {
|
|
|
|
|
+ if (v === null || v === void 0) {
|
|
|
|
|
+ return '-';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (_.isArray(v)) {
|
|
if (_.isArray(v)) {
|
|
@@ -65,8 +92,18 @@ export class TableRenderer {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
renderCell(columnIndex, value) {
|
|
renderCell(columnIndex, value) {
|
|
|
- var colValue = this.formatColumnValue(columnIndex, value);
|
|
|
|
|
- return '<td>' + colValue + '</td>';
|
|
|
|
|
|
|
+ var value = this.formatColumnValue(columnIndex, value);
|
|
|
|
|
+ var style = '';
|
|
|
|
|
+ if (this.colorState.cell) {
|
|
|
|
|
+ style = ' style="background-color:' + this.colorState.cell + ';color: white"';
|
|
|
|
|
+ this.colorState.cell = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (this.colorState.value) {
|
|
|
|
|
+ style = ' style="color:' + this.colorState.value + '"';
|
|
|
|
|
+ this.colorState.value = null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return '<td' + style + '>' + value + '</td>';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
render(page) {
|
|
render(page) {
|