Hi,
I have 2 inputs that show streamed data (single numbers) from 2 sensors. With Onclick
event, I want to show the data in a graph: the function plot( )
is based on the plotly example here. When the page is refreshed, and I click on the <input>
of sensor1 for example, the graph shows the sensor1 data as expected. However, if I then click on the of sensor2, the graph shows data from the 2 sensors alternatively, instead of showing the plot from the sensor2 alone. I tried placing Plotly.purge('graph')
at the beginning of the function plot( )
, but it does not make a difference. I 'd really appreciate some suggestions. Thanks. Below is the code:
<script>
function plot(elt){
Plotly.purge("graph")
var graph = document.getElementById("graph");
var x_source = document.getElementsByName('t')[0];
var x_init = Number(x_source.value) //initial value of the timestamp
var data = [{
x: [0],
y: [Number(elt.value)],
mode: 'lines',
line: {color: '#80CAF6'},
}]
Plotly.plot('graph', data);
var cnt = 0;
var interval = setInterval(function() {
var update = {
x: [[(Number(x_source.value)-x_init)/1000]],
y: [[Number(elt.value)]]
}
Plotly.extendTraces('graph', update, [0])
if(cnt === 2000) clearInterval(interval);
}, 100);
}
</script>
<body>
<div id="graph"></div>
<ul id="sensors">
<li> <input class='value_telemetry' name="sensor1" onclick="plot(this)" value = "0"></li>
<li><input class='value_telemetry' name="sensor2" onclick="plot(this)" value = "0"></li>
</ul>
</body>