Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

86
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda