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

110
Views
Representación de un gráfico en React con datos de objetos asíncronos

Estoy intentando hacer un gráfico con datos que se obtienen de una API, pero un problema que tengo es que no he podido representar correctamente el gráfico debido a una llamada asíncrona que estoy haciendo para obtener los datos. Creo que el gráfico se está ejecutando antes de que se puedan completar los datos porque al cargar la ventana no aparece nada. Sin embargo, cuando probé valores de codificación duros en lugar de 'marcas de tiempo' y 'precios' en la variable de datos, el gráfico se representa de inmediato. ¿Alguien sabe cómo puedo formatear el resto del código para que el gráfico se muestre solo después de que se hayan llenado las matrices de marca de tiempo y precio?

 import { useEffect } from 'react'; import { Line } from 'react-chartjs-2'; const MarketAPI = require('market-api'); const Client = new MarketAPI(); function GRAPH(){ const timestamps = []; const prices = []; var getData = async() => { const fromTS = (Date.now() / 1000 | 0 ) - 86400; // 1D from current timestamp const toTS = Date.now() / 1000 | 0; // current timestamp let get = await Client.assets.fetchAssetHist('MSFT', { from: fromTS, to: toTS, }); for(let i = 0; i < get.data.prices.length; i++){ timestamps.push(get.data.prices[i].[0]); prices.push(get.data.prices[i].[1]); } console.log(timestamps); console.log(prices); } const data = { labels: timestamps, datasets: [ { data: prices, fill: true, backgroundColor: 'rgba(243, 230, 200, 0.2)', borderColor: 'rgba(243, 210, 18)', }, ], }; const options = { responsive: true, maintainAspectRatio: false, plugins: { legend: { display: false } }, scales: { x: { grid: { display: false }, display:true }, y: { grid: { display: false }, display: false } }, elements: { point:{ radius: 0 } }, }; useEffect(()=>{ getData() },[]); return( <div> <Line data={data} options={options}/> </div> ); } function App() { return ( <GRAPH/> ); }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En su lugar, utilice useState Hook para almacenar sus datos.

 import { useEffect, useState } from 'react'; import { Line } from 'react-chartjs-2'; const MarketAPI = require('market-api'); const Client = new MarketAPI(); function GRAPH(){ const timestamps = []; const prices = []; const [data, setData] = useState({}) var getData = async() => { const fromTS = (Date.now() / 1000 | 0 ) - 86400; // 1D from current timestamp const toTS = Date.now() / 1000 | 0; // current timestamp let get = await Client.assets.fetchAssetHist('MSFT', { from: fromTS, to: toTS, }); for(let i = 0; i < get.data.prices.length; i++){ timestamps.push(get.data.prices[i].[0]); prices.push(get.data.prices[i].[1]); } console.log(timestamps); console.log(prices); } setData({ labels: timestamps, datasets: [ { data: prices, fill: true, backgroundColor: 'rgba(243, 230, 200, 0.2)', borderColor: 'rgba(243, 210, 18)', }, ], }) const options = { responsive: true, maintainAspectRatio: false, plugins: { legend: { display: false } }, scales: { x: { grid: { display: false }, display:true }, y: { grid: { display: false }, display: false } }, elements: { point:{ radius: 0 } }, }; useEffect(()=>{ getData() },[]); return( <div> <Line data={data} options={options}/> </div> ); } function App() { return ( <GRAPH/> ); }
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!