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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:04:42+00:00 2026-05-12T22:04:42+00:00

I have a continuously generated data (text file) generated by a program on the

  • 0

I have a continuously generated data (text file) generated by a program on the server. I want to plot the data as a real-time graph just like powergrid does. This was my approach:

As the data is generated continuously on the server in a text file, I wrote a PHP script which reads that file(get_file_contents), outputs the data points and plot the graph using sparkline jQuery plugin. But the problem is that it reads the file all at once. Moreover, the text file keeps on growing. Can anyone suggest me a better approach?

  • 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-12T22:04:42+00:00Added an answer on May 12, 2026 at 10:04 pm

    As you’re talking about using a Javascript plotting solution you do the following:

    • on page load you create the current graph by reading the complete text file and remembering it’s size.
    • after the page is loaded you create a Javascript function that regularly polls a specific script on your server using AJAX-techniques (XMLHttpRequest) and passing the last-known filesize of your text file as a parameter.
    • your polling script takes the filesize parameter, opens the text file, skips through the file until it reaches the point from which you last read the file (filesize-parameter).
    • the polling script returns all the available data from filesize to the end of the file and the new filesite
    • your Javascript reads in the AJAX response and adds the required plot points to your graph
    • you can then start over polling your server-side script with the new filesize as a parameter

    This procedure involves server-side as well as client-side programming but can be accomplished easily.

    The following is a sample polling script that requires a index paramater that tells the script from which position to read the text file and returns a JSON-encoded list of plot points and the new index pointer.

    // poll.php
    $index = (isset($_GET['index'])) ? (int)$_GET['index'] : 0;
    $file = fopen('path/to/your/file.txt', 'r');
    $data = array(
        'index' => null,
        'data'  => array()
    );
    // move forward to the designated position
    fseek($file, $index, SEEK_SET);
    while (!feof($file)) {
        /*
         * assuming we have a file that looks like
         * 0,10
         * 1,15
         * 2,12
         * ...
         */
        list($x, $y) = explode(',', trim(fgets($handle)), 2);
        $data['data'][] = array('x' => $x, 'y' => $y);
    }
    // set the new index
    $data['index'] = ftell($file);
    fclose($file);
    
    header('Content-Type: application/json');
    echo json_encode($data);
    exit();
    

    The corresponding Javascript/jQuery snippet could be:

    // the jQuery code to poll the script
    var current = 0;
    function pollData() {
        $.getJSON('poll.php', { 'index': current }, function(data) {
            current = data.index;
            for (var i= 0; i < data.data.length; i++) {
                var x = data.data[i].x;
                var y = data.data[i].y;
                // do your plotting here
            }
        });
    }
    // call pollData() every 5 seconds
    var timer = window.setInterval(pollData, 5000);
    

    Please be careful that this is only an example and that it lacks all error checking (e.g. concurrent calls to pollData() on the same page will be problematic).

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

Sidebar

Ask A Question

Stats

  • Questions 227k
  • Answers 227k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You might need to create your own task for this.… May 13, 2026 at 1:24 am
  • Editorial Team
    Editorial Team added an answer Warning! Microsoft strongly recommends that you don't mess with their… May 13, 2026 at 1:24 am
  • Editorial Team
    Editorial Team added an answer It's usually an html div that is popped up through… May 13, 2026 at 1:24 am

Related Questions

For the past few years I've continuously struggled with unit testing database code and
I have a data feed continuously feeding data packet in. There are 5 threads(A,
I found the source of the problem #2. It is the use of session_register(foo)
I've a C# program generating JPEG images in realtime, i need to (continuously) generate
I have joined a rails project as a contractor. The project has been going

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.