Aldo
Aldo

Reputation: 303

D3 Graph with background AJAX update: cannot redraw graph

I'm really new to D3, so I'm trying to learn by modifying some example. Using this as a starting point, (here's the working Pen) I' trying move the needle by fetching data with AJAX in background.

I have tried using this code:

 $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: 'randomdata.php',
            dataType: 'json',
            async: false,
            data: "{}", 
            success: function (data) {
               var pos_data = data;
               needle.moveTo(percent);

            },
            error: function (result) {



}
    })

(with randomdata.php just providing random values between 0 and 100)

..but I don't get any needle movement, even by just calling the needle.moveTo in a setTimeout. Again, I'm a total noob here.. and I'm surely missing something. Can I ask for help please? Thanks!

Upvotes: 0

Views: 43

Answers (1)

Matt Sergej Rinc
Matt Sergej Rinc

Reputation: 565

Add a

console.log(percent + ", " + typeof(percent));

to your success function for determining the percent variable value and type of variable.

Your code has a problem there since in your codePen example commands like

needle.moveTo(0.7);
needle.moveTo("0.7");

work. So I guess a function call doesn't have a valid percentage value (from 0 to 1, not 0 to 100) or proper format of it (a dot, not a comma etc).

If you have a value between 0 to 100 then just change the call to

needle.moveTo((percent/100).toFixed(2));

Upvotes: 1

Related Questions