Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
How to use Real time chart in react?

I'm currently building a realtime chart to test it out. I wrote something to receive data as an example, but it doesn't work. Below is my code.

import { StreamingPlugin, ChartStreaming } from 'chartjs-plugin-streaming'
import {  
  Chart as ChartJS,
  LinearScale,
  CategoryScale,
  RadialLinearScale,
  BarElement,
  PointElement,
  LineElement,
  ArcElement,
  Legend,
  Tooltip
} from 'chart.js'
import {
  Chart,
  Line,
  Pie,
  Doughnut,
  Radar,
  getDatasetAtEvent,
  getElementAtEvent,
  getElementsAtEvent,
} from 'react-chartjs-2'
ChartJS.register(
  LinearScale,
  RadialLinearScale,
  CategoryScale,
  BarElement,
  PointElement,
  LineElement,
  ArcElement,
  StreamingPlugin, 
  Legend,
  Tooltip
)

const data =
{
  datasets :
  [
    {
      label : "real",
      backgroundColor : "rgb(255, 99, 132)",
      borderColor: "rgb(255, 99, 132)",
      fill : false,
      lineTension : 0,
      borderDash: [8,4],
      data : [],
      showLine: true,
    }
  ]
}
console.log(data.datasets[0].data)
const RealTime_ChartTest_options = 
{
  elements : 
  {
    line :
    {
      tesion: 0.5
    }
  },
  scales :
  {
    xAxes: [
      {
        type: "realtime",
        realtime: {
          onRefresh: function () {
            data.datasets[0].data.push({
              x: Date.now(),
              y: Math.random() * 100
            }
            );
          },
          delay: 300,
          refresh: 300
        }
      }
    ],
    yAxes: [
      {
        scaleLabel: {
          display: true,
          fontFamily: "Arial",
          labelString: "Moment",
          fontSize: 20,
          fontColor: "#6c757d"
        },
        ticks: {
          max: 100,
          min: 0
        }
      }
    ]
  }
}

const App = ()=>
{
  retrun(
   <Line data={data} options={RealTime_ChartTest_options} />
  )
}

As a result, the Y-axis and labels are displayed correctly, but the values are not stored in the data in real time and the graph is not drawn.

What am I doing wrong in the code?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is because your scale config is wrong, you are using V2 syntax while using V3, changing it to this should resolve the issue:

scales: {
  x: {
    type: "realtime",
    realtime: {
      onRefresh: function() {
        data.datasets[0].data.push({
          x: Date.now(),
          y: Math.random() * 100
        });
      },
      delay: 300,
      refresh: 300
    }
  },
  y: {
    max: 100,
    min: 0
  }
}

For all changes please read the migration guide

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda