Hi. I am using Plotly to display a gauge on a webpage. Working great.
I would like to display some text beside the gauge which is updated when the gauge is updated.
I update my string variable (chgstatus_update) from the incoming data[1]. data[0] is used for the gauge.
How do I display this updating text field on my webpage within the Plotly code below?
Thanks!
document.addEventListener("DOMContentLoaded", function() {
// Voltage Gauge Configurations
var voltageData = [{
domain: {
x: [0, 1],
y: [0, 1]
},
value: 12.5,
title: {
text: "Voltage"
},
type: "indicator",
mode: "gauge+number",
gauge: {
bar: {
color: "darkblue",
thickness: 0.2
},
axis: {
range: [12, 15]
},
steps: [{
range: [12, 13],
color: "red"
},
{
range: [13.0, 13.1],
color: "skyblue"
},
{
range: [13.1, 13.2],
color: "lawngreen"
},
{
range: [13.2, 14.5],
color: "skyblue"
},
{
range: [14.5, 15],
color: "red"
}
]
}
}];
///////////////// Heading: "Charge Level Status" ///////////////////
///////////////// text: chgstatus_update (is a STRING) //////////////
// Layout object that set's the size of our Gauge
var layout = {
width: 600,
height: 450,
margin: {
t: 0,
b: 0
}
};
Plotly.newPlot('voltageDiv', voltageData, layout);
//////// Plotly.newPlot('dataDiv', dataData, layout); ///// add something like this
});
// Callback function that will retrieve our latest sensor readings and redraw our Gauge with the latest readings
function updatePlot() {
console.log("Updating chart");
fetch(`/updateValues`)
.then((response) => response.json())
.then(data => {
var voltage_update = {
value: data[0]
};
Plotly.update("voltageDiv", voltage_update);
var chgstatus_update = {
value: data[1]
};
/////// Plotly.update("dataDiv", chgstatus_update); /// How to display this string??
})
}
// Continuos loop that runs evry 3 seconds to update our web page with the latest sensor readings
(function loop() {
setTimeout(() => {
updatePlot()
loop();
}, 3000);
})();