playground javascript chart add min, max , avg to lables

This commit is contained in:
DeltaLima 2025-04-13 23:04:48 +02:00
parent 9e9d14b971
commit 9c410772d5

View file

@ -305,7 +305,7 @@ function DrawChart() {
const datapointsSum = [];
var datapointsMax = 0;
var dataMin, dataMax, dataAvg = 0;
/* get highest number of datapoints */
for(let i = 0; i < chartSum; i++) {
if(ChartJson.chart[i].datapoints > datapointsMax) {
@ -313,9 +313,31 @@ function DrawChart() {
}
/* we are already interating through available charts, so we draw at this point the legend as well */
var ul = document.getElementById("chartLegend");
var li = document.createElement("li");
li.appendChild(document.createTextNode(ChartJson.chart[i].descr + ' (' + ChartJson.chart[i].name + ')'));
/* get dataMax and dataMin */
for(let j = 0; j < datapointsMax; j++) {
if((ChartJson.chart[i].hasOwnProperty('data')) && ((typeof ChartJson.chart[i].data[j] != undefined) && ((dataMin > ChartJson.chart[i].data[j]) || (j == 0)))) {
dataMin = ChartJson.chart[i].data[j];
}
if((ChartJson.chart[i].hasOwnProperty('data')) && ((typeof ChartJson.chart[i].data[j] != undefined) && ((dataMax < ChartJson.chart[i].data[j]) || (j == 0)))) {
dataMax = ChartJson.chart[i].data[j];
}
/* add to Average */
if((ChartJson.chart[i].hasOwnProperty('data')) && (typeof ChartJson.chart[i].data[j] != undefined) && (j < ChartJson.chart[i].datapoints)) {
dataAvg += ChartJson.chart[i].data[j];
console.log('Chart: ' + i + ' dataP ' + j + ' dataAvg: ' + ChartJson.chart[i].data[j]);
}
//console.log('Chart: ' + i + ' dataP ' + j + ' dataAvg: ' + ChartJson.chart[i].data[j]);
}
/* calculate average */
dataAvg = dataAvg / ChartJson.chart[i].datapoints;
var ul = document.getElementById('chartLegend');
var li = document.createElement('li');
li.appendChild(document.createTextNode('\u2B07 ' + dataMin + ' \u2B06 ' + dataMax + ' \u27A1 ' + dataAvg.toFixed(2) + ' - ' + ChartJson.chart[i].descr + ' (' + ChartJson.chart[i].name + ')'));
ul.appendChild(li);
}
@ -371,7 +393,7 @@ function DrawChart() {
cssString = '--start: calc( ' + MapNum(previousData[j], ChartJson.chart[j].min, ChartJson.chart[j].max) + ' / 100); --end: calc( ' + MapNum(ChartJson.chart[j].data[dataIndex], ChartJson.chart[j].min, ChartJson.chart[j].max) + ' / 100);';
}
cell[j].style.cssText = cssString;
console.log('normal: ' + ChartJson.chart[j].data[dataIndex] + ' map: ' + MapNum(ChartJson.chart[j].data[dataIndex], ChartJson.chart[j].min, ChartJson.chart[j].max));
//console.log('normal: ' + ChartJson.chart[j].data[dataIndex] + ' map: ' + MapNum(ChartJson.chart[j].data[dataIndex], ChartJson.chart[j].min, ChartJson.chart[j].max));
/* store datapoint as previous data */
previousData[j] = ChartJson.chart[j].data[dataIndex];
} else {