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

97
Vistas
Add data to chart properly

Hey folks I have a task to show user data on the chart (I'm using react-native slide charts) but it becomes a little bit tricky. So what I'm trying to achieve is next: I'm making a request and get the next data from the server:

{
  "trend": [92, 93, 94, 94, 93, 93, 94, 95, 95, 95, 95, 94, 96, 95, 96],
  "history": [1087, 112, 117, 190, 153, 163, 138, 166, 191, 191, 187, 163, 187, 177, 109, 107, 196, 175, 113, 192, 113, 112, 169, 109, 109, 157, 126, 170, 137, 146, 132, 114],
}

And I know that trend is data for the last 15 minutes with an interval of 1 min, and the history is data for the last 8 hours with an interval of 15 mins.

I need to map this data and show it on the chart. I'm trying to do the next:

const initialDateTime = dayjs();

const mappedTrend = data.trend.reverse().map((el, i) => ({
      x: i === 0 ? initialDateTime.valueOf() : initialDateTime.add(i * 1, 'minutes').valueOf(),
      y: el,
}));

const mappedHistory = data.history.reverse().map((el, i) => ({
      x: i === 0 ? initialDateTime.valueOf() : initialDateTime.add(i * 15, 'minutes').valueOf(),
      y: el,
}));

in output I have 2 arrays like {x: timestamp, y: value};

Then I'm adding it to the state and the chart looks at this data:

setState({data: [...mappedHistory, ...mapTrend]})

seems like it is ok till here, but now, for example, I need to load more data, after for example 5 minutes I can make another request and will get almost the same data but with different numbers, and I need now, to prolong the chart data with a new one, but how to do it? How to add data properly?

I prepared a snack here Problems are, the chart is looking quirky and when new data comes it also shows strange. And the thing is, on chart data should be in range on 24h that's why I use xRange on the chart. So any ideas on how to solve this or fix it?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you are just wanting to combine the data currently being kept in your state, you can spread the existing array with the incoming array;

updateArray = (mappedHistory, mapTrend) => {
   const newArray =  [...mappedHistory, ...mapTrend];
   this.setState({ data: [...this.state.data, ...newArray] });
}
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