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

145
Vistas
How can I add new data points to an existing chart with chart.js?

I'm using the code below to generate a random graph using chart.js. I'm trying to use a function called nextDay to add new points, but it's not working. nextDay is attached to a button and the goal is every time the button is pressed a new point will be added. Any help would be appreciated.

<script>
            const labels = [
  ];

  const data = {
    labels: labels,
    datasets: [{
      label: 'graph',
      backgroundColor: 'rgb(255, 99, 132)',
      borderColor: 'rgb(255, 99, 132)',
      data: [],
    }]
  };

  const config = {
    type: 'line',
    data: data,
    options: {}
  };
</script>

<script>
function draw(){
const myChart = new Chart(
    document.getElementById('myChart'),
    config
  );
}

        var balance = 0;

        function nextDay(){
                randNum = Math.random() * (max - min) + min;
                data.datasets[0].data.push(randNum);
                labels.push('Week ' + (startingCount + 1));
                myChart.update();
        }

        function sell(){

            balance = balance + randNum;
            console.log("current balance:" + balance);
            randNum = 0;
        }

        function getStartingValues(){
        var max = 100;
        var min = -100;
            for (var startingCount = 0; startingCount < 15; startingCount++)
            {
            var randNum = (Math.random() * (max - min) + min);
            max = randNum + 100;
            min = randNum - 100;
            data.datasets[0].data.push(randNum);
            labels.push('Week ' + (startingCount + 1));
            }
        }
        getStartingValues();
</script>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are updating your original variables, this does not work. You have to update chart.js its internal data like so:

function nextDay() {
  randNum = Math.random() * (max - min) + min;
  myChart.data.datasets[0].data.push(randNum);
  myChart.data.labels.push('Week ' + (startingCount + 1));
  myChart.update();
}
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