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 6964901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:59:01+00:00 2026-05-27T15:59:01+00:00

This is a sample dynamically updated chart: http://www.highcharts.com/demo/dynamic-update The chart is updated every second

  • 0

This is a sample dynamically updated chart:
http://www.highcharts.com/demo/dynamic-update

The chart is updated every second with the the date as the x value and a random number as the y value.

load: function() {            
    // set up the updating of the chart each second
    var series = this.series[0];
    setInterval(function() {
        var x = (new Date()).getTime(), // current time
            y = Math.random();
        series.addPoint([x, y], true, true);
    }, 1000);
}

How would I rewrite load to fetch x and y from another webpage using AJAX, rather than generating the values within the function?

  • 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-27T15:59:02+00:00Added an answer on May 27, 2026 at 3:59 pm

    I take it what you want is the sample load method but with the lines that set x and y replaced with an AJAX call. You’ll need to perform a fairly basic continuation-passing code transformation, turning the code after the point you want the asynchronous call into a function that’s passed to the asynchronous call. “Continuation” simply means “the rest of the calculation, from a given point forward”. In the code sample, that’s the call to series.addPoint. The pattern for this is you transform:

    function f(...) {
        (1)
        result = sync(...);
        (2)
    }
    

    into:

    function f(...) {
        (1)
        async(..., function (result) {
              (2)
          });
    }
    

    If (2) returned a value in the original function, then the call to f must also be rewritten using continuation passing style, adding an extra parameter to hold the continuation.

    The other thing you should do is make sure the PHP script outputs the numeric pair as valid JSON, either as an array of two numbers (which can be passed directly to the series.addPoint call after parsing), or as an object with properties “x” and “y”.

    Note that, due to the nature of network communication, the data may not arrive in a timely manner, resulting in a graph with irregular intervals.

    Here’s the gist, wrapping up the nuts-and-bolts of the asynchronous call into a function named ajaj. The sample assumes the browser supports the necessary standards for XMLHttpRequest and the JSON parser.

    /* Asynchronous Javascript And Json */
    function ajaj(url, succeed, fail) {
        if (! fail) {
            fail = function() {};
        }
        var xhr = new XMLHttpRequest;
        xhr.open('GET', url);
        xhr.onreadystatechange = function() {
            if (xhr.readyState==4) {
                if (200 <= xhr.status && xhr.status < 300) {
                    succeed(JSON.parse(xhr.responseText));
                } else {
                    // error
                    fail(xhr.status, xhr.statusText, xhr.responseText);
                }
            }
        };
        xhr.send();
        return xhr;
    }
    
    ...
    
        url: '...',
    
        load: function() {
            // ensure only one data load interval is running for this object
            if (this.updateInterval) {
                return;
            }
            // set up the updating of the chart each second
            var series = this.series[0];
    
            this.updateInterval = setInterval(function() {
                ajaj(this.url, 
                     function(point) { // success
                         series.addPoint(point, true, true);
                     },
                     function(status, statusText, response) { // failure
                         ...
                     }
                );
            }, 1000);
        }
    

    JS libraries provide their own version of ajaj, often with more features. If you’re doing anything of any complexity for a production site, look into adopting one. If you’re using jQuery (as the tag suggests), you can, for example, use jQuery.get:

        load: function() {
            if (this.updateInterval) {
                return;
            }
            // set up the updating of the chart each second
            var series = this.series[0];
    
            this.updateInterval = setInterval(function() {
                jQuery.get(this.url, 
                     function(point, textStatus, jqXHR) { // success
                         series.addPoint(point, true, true);
                     }, 'json'
                );
            }, 1000);
        }
    

    The server side of things is dead simple. time() returns a Unix timestamp, rand() returns a (not very) pseudorandom number (though good enough for a demo), and json_encode(), well, you can figure that out.

    <?php
    header('Content-type: application/json');
    
    echo json_encode(
            array(
                time(),
                rand() / getrandmax(),
            ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine this sample java class: class A { void addListener(Listener obj); void removeListener(Listener obj);
Consider this sample code: <div class=containter id=ControlGroupDiv> <input onbeforeupdate=alert('bingo 0'); return false; onclick=alert('click 0');return
Consider this sample class, class TargetClass { private static String SENSITIVE_DATA = sw0rdfish; private
In this sample code the URL of the app seems to be determined by
in this sample method/message: -(void) setNumerator:(int) n { numerator = n; } What does
Take this sample class as an example: [AttributeUsage(AttributeTargets.All, AllowMultiple=true)] public class BugFixAttribute : System.Attribute
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
I have this sample text, which is retrieved from the class name on an
I've written this sample code to print the length of x. But for some
I have this sample code for async operations (copied from the interwebs) public class

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.