Plotly and Websockets

≡放荡痞女 提交于 2021-02-10 14:40:54

问题


Currently I am trying to use Plotly to make a live pie chart with data coming in from a websocket connection. However, I cant seem to get the data from the socket to the graph. Right now I see two ways of doing it. One is to get the data out of the onmessage function, such as returning a value to the parent function. The other is putting the Plotly code into the onmessage function. Neither are currently working. I'm not planning on talking about getting data out of the onmessage function in this thread since this one is for Plotly not websocket. I will link a stack overflow thread where I asked about the websocket problem if your interested in that part.

JavaScript: Getting Values out of Websocket Functions

So, Onto the problems I’m running into with putting Plotly inside the onmessage function. For some reason, when I cut and paste the code to create the graph into that function it “freezes” the program. The script will run as normal till it gets to the point where it makes the graph, you can see it make the graph, then it just sits there. The entire code stops as shown by the fact that it should loop back to the start of the function and update live text on the page and this stream stops. My code is given below if you want to take a look at it.

var pmsg = "null";
var ws = new WebSocket('ws://localhost:10011/');
ws.onmessage = function(event)
{
     if(pmsg == 'QC1R1TR')
     {
          QC1R1TR = event.data;
          var testing = document.getElementById('QC1R1TR');
          testing.innerHTML = "Run Time: " + QC1R1TR;
     }
 
     pmsg = event.data;

     var data = [{
     values: [2, 1],
     labels: ['TR', 'TNR'],
     type: 'pie',
     automargin: true
     }];

     var layout = {
     height: 300,
     width: 300,
     margin: {"t": 0, "b": 0, "l": 0, "r": 0},
     //showlegend: false
     };

     Plotly.newPlot('QC1R1Pie', data, layout);
}

Please note that when the plot is static numbers currently so its not an issue with the variables. The plot works when its in the parent function and so does the live text on the page.

Any help with this would be greatly appreciated!

Thanks, Luke

来源:https://stackoverflow.com/questions/66104834/plotly-and-websockets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!