From abffa93849195b72c30760f16d166aa4b479ecc0 Mon Sep 17 00:00:00 2001 From: "felix.gateru" Date: Mon, 11 Mar 2024 16:53:45 +0300 Subject: [PATCH 1/4] Add data to horizontal bar chart Signed-off-by: felix.gateru --- ui/web/static/js/charts.js | 61 ++++++++++++++++--- .../charts/horizontalbarchartmodal.html | 45 ++++---------- 2 files changed, 63 insertions(+), 43 deletions(-) diff --git a/ui/web/static/js/charts.js b/ui/web/static/js/charts.js index 2b55ad99..ef694380 100644 --- a/ui/web/static/js/charts.js +++ b/ui/web/static/js/charts.js @@ -706,13 +706,7 @@ class HorizontalBarChart extends Echart { }, dataset: { source: [ - ['score', 'amount', 'product'], - [79.3,85212, 'thing1'], - [57.1, 78254, 'thing2'], - [14.4, 41032, 'thing3'], - [30.1, 12755, 'thing4'], - [18.7, 20145, 'thing5'], - [88.1, 89146, 'thing6'], + [], ] }, grid: { containLabel: true }, @@ -731,7 +725,7 @@ class HorizontalBarChart extends Echart { visualMap: { orient: 'horizontal', left: 'center', - min: 10, + min: 0, max: 100, text: ['High temperature', 'Low temperature'], // Map the score column to color @@ -751,7 +745,56 @@ class HorizontalBarChart extends Echart { ] }; - horizontalBarChart.setOption(option);`; + horizontalBarChart.setOption(option); + var chartData = { + channel: '${this.chartData.channel}', + publisher: '${this.chartData.thing}', + name: '${this.chartData.valueName}', + from: ${this.chartData.startTime}, + to: ${this.chartData.stopTime}, + aggregation: '${this.chartData.aggregationType}', + limit: 100, + interval : getInterval(), + } + + async getData(horizontalBarChart, chartData) { + try { + const apiEndpoint = "/data?channel=" + chartData.channel + + "&name=" + chartData.name + + "&from=" + chartData.from + + "&to=" + chartData.to + + "&aggregation=" + chartData.aggregation + + "&limit=" + chartData.limit + + "&interval=" + chartData.interval; + + const response = await fetch(apiEndpoint); + + if (!response.ok) { + throw new Error('API request failed with status ${response.status}'); + } + + const data = await response.json(); + updateChart(data); + + } catch (error) { + console.error("Error fetching data:", error); + // Handle the error (e.g., display an error message) + } finally { + // Schedule the next update + setTimeout(() => this.fetchDataAndUpdate(), 5000); // Example: 5 seconds + } + } + + updateChart(data) { + const barChart = echarts.init(document.getElementById(this.ID)); + const option = barChart.getOption(); // Get the existing options + + option.series[0].data = data.seriesData; + + barChart.setOption(option); // Update the chart + } + } + `; } } diff --git a/ui/web/templates/charts/horizontalbarchartmodal.html b/ui/web/templates/charts/horizontalbarchartmodal.html index 5d5a6c9a..411152a4 100644 --- a/ui/web/templates/charts/horizontalbarchartmodal.html +++ b/ui/web/templates/charts/horizontalbarchartmodal.html @@ -76,19 +76,6 @@
Data Source
case "startTime": case "stopTime": const inputDate = new Date(pair[1]); - chartData[pair[0]] = inputDate.getTime() * 1e6; + chartData[pair[0]] = inputDate.getTime(); break; case "channel": channels.push(pair[1]); From 179d58baf458bb99cb3c1ec1c820ebfb3dbe1e67 Mon Sep 17 00:00:00 2001 From: 1998-felix Date: Wed, 20 Mar 2024 10:10:03 +0300 Subject: [PATCH 4/4] feat: Remove conversion to unix Nano Signed-off-by: 1998-felix --- ui/web/static/js/charts.js | 4 +- .../charts/horizontalbarchartmodal.html | 70 +++++++++++++++---- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/ui/web/static/js/charts.js b/ui/web/static/js/charts.js index 294b9242..a2169fc7 100644 --- a/ui/web/static/js/charts.js +++ b/ui/web/static/js/charts.js @@ -798,14 +798,14 @@ class HorizontalBarChart extends Echart { function getInterval(chartData) { const interval = chartData.to - chartData.from; - const nanosecondsPerSecond = 1e3; + const millisecondsPerSecond = 1e3; const secondsPerMinute = 60; const minutesPerHour = 60; let minutes = 0; let hours = 0; let intervalString = ""; - let seconds = parseInt(interval) / nanosecondsPerSecond; + let seconds = parseInt(interval) / millisecondsPerSecond; intervalString = Math.ceil(seconds).toString() +'s'; if (seconds >= secondsPerMinute) { minutes = seconds/ secondsPerMinute; diff --git a/ui/web/templates/charts/horizontalbarchartmodal.html b/ui/web/templates/charts/horizontalbarchartmodal.html index 5191e335..521e001b 100644 --- a/ui/web/templates/charts/horizontalbarchartmodal.html +++ b/ui/web/templates/charts/horizontalbarchartmodal.html @@ -18,7 +18,7 @@