Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7603269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:37:08+00:00 2026-05-30T23:37:08+00:00

Can anyone help with how to use a jQuery get (json, txt, xml, whatever)

  • 0

Can anyone help with how to use a jQuery get(json, txt, xml, whatever) and setInterval (d3.js) so that I can update my d3 bar chart every N seconds?
Or does anyone know of an example out there that uses a RESTful get to update data through setInterval in d3 SVGs?
I’ve read this tutorial all day, but not clicking with how to incorporate json instead of random walk.
Many Thanks in advance….

My Unsuccessful Attempt:

        <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8">
        <title> Testing a d3.js Walking Bar Chart via jQuery getText and d3.js setInterval </title>
        <script type="text/javascript" src="http://localhost:8080/dev_tests/d3/d3.js"></script>
        <script type="text/javascript" src="http://localhost:8080/dev_tests/latest.jquery/jquery-latest.js"></script>
      </head>

      <body>
        <div class="body">
          <div class="content">          
            <style type='text/css'>

            .chart {
              margin-left: 42px;
            }

            .chart rect {
              fill: steelblue;
              stroke: white;
            }

            </style>

                <script type='text/javascript'>


                var t = 1297110663,
                    v = 70,
                    data = d3.range(33).map(next1);

                    function next1() {
                    $.get('http://localhost:8080/dev_tests/data/file.txt', function(data1) { 
                        $('.result').text(data1); 
                        alert(data1);
                        return {time: ++t, value: v = data1 };
                            });
                    }
                        var w = 20,
                            h = 80;

                        var x = d3.scale.linear()
                            .domain([0, 1])
                            .range([0, w]);

                        var y = d3.scale.linear()
                            .domain([0, 100])
                            .rangeRound([0, h]);

                    </script>

                    <p>Et voila!</p>
                    <script type='text/javascript'>

                    var chart3 = d3.select(".content").append("svg")
                        .attr("class", "chart")
                        .attr("width", w * data.length - 1)
                        .attr("height", h);

                    chart3.append("line")
                        .attr("x1", 0)
                        .attr("x2", w * data.length)
                        .attr("y1", h - .5)
                        .attr("y2", h - .5)
                        .style("stroke", "#000");

                    redraw3();

                        function redraw3() {

                          var rect = chart3.selectAll("rect")
                              .data(data, function(d) { return d.time; });

                          rect.enter().insert("rect", "line")
                              .attr("x", function(d, i) { return x(i + 1) - .5; })
                              .attr("y", function(d) { return h - y(d.value) - .5; })
                              .attr("width", w)
                              .attr("height", function(d) { return y(d.value); })
                            .transition()
                              .duration(1000)
                              .attr("x", function(d, i) { return x(i) - .5; });

                          rect.transition()
                              .duration(1000)
                              .attr("x", function(d, i) { return x(i) - .5; });

                          rect.exit().transition()
                              .duration(1000)
                              .attr("x", function(d, i) { return x(i - 1) - .5; })
                              .remove();

                        }

                        setInterval(function() {
                          data.shift();
                          data.push(data);
                          redraw3();
                        }, 3500);
                        </script>
                </div>
        </div>
      </body>
    </html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:37:09+00:00Added an answer on May 30, 2026 at 11:37 pm

    jQuery’s ajax functions are asynchronous, so next1() isn’t returning anything. You want to:

    1. Create your data array
    2. Load new data asynchronously
    3. Upon loading data, push() it onto the data array
    4. Redraw your graph

    You can simulate this without a JSON call by doing this:

    var data = [],
        t = 0;
    var interval = setInterval(function() {
        var value = Math.random() * 100;
        data.push({time: ++t, value: value});
        redraw();
    }, 1000);
    

    And when you get that working, switch to this:

    var data = [],
        t = 0;
    var interval = setInterval(function() {
        $.getJSON("path/to/data.json", function(datum) {
            data.push({time: ++t, value: datum});
            redraw();
        });
    }, 1000);
    

    One problem with this, though, is that if any of the JSON requests take longer than a second you could end up with data loading out of order. So it’s best to use setTimeout() and queue up the next load only after you’re done with the previous one:

    var data = [],
        t = 0,
        timeout;
    function loadNext() {
        $.getJSON("path/to/data.json", function(datum) {
            data.push({time: ++t, value: datum});
            redraw();
            timeout = setTimeout(loadNext, 1000);
        });
    }
    loadNext();
    

    Make sense?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help with a jQuery snippet that would use Ajax to pull an
Can anyone help? I normally use server controls i.e Textbox so i can get
Can anyone help me to rewrite the below code which use a fade effect
Can anyone help? I have an issue with calling a asp.net webservice from jquery..
I wonder if anyone can help with a jQuery problem I am having. I
I have been struggling to get this right! Can anyone help me to convert
is there anyone have tried to use highcharts jquery with stacked bar like this
Can anyone help me with the trying to write SQL (MS SqlServer) - I
Can anyone help with with the time complexity of this algorithm, and why it
Can anyone help? I have been designing a site using Javascript but the rest

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.