From ac961d12d1292bc4e55fe025c842feec04aa36ed Mon Sep 17 00:00:00 2001 From: 1998-felix Date: Fri, 15 Mar 2024 11:28:48 +0300 Subject: [PATCH] feat: Change chart modal for data sources Signed-off-by: 1998-felix --- ui/web/static/js/charts.js | 98 +++++++---- .../charts/horizontalbarchartmodal.html | 163 ++++++++++++++---- 2 files changed, 196 insertions(+), 65 deletions(-) diff --git a/ui/web/static/js/charts.js b/ui/web/static/js/charts.js index ef694380..8224ede9 100644 --- a/ui/web/static/js/charts.js +++ b/ui/web/static/js/charts.js @@ -706,7 +706,14 @@ class HorizontalBarChart extends Echart { }, dataset: { source: [ - [], + [ + "thing", + "value" + ], + [ + "thing0", + 99 + ], ] }, grid: { containLabel: true }, @@ -747,53 +754,74 @@ class HorizontalBarChart extends Echart { horizontalBarChart.setOption(option); var chartData = { - channel: '${this.chartData.channel}', - publisher: '${this.chartData.thing}', + channels: ${this.chartData.channel}, + things: ${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) { + }; + fetchData(horizontalBarChart, chartData); + + async function fetchData(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); + const thingIds = getThingIds(chartData); + const plotData = [['value', 'thing']]; - if (!response.ok) { - throw new Error('API request failed with status ${response.status}'); - } - - const data = await response.json(); - updateChart(data); + for (let i = 0; i < chartData.channels.length; i++) { + const apiEndpoint = '${pathPrefix}/data?channel=' + chartData.channel[i] + + '&name=' + chartData.name + + '&from='+ chartData.from + + '&to=' + chartData.to + + '&aggregation=' + chartData.aggregation + + '&limit=10' + + '&interval=' + getInterval(chartData) + + '&publisher=' + chartData.things[i]; + const response = await fetch(apiEndpoint); + if (!response.ok) { + throw new Error('HTTP request failed with status:', response.status) + } + const data = await response.json(); + plotData.push([data.messages[0].value, 'thing'+i.toString()]); + } + updateChart(horizontalBarChart, chartData, plotData); } 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 + console.error("Error fetching data:", error); } } - 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 + function updateChart(horizontalBarChart,chartData,plotData) { + option = horizontalBarChart.getOption(); + option.series[0].encode.x = 'value'; + option.series[0].encode.y = 'thing'; + option.visualMap.text = ['High'+chartData.name, 'Low'+chartData.name]; + option.dataset[0].source = plotData; + horizontalBarChart.setOption(option); + } + + function getInterval(chartData) { + const interval = chartData.to - chartData.from; + const nanosecondsPerSecond = 1e3; + const secondsPerMinute = 60; + const minutesPerHour = 60; + let minutes = 0; + let hours = 0; + let intervalString = ""; + + let seconds = parseInt(interval) / nanosecondsPerSecond; + intervalString = Math.ceil(seconds).toString() +'s'; + if (seconds >= secondsPerMinute) { + minutes = seconds/ secondsPerMinute; + intervalString = Math.ceil(minutes).toString() + 'm'; + } + if (minutes >= minutesPerHour) { + hours = minutes /minutesPerHour; + intervalString = Math.ceil(hours).toString() + 'h'; + } + return intervalString; } - } `; } } diff --git a/ui/web/templates/charts/horizontalbarchartmodal.html b/ui/web/templates/charts/horizontalbarchartmodal.html index 411152a4..d75e3cef 100644 --- a/ui/web/templates/charts/horizontalbarchartmodal.html +++ b/ui/web/templates/charts/horizontalbarchartmodal.html @@ -63,27 +63,71 @@