0

I want to take data from JSON requests with ajax and plot data according to specific fields. When I request link:

http://127.0.0.1:8000/sensor/print/

I get a response as JSON:

{"temp0": "10", "temp1": "30", "voltage": "10", "current": "100"}

Then i create a js:

$(document).ready(function(){
    function secret(){
    $.ajax({
              type: "GET",
              url: 'http://127.0.0.1:8000/sensor/print/',
              data: {get_param: 'temp0'},
              dataType: 'json',
              success: function (data) {
                return Number(data.temp0);
              }
            });

    }


    Plotly.newPlot('simple',[{
        y: [1,2,3].map(secret),
        mode: 'lines',
        line: {color: '#80CAF6'}
    }]);
        var interval = setInterval(function() {
                  Plotly.extendTraces('simple', {
                    y: [[secret()]]
                  }, [0])

                  if(++cnt === 100) clearInterval(interval);
                }, 300);
        });

and as result, I have a moving plot without any print or my values. How can I get my values from the JSON response and plot a graph with it?

CHAHI Saad
  • 309
  • 4
  • 15
  • The function `secret` doesn't contain a return statement. It implicitly returns `undefined`. Move the call to `Plotly.newPlot` into the `success` callback and use `data.temp0` for the plot. – jabaa Mar 27 '22 at 21:13
  • but i need to plot real-time graph, so i need to call ajax every interval of time, if i would call the function into the success callback, i can't do it. I need a mant ajax requests. Maybe I'm wrong, could you write how it will look like, please. I dummy in js) – Vladimir Kosilov Mar 28 '22 at 06:50
  • Why can't you sent the request periodically, while you call the plot function in the success callback? What is the exact problem? – jabaa Mar 28 '22 at 07:33

0 Answers0