Commit c13d4f89d0658ae71b0ae90fd9d09e4e6d46bc6c

Authored by Igor Kulikov
Committed by GitHub
2 parents d96c1c40 914264b0

Merge pull request #208 from thingsboard/feature/TB-70

TB-70: Improve timeseries table widget. Fix numeric value conversion …
@@ -109,9 +109,9 @@ @@ -109,9 +109,9 @@
109 "sizeX": 8, 109 "sizeX": 8,
110 "sizeY": 6.5, 110 "sizeY": 6.5,
111 "resources": [], 111 "resources": [],
112 - "templateHtml": "<tb-timeseries-table-widget \n config=\"config\"\n table-id=\"tableId\"\n datasources=\"datasources\"\n data=\"data\">\n</tb-timeseries-table-widget>", 112 + "templateHtml": "<tb-timeseries-table-widget \n table-id=\"tableId\"\n ctx=\"ctx\">\n</tb-timeseries-table-widget>",
113 "templateCss": "", 113 "templateCss": "",
114 - "controllerScript": "self.onInit = function() {\n \n var scope = self.ctx.$scope;\n var id = self.ctx.$scope.$injector.get('utils').guid();\n\n scope.config = {\n settings: self.ctx.settings,\n widgetConfig: self.ctx.widgetConfig\n }\n\n scope.datasources = self.ctx.datasources;\n scope.data = self.ctx.data;\n scope.tableId = \"table-\"+id;\n \n}\n\nself.onDataUpdated = function() {\n self.ctx.$scope.data = self.ctx.data;\n self.ctx.$scope.$broadcast('timeseries-table-data-updated', self.ctx.$scope.tableId);\n}\n\nself.onDestroy = function() {\n}", 114 + "controllerScript": "self.onInit = function() {\n var scope = self.ctx.$scope;\n var id = self.ctx.$scope.$injector.get('utils').guid();\n scope.tableId = \"table-\"+id;\n scope.ctx = self.ctx;\n}\n\nself.onDataUpdated = function() {\n self.ctx.$scope.$broadcast('timeseries-table-data-updated', self.ctx.$scope.tableId);\n}\n\nself.onDestroy = function() {\n}",
115 "settingsSchema": "{\n \"schema\": {\n \"type\": \"object\",\n \"title\": \"TimeseriesTableSettings\",\n \"properties\": {\n \"showTimestamp\": {\n \"title\": \"Display timestamp column\",\n \"type\": \"boolean\",\n \"default\": true\n }\n },\n \"required\": []\n },\n \"form\": [\n \"showTimestamp\"\n ]\n}", 115 "settingsSchema": "{\n \"schema\": {\n \"type\": \"object\",\n \"title\": \"TimeseriesTableSettings\",\n \"properties\": {\n \"showTimestamp\": {\n \"title\": \"Display timestamp column\",\n \"type\": \"boolean\",\n \"default\": true\n }\n },\n \"required\": []\n },\n \"form\": [\n \"showTimestamp\"\n ]\n}",
116 "dataKeySettingsSchema": "{\n \"schema\": {\n \"type\": \"object\",\n \"title\": \"DataKeySettings\",\n \"properties\": {\n \"useCellStyleFunction\": {\n \"title\": \"Use cell style function\",\n \"type\": \"boolean\",\n \"default\": false\n },\n \"cellStyleFunction\": {\n \"title\": \"Cell style function: f(value)\",\n \"type\": \"string\",\n \"default\": \"\"\n },\n \"useCellContentFunction\": {\n \"title\": \"Use cell content function\",\n \"type\": \"boolean\",\n \"default\": false\n },\n \"cellContentFunction\": {\n \"title\": \"Cell content function: f(value, rowData, filter)\",\n \"type\": \"string\",\n \"default\": \"\"\n }\n },\n \"required\": []\n },\n \"form\": [\n \"useCellStyleFunction\",\n {\n \"key\": \"cellStyleFunction\",\n \"type\": \"javascript\"\n },\n \"useCellContentFunction\",\n {\n \"key\": \"cellContentFunction\",\n \"type\": \"javascript\"\n }\n ]\n}", 116 "dataKeySettingsSchema": "{\n \"schema\": {\n \"type\": \"object\",\n \"title\": \"DataKeySettings\",\n \"properties\": {\n \"useCellStyleFunction\": {\n \"title\": \"Use cell style function\",\n \"type\": \"boolean\",\n \"default\": false\n },\n \"cellStyleFunction\": {\n \"title\": \"Cell style function: f(value)\",\n \"type\": \"string\",\n \"default\": \"\"\n },\n \"useCellContentFunction\": {\n \"title\": \"Use cell content function\",\n \"type\": \"boolean\",\n \"default\": false\n },\n \"cellContentFunction\": {\n \"title\": \"Cell content function: f(value, rowData, filter)\",\n \"type\": \"string\",\n \"default\": \"\"\n }\n },\n \"required\": []\n },\n \"form\": [\n \"useCellStyleFunction\",\n {\n \"key\": \"cellStyleFunction\",\n \"type\": \"javascript\"\n },\n \"useCellContentFunction\",\n {\n \"key\": \"cellContentFunction\",\n \"type\": \"javascript\"\n }\n ]\n}",
117 "defaultConfig": "{\"datasources\":[{\"type\":\"function\",\"name\":\"function\",\"dataKeys\":[{\"name\":\"f(x)\",\"type\":\"function\",\"label\":\"Temperature °C\",\"color\":\"#2196f3\",\"settings\":{\"useCellStyleFunction\":true,\"cellStyleFunction\":\"if (value) {\\n var percent = (value + 60)/120 * 100;\\n var color = tinycolor.mix('blue', 'red', amount = percent);\\n color.setAlpha(.5);\\n return {\\n paddingLeft: '20px',\\n color: '#ffffff',\\n background: color.toRgbString(),\\n fontSize: '18px'\\n };\\n} else {\\n return {};\\n}\"},\"_hash\":0.8587686344902596,\"funcBody\":\"var value = prevValue + Math.random() * 40 - 20;\\nvar multiplier = Math.pow(10, 1 || 0);\\nvar value = Math.round(value * multiplier) / multiplier;\\nif (value < -60) {\\n\\tvalue = -60;\\n} else if (value > 60) {\\n\\tvalue = 60;\\n}\\nreturn value;\"},{\"name\":\"f(x)\",\"type\":\"function\",\"label\":\"Humidity, %\",\"color\":\"#ffc107\",\"settings\":{\"useCellStyleFunction\":true,\"cellStyleFunction\":\"if (value) {\\n var percent = value;\\n var backgroundColor = tinycolor('blue');\\n backgroundColor.setAlpha(value/100);\\n var color = 'blue';\\n if (value > 50) {\\n color = 'white';\\n }\\n \\n return {\\n paddingLeft: '20px',\\n color: color,\\n background: backgroundColor.toRgbString(),\\n fontSize: '18px'\\n };\\n} else {\\n return {};\\n}\",\"useCellContentFunction\":false},\"_hash\":0.12775350966079668,\"funcBody\":\"var value = prevValue + Math.random() * 20 - 10;\\nvar multiplier = Math.pow(10, 1 || 0);\\nvar value = Math.round(value * multiplier) / multiplier;\\nif (value < 5) {\\n\\tvalue = 5;\\n} else if (value > 100) {\\n\\tvalue = 100;\\n}\\nreturn value;\"}]}],\"timewindow\":{\"realtime\":{\"interval\":1000,\"timewindowMs\":60000},\"aggregation\":{\"type\":\"NONE\",\"limit\":200}},\"showTitle\":true,\"backgroundColor\":\"rgb(255, 255, 255)\",\"color\":\"rgba(0, 0, 0, 0.87)\",\"padding\":\"8px\",\"settings\":{\"showTimestamp\":true},\"title\":\"Timeseries table\",\"dropShadow\":true,\"enableFullscreen\":true,\"titleStyle\":{\"fontSize\":\"16px\",\"fontWeight\":400},\"useDashboardTimewindow\":false,\"showLegend\":false}" 117 "defaultConfig": "{\"datasources\":[{\"type\":\"function\",\"name\":\"function\",\"dataKeys\":[{\"name\":\"f(x)\",\"type\":\"function\",\"label\":\"Temperature °C\",\"color\":\"#2196f3\",\"settings\":{\"useCellStyleFunction\":true,\"cellStyleFunction\":\"if (value) {\\n var percent = (value + 60)/120 * 100;\\n var color = tinycolor.mix('blue', 'red', amount = percent);\\n color.setAlpha(.5);\\n return {\\n paddingLeft: '20px',\\n color: '#ffffff',\\n background: color.toRgbString(),\\n fontSize: '18px'\\n };\\n} else {\\n return {};\\n}\"},\"_hash\":0.8587686344902596,\"funcBody\":\"var value = prevValue + Math.random() * 40 - 20;\\nvar multiplier = Math.pow(10, 1 || 0);\\nvar value = Math.round(value * multiplier) / multiplier;\\nif (value < -60) {\\n\\tvalue = -60;\\n} else if (value > 60) {\\n\\tvalue = 60;\\n}\\nreturn value;\"},{\"name\":\"f(x)\",\"type\":\"function\",\"label\":\"Humidity, %\",\"color\":\"#ffc107\",\"settings\":{\"useCellStyleFunction\":true,\"cellStyleFunction\":\"if (value) {\\n var percent = value;\\n var backgroundColor = tinycolor('blue');\\n backgroundColor.setAlpha(value/100);\\n var color = 'blue';\\n if (value > 50) {\\n color = 'white';\\n }\\n \\n return {\\n paddingLeft: '20px',\\n color: color,\\n background: backgroundColor.toRgbString(),\\n fontSize: '18px'\\n };\\n} else {\\n return {};\\n}\",\"useCellContentFunction\":false},\"_hash\":0.12775350966079668,\"funcBody\":\"var value = prevValue + Math.random() * 20 - 10;\\nvar multiplier = Math.pow(10, 1 || 0);\\nvar value = Math.round(value * multiplier) / multiplier;\\nif (value < 5) {\\n\\tvalue = 5;\\n} else if (value > 100) {\\n\\tvalue = 100;\\n}\\nreturn value;\"}]}],\"timewindow\":{\"realtime\":{\"interval\":1000,\"timewindowMs\":60000},\"aggregation\":{\"type\":\"NONE\",\"limit\":200}},\"showTitle\":true,\"backgroundColor\":\"rgb(255, 255, 255)\",\"color\":\"rgba(0, 0, 0, 0.87)\",\"padding\":\"8px\",\"settings\":{\"showTimestamp\":true},\"title\":\"Timeseries table\",\"dropShadow\":true,\"enableFullscreen\":true,\"titleStyle\":{\"fontSize\":\"16px\",\"fontWeight\":400},\"useDashboardTimewindow\":false,\"showLegend\":false}"
@@ -589,7 +589,7 @@ function DatasourceSubscription(datasourceSubscription, telemetryWebsocketServic @@ -589,7 +589,7 @@ function DatasourceSubscription(datasourceSubscription, telemetryWebsocketServic
589 if (keyData.length > 0) { 589 if (keyData.length > 0) {
590 series = keyData[0]; 590 series = keyData[0];
591 time = series[0]; 591 time = series[0];
592 - value = series[1]; 592 + value = convertValue(series[1]);
593 if (dataKey.postFunc) { 593 if (dataKey.postFunc) {
594 value = dataKey.postFunc(time, value, prevSeries[1]); 594 value = dataKey.postFunc(time, value, prevSeries[1]);
595 } 595 }
@@ -814,7 +814,7 @@ export default function WidgetController($scope, $state, $timeout, $window, $ele @@ -814,7 +814,7 @@ export default function WidgetController($scope, $state, $timeout, $window, $ele
814 function formatValue(value, dec, units) { 814 function formatValue(value, dec, units) {
815 if (angular.isDefined(value) && 815 if (angular.isDefined(value) &&
816 value !== null && isNumeric(value)) { 816 value !== null && isNumeric(value)) {
817 - var formatted = value; 817 + var formatted = Number(value);
818 if (angular.isDefined(dec)) { 818 if (angular.isDefined(dec)) {
819 formatted = formatted.toFixed(dec); 819 formatted = formatted.toFixed(dec);
820 } 820 }
@@ -824,7 +824,7 @@ export default function WidgetController($scope, $state, $timeout, $window, $ele @@ -824,7 +824,7 @@ export default function WidgetController($scope, $state, $timeout, $window, $ele
824 } 824 }
825 return formatted; 825 return formatted;
826 } else { 826 } else {
827 - return ''; 827 + return value;
828 } 828 }
829 } 829 }
830 830
@@ -36,9 +36,7 @@ function TimeseriesTableWidget() { @@ -36,9 +36,7 @@ function TimeseriesTableWidget() {
36 scope: true, 36 scope: true,
37 bindToController: { 37 bindToController: {
38 tableId: '=', 38 tableId: '=',
39 - config: '=',  
40 - datasources: '=',  
41 - data: '=' 39 + ctx: '='
42 }, 40 },
43 controller: TimeseriesTableWidgetController, 41 controller: TimeseriesTableWidgetController,
44 controllerAs: 'vm', 42 controllerAs: 'vm',
@@ -53,10 +51,12 @@ function TimeseriesTableWidgetController($element, $scope, $filter) { @@ -53,10 +51,12 @@ function TimeseriesTableWidgetController($element, $scope, $filter) {
53 vm.sources = []; 51 vm.sources = [];
54 vm.sourceIndex = 0; 52 vm.sourceIndex = 0;
55 53
56 - $scope.$watch('vm.config', function() {  
57 - if (vm.config) {  
58 - vm.settings = vm.config.settings;  
59 - vm.widgetConfig = vm.config.widgetConfig; 54 + $scope.$watch('vm.ctx', function() {
  55 + if (vm.ctx) {
  56 + vm.settings = vm.ctx.settings;
  57 + vm.widgetConfig = vm.ctx.widgetConfig;
  58 + vm.data = vm.ctx.data;
  59 + vm.datasources = vm.ctx.datasources;
60 initialize(); 60 initialize();
61 } 61 }
62 }); 62 });
@@ -119,11 +119,8 @@ function TimeseriesTableWidgetController($element, $scope, $filter) { @@ -119,11 +119,8 @@ function TimeseriesTableWidgetController($element, $scope, $filter) {
119 } 119 }
120 return hash; 120 return hash;
121 } 121 }
122 - }  
123 -  
124 - $scope.$watch('vm.datasources', function() {  
125 updateDatasources(); 122 updateDatasources();
126 - }); 123 + }
127 124
128 $scope.$on('timeseries-table-data-updated', function(event, tableId) { 125 $scope.$on('timeseries-table-data-updated', function(event, tableId) {
129 if (vm.tableId == tableId) { 126 if (vm.tableId == tableId) {
@@ -186,6 +183,8 @@ function TimeseriesTableWidgetController($element, $scope, $filter) { @@ -186,6 +183,8 @@ function TimeseriesTableWidgetController($element, $scope, $filter) {
186 } catch (e) { 183 } catch (e) {
187 content = strContent; 184 content = strContent;
188 } 185 }
  186 + } else {
  187 + content = vm.ctx.utils.formatValue(value, contentInfo.decimals, contentInfo.units);
189 } 188 }
190 return content; 189 return content;
191 } 190 }
@@ -271,7 +270,9 @@ function TimeseriesTableWidgetController($element, $scope, $filter) { @@ -271,7 +270,9 @@ function TimeseriesTableWidgetController($element, $scope, $filter) {
271 270
272 source.ts.contentsInfo.push({ 271 source.ts.contentsInfo.push({
273 useCellContentFunction: useCellContentFunction, 272 useCellContentFunction: useCellContentFunction,
274 - cellContentFunction: cellContentFunction 273 + cellContentFunction: cellContentFunction,
  274 + units: dataKey.units,
  275 + decimals: dataKey.decimals
275 }); 276 });
276 277
277 } 278 }