Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
Plotly js live update from online data

I would like my Plotly graph to update automatically every 1 seconds by reading data from an online CSV file.

This is what I have so far:

<!DOCTYPE html>
<html>
  <head>
    <title>Test</title>
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    <script src="https://d3js.org/d3.v4.min.js"></script>
  </head>

  <body>
    <div id="graph"></div>
    <script>
      function read_data() {
        d3.csv(
          "https://docs.google.com/spreadsheets/d/e/2PACX-1vTkbRgvvBwM0tMheEziQC4ldtYoMVCgIek67Y5Lcjnu1WH0tTLLCzJPse-pL5OTR9U58Gk8VBD65L3u/pub?gid=0&single=true&output=csv",
          function (data) {
            processData(data);
          }
        );
      }

      function processData(allRows) {
        console.log(allRows);
        var x = [];
        var y = [];

        for (var i = 0; i < allRows.length; i++) {
          row = allRows[i];
          x.push(row["x"]);
          y.push(row["y"]);
        }
        console.log("Y", y);
        return y;
      }

      Plotly.newPlot(graph, [
        {
          y: [1, 2, 3],
          mode: "lines",
          line: { color: "#80CAF6" },
        },
      ]);

      var interval = setInterval(function () {
        Plotly.restyle(
          graph,
          {
            y: [[read_data()]],
          },
          [0]
        );
      }, 1000);
    </script>
  </body>
</html>

Although the y data is printed in the console, the plot is not updated.

My script is based on these two tutorials:

  • Streaming in JavaScript
  • Read CSV Data from an Ajax Call in JavaScript

Additional question: is there a way to automatically update the graph each time the data is updated in the CSV document? That is, without having to loop over each second.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your code, read_data() returns undefined. It also schedules processData() to run later, and that function returns some data, but it was called by the JavaScript runtime which ignores this returned value.

You could stick the Plotly.restyle(... code in a function that processData calls, or you could stick that code inside processData. See the code sample below.

However, there's another issue here (watch the code sample below fail). This file can't be loaded by a browser page right now. Google sheets links like https://docs.google.com/spreadsheets/d/e/2PACX-1vTkbRgvvBwM0tMheEziQC4ldtYoMVCgIek67Y5Lcjnu1WH0tTLLCzJPse-pL5OTR9U58Gk8VBD65L3u/pub?gid=0&single=true&output=csv no longer work in the browser as of about 18 months ago.

You'll need to use another method to get your data into a web page (see linked questions above for some suggestions).

<!DOCTYPE html>
<html>
  <head>
    <title>Test</title>
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    <script src="https://d3js.org/d3.v4.min.js"></script>
  </head>

  <body>
    <div id="graph"></div>
    <script>
      function read_data() {
        d3.csv(
          "https://docs.google.com/spreadsheets/d/e/2PACX-1vTkbRgvvBwM0tMheEziQC4ldtYoMVCgIek67Y5Lcjnu1WH0tTLLCzJPse-pL5OTR9U58Gk8VBD65L3u/pub?gid=0&single=true&output=csv",
          function (data) {
            processData(data);
          }
        );
      }

      function processData(allRows) {
        console.log(allRows);
        var x = [];
        var y = [];

        for (var i = 0; i < allRows.length; i++) {
          row = allRows[i];
          x.push(row["x"]);
          y.push(row["y"]);
        }
        console.log("Y", y);
        
        Plotly.restyle(
          graph,
          {
            y: y,
          },
          [0]
        );
      }

      Plotly.newPlot(graph, [
        {
          y: [1, 2, 3],
          mode: "lines",
          line: { color: "#80CAF6" },
        },
      ]);

      var interval = setInterval(read_data, 1000);
    </script>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!