From 0b5beacbd6dd73a38901dd2e4d28dec6181bffdc Mon Sep 17 00:00:00 2001 From: ianmuchyri Date: Sun, 17 Mar 2024 15:10:15 +0300 Subject: [PATCH] initial version of stackedlinechart Signed-off-by: ianmuchyri --- ui/web/static/js/charts.js | 547 +++++++++++------- ui/web/templates/charts/linechartmodal.html | 4 +- .../charts/stackedlinechartmodal.html | 280 ++++++--- 3 files changed, 524 insertions(+), 307 deletions(-) diff --git a/ui/web/static/js/charts.js b/ui/web/static/js/charts.js index 2d4c0fe1..cb54e2c0 100644 --- a/ui/web/static/js/charts.js +++ b/ui/web/static/js/charts.js @@ -796,171 +796,171 @@ class TimeSeriesLineChart extends Echart { const names = JSON.stringify(this.chartData.seriesNames); const colors = JSON.stringify(this.chartData.colors); return ` - var lineChart = echarts.init(document.getElementById("${this.ID}")); - var option = { - title: { - text: '${this.chartData.title}', - left: 'center', - show: true - }, - tooltip: { - trigger: 'axis', - }, - xAxis: { - type: 'category', - name: '${this.chartData.xAxisLabel}', - min: '${new Date(this.chartData.startTime)}', - max: '${new Date(this.chartData.stopTime)}', - }, - yAxis: { - type: 'value', - name: '${this.chartData.yAxisLabel}', - nameLocation: 'middle', - }, - legend: { - show: true, - }, - series: [ - { - data: [], - type: 'line', - lineStyle: { - width: ${this.chartData.lineWidth}, - type: 'solid', - }, - } - ] - }; + var lineChart = echarts.init(document.getElementById("${this.ID}")); + var option = { + title: { + text: '${this.chartData.title}', + left: 'center', + show: true + }, + tooltip: { + trigger: 'axis', + }, + xAxis: { + type: 'category', + name: '${this.chartData.xAxisLabel}', + min: '${new Date(this.chartData.startTime)}', + max: '${new Date(this.chartData.stopTime)}', + }, + yAxis: { + type: 'value', + name: '${this.chartData.yAxisLabel}', + nameLocation: 'middle', + }, + legend: { + show: true, + }, + series: [ + { + data: [], + type: 'line', + lineStyle: { + width: ${this.chartData.lineWidth}, + type: 'solid', + }, + } + ] + }; - lineChart.setOption(option); - var chartdata = { - channels:${channels}, - publishers:${things}, - names:${names}, - colors:${colors}, - valueName: '${this.chartData.valueName}', - from:${this.chartData.startTime}, - to:${this.chartData.stopTime}, - aggregation:'${this.chartData.aggregationType}', - limit:100, - interval:'${this.chartData.updateInterval}', - }; - getData(lineChart,chartdata); - - async function getData(linechart,chartData) { - try { - let xAxisArray=[]; - const yAxisArray = []; - let currentTimestamp = chartData.from; - let previousTimestamp=0; - const endTimestamp = chartData.to; - const timeDifference = chartData.to - chartData.from; - let intervalNum; - switch (true) { - case (timeDifference <= 3.6*1e6): - intervalNum = 6*1e5; - break; - case (timeDifference <= 8.64 * 1e7): - intervalNum = 6*1e6; - break; - case (timeDifference <= 6.05 * 1e8): - intervalNum = 6*1e7; - break; - case (timeDifference <= 2.63 * 1e9): - intervalNum = 6*1e8; - break; - case (timeDifference <= 3.16 * 1e10): - intervalNum = 6*1e9; - break; - default: - intervalNum = 6*1e8; - break; - } + lineChart.setOption(option); + var chartdata = { + channels:${channels}, + publishers:${things}, + names:${names}, + colors:${colors}, + valueName: '${this.chartData.valueName}', + from:${this.chartData.startTime}, + to:${this.chartData.stopTime}, + aggregation:'${this.chartData.aggregationType}', + limit:100, + interval:'${this.chartData.updateInterval}', + }; + getData(lineChart,chartdata); + + async function getData(linechart,chartData) { + try { + let xAxisArray=[]; + const yAxisArray = []; + let currentTimestamp = chartData.from; + let previousTimestamp=0; + const endTimestamp = chartData.to; + const timeDifference = chartData.to - chartData.from; + let intervalNum; + switch (true) { + case (timeDifference <= 3.6*1e6): + intervalNum = 6*1e5; + break; + case (timeDifference <= 8.64 * 1e7): + intervalNum = 6*1e6; + break; + case (timeDifference <= 6.05 * 1e8): + intervalNum = 6*1e7; + break; + case (timeDifference <= 2.63 * 1e9): + intervalNum = 6*1e8; + break; + case (timeDifference <= 3.16 * 1e10): + intervalNum = 6*1e9; + break; + default: + intervalNum = 6*1e8; + break; + } - const initialXAxisArray = [] - const NoOfValues = Math.ceil(timeDifference/intervalNum); - for (i=0; i<=NoOfValues; i++) { - initialXAxisArray.push(currentTimestamp); - currentTimestamp += intervalNum; - } - xAxisArray.push(initialXAxisArray); - - for (i=0; i < chartData.channels.length; i++) { - const url = "${pathPrefix}/data?channel=" + chartData.channels[i] + - "&publisher=" + chartData.publishers[i] + - "&name=" + chartData.valueName + - "&from=" + chartData.from + - "&to=" + chartData.to + - "&aggregation=" + chartData.aggregation + - "&limit=" + chartData.limit + - "&interval=" + chartData.interval; - const response = await fetch(url); - if (response.ok) { - const data = await response.json(); - const xAxis=[]; - const yAxis=[]; - if (data.messages){ - const currArray = xAxisArray[xAxisArray.length-1]; - let currentTime, previousTime; - for (j=0; j < currArray.length; j++) { - let isempty = true; - let currentTime = currArray[j]; - for (k = (data.messages.length -1); k>= 0 ; k--) { - const item = data.messages[k]; - if (item.time > previousTime && item.time <= currentTime ) { - xAxis.push(item.time); - yAxis.push(item.value); - isEmpty= false; + const initialXAxisArray = [] + const NoOfValues = Math.ceil(timeDifference/intervalNum); + for (i=0; i<=NoOfValues; i++) { + initialXAxisArray.push(currentTimestamp); + currentTimestamp += intervalNum; + } + xAxisArray.push(initialXAxisArray); + + for (i=0; i < chartData.channels.length; i++) { + const url = "${pathPrefix}/data?channel=" + chartData.channels[i] + + "&publisher=" + chartData.publishers[i] + + "&name=" + chartData.valueName + + "&from=" + chartData.from + + "&to=" + chartData.to + + "&aggregation=" + chartData.aggregation + + "&limit=" + chartData.limit + + "&interval=" + chartData.interval; + const response = await fetch(url); + if (response.ok) { + const data = await response.json(); + const xAxis=[]; + const yAxis=[]; + if (data.messages){ + const currArray = xAxisArray[xAxisArray.length-1]; + let currentTime, previousTime; + for (j=0; j < currArray.length; j++) { + let isempty = true; + let currentTime = currArray[j]; + for (k = (data.messages.length -1); k>= 0 ; k--) { + const item = data.messages[k]; + if (item.time > previousTime && item.time <= currentTime ) { + xAxis.push(item.time); + yAxis.push(item.value); + isEmpty= false; + } } + if (isempty) { + xAxis.push(currentTime); + yAxis.push("-"); + } + previousTime=currentTime; } - if (isempty) { - xAxis.push(currentTime); - yAxis.push("-"); - } - previousTime=currentTime; } + xAxisArray.push(xAxis); + // Remove the previous element in the array + xAxisArray.shift(); + yAxisArray.push(yAxis); + } else { + // Handle errors + console.error("HTTP request failed with status:", response.status); } - xAxisArray.push(xAxis); - // Remove the previous element in the array - xAxisArray.shift(); - yAxisArray.push(yAxis); - } else { - // Handle errors - console.error("HTTP request failed with status:", response.status); } - } - const xAxisData=[]; - xAxisArray[xAxisArray.length-1].forEach((element) => { - const date = new Date(element); - xAxisData.push(date); - }) - const seriesData = [] - for (i=0; i { + const date = new Date(element); + xAxisData.push(date); + }) + const seriesData = [] + for (i=0; i { + legendData.push(element); + }); + var option = { + title: { + text: "${this.chartData.title}", + left: 'left', }, - { - name: '${seriesName[1]}', - type: 'line', - stack: 'Total', - data: [220, 182, 191, 234, 290, 330, 310] + tooltip: { + trigger: 'axis' }, - { - name: '${seriesName[2]}', - type: 'line', - stack: 'Total', - data: [150, 232, 201, 154, 190, 330, 410] + legend: { + data: legendData, }, - ] + grid: { + left: '3%', + right: '4%', + bottom: '3%', + containLabel: true + }, + xAxis: { + type: 'category', + boundaryGap: false, + name: '${this.chartData.xAxisLabel}', + + }, + yAxis: { + type: 'value', + name: '${this.chartData.yAxisLabel}', + }, + series: [ + { + data: [], + type: 'line', + stack: 'Total', + lineStyle: { + width: ${this.chartData.lineWidth}, + type: 'solid', + }, + } + ] + } + + stackedLineChart.setOption(option); + var chartdata = { + channels:${channels}, + publishers:${things}, + names:${names}, + colors:${colors}, + valueName: '${this.chartData.valueName}', + from:${this.chartData.startTime}, + to:${this.chartData.stopTime}, + aggregation:'${this.chartData.aggregationType}', + limit:100, + interval:'${this.chartData.updateInterval}', }; + getData(stackedLineChart,chartdata); + async function getData(stackedLineChart,chartData) { + try { + let xAxisArray=[]; + const yAxisArray = []; + let currentTimestamp = chartData.from; + let previousTimestamp=0; + const endTimestamp = chartData.to; + const timeDifference = chartData.to - chartData.from; + let intervalNum; + switch (true) { + case (timeDifference <= 3.6*1e6): + intervalNum = 6*1e5; + break; + case (timeDifference <= 8.64 * 1e7): + intervalNum = 6*1e6; + break; + case (timeDifference <= 6.05 * 1e8): + intervalNum = 6*1e7; + break; + case (timeDifference <= 2.63 * 1e9): + intervalNum = 6*1e8; + break; + case (timeDifference <= 3.16 * 1e10): + intervalNum = 6*1e9; + break; + default: + intervalNum = 6*1e8; + break; + } - stackedLineChart.setOption(option);`; + const initialXAxisArray = [] + const NoOfValues = Math.ceil(timeDifference/intervalNum); + for (i=0; i<=NoOfValues; i++) { + initialXAxisArray.push(currentTimestamp); + currentTimestamp += intervalNum; + } + xAxisArray.push(initialXAxisArray); + + for (i=0; i < chartData.channels.length; i++) { + const url = "${pathPrefix}/data?channel=" + chartData.channels[i] + + "&publisher=" + chartData.publishers[i] + + "&name=" + chartData.valueName + + "&from=" + chartData.from + + "&to=" + chartData.to + + "&aggregation=" + chartData.aggregation + + "&limit=" + chartData.limit + + "&interval=" + chartData.interval; + const response = await fetch(url); + if (response.ok) { + const data = await response.json(); + const xAxis=[]; + const yAxis=[]; + if (data.messages){ + const currArray = xAxisArray[xAxisArray.length-1]; + let currentTime, previousTime; + for (j=0; j < currArray.length; j++) { + let isempty = true; + let currentTime = currArray[j]; + for (k = (data.messages.length -1); k>= 0 ; k--) { + const item = data.messages[k]; + if (item.time > previousTime && item.time <= currentTime ) { + xAxis.push(item.time); + yAxis.push(item.value); + isEmpty= false; + } + } + if (isempty) { + xAxis.push(currentTime); + yAxis.push("-"); + } + previousTime=currentTime; + } + } + xAxisArray.push(xAxis); + // Remove the previous element in the array + xAxisArray.shift(); + yAxisArray.push(yAxis); + } else { + // Handle errors + console.error("HTTP request failed with status:", response.status); + } + } + + const xAxisData=[]; + xAxisArray[xAxisArray.length-1].forEach((element) => { + const date = new Date(element); + xAxisData.push(date); + }) + const seriesData = [] + for (i=0; iData Source {{ end }}