Skip to content

How to properly use extendTraces #85

Closed
@gavargas22

Description

@gavargas22

I have been trying to use extendTraces with this library but I keep getting an error: Uncaught Error: indices must be valid indices for gd.data.

I have been able to get it working with a regular HTML page but I am having trouble with the ReactJS library

Activity

nicolaskruchten

nicolaskruchten commented on Jun 19, 2018

@nicolaskruchten
Contributor

It's not really possible to use extendTraces with this library, which is react-plotly.js, the ReactJS binding for plotly.js. With this library you just specify the full data/layout and the plot is efficiently redrawn to the new data. If you wish to use extendTraces you will not be able to use react-plotly.js.

gavargas22

gavargas22 commented on Jun 19, 2018

@gavargas22
Author

I see, thank you for your help!

minaee

minaee commented on Mar 6, 2021

@minaee

I have the same error:

var interval = setInterval(function() {
    $.ajax({
        url : 'ajax/updatevalues',
        dataType : 'json',
        type : 'GET',
        success: function(data)
        {   
            var dateStr = JSON.parse(data.temperature.last_timestamp)
            // console.log('dateStr:', dateStr); // 2014-01-01T23:28:56.782Z
            temp_time = new Date(dateStr);
            // console.log('temp_time:', temp_time); // 2014-01-01T23:28:56.782Z
            temp_temperature = data.temperature.last;
            temp_refined_fuels = data.refined_fuels.last;
            temp_crude_oils = data.crude_oils.last;

            index.push(temp_time);
            temperature.push(data.temperature.last);
            refined_fuels.push(data.refined_fuels.last);
            crude_oils.push(data.crude_oils.last);
        }
    });

    
    xx = [];
    yy = [];
    

    temperature_list = [];
    refined_fuels_list = [];
    crude_oils_list = [];

    $('input[type=checkbox]').each(function () {

        if(this.checked == true){
            if(this.name == 'Temperature'){
                tt = [];
                tt.push(temp_time);
                xx.push(tt);
                temperature_list.push(temp_temperature);
                yy.push(temperature_list);
            }else if(this.name == 'Refined_Fuels'){
                tt = [];
                tt.push(temp_time);
                xx.push(tt);
                refined_fuels_list.push(temp_refined_fuels);
                yy.push(refined_fuels_list);
            }else if(this.name == 'Crude_Oils'){
                tt = [];
                tt.push(temp_time);
                xx.push(tt);
                crude_oils_list.push(temp_crude_oils);
                yy.push(crude_oils_list);
            }
        }        
    });

    console.log('xx: ', xx);
    console.log('yy: ', yy);
    console.log('get_number_of_traces(): ', get_number_of_traces());

    Plotly.extendTraces(testGraph, {
        x: xx ,
        y: yy 
        }, get_number_of_traces() );
    cnt++;

if(++cnt === 100) clearInterval(interval);
}, 2000);

the console lines print :

xx: (2) [Array(1), Array(1)]
(y: (2) [Array(1), Array(1)]
(active_parameters: (2) ["Refined_Fuels", "Crude_Oils"]
(get_number_of_traces(): (2) [0, 1]

console log of error:

Uncaught Error: indices must be valid indices for gd.data.
at z (plotly-latest.min.js:formatted:115679)
at plotly-latest.min.js:formatted:115705
at D (plotly-latest.min.js:formatted:115712)
at Object.t [as extendTraces] (plotly-latest.min.js:formatted:116859)
at (index):1432

I want to show multiple data on the same chart. the user may select or deselect the checkboxes to add or delete traces. the data are coming from AJAX. all the received data are tested and are valid.
any suggestions?

mthielvoldt

mthielvoldt commented on Nov 15, 2024

@mthielvoldt

I had the same needs, and seem to have "it working" ™ (meaning my plot does update with the new data) with the following sample.

import { useEffect, useRef } from 'react';
import Plot from 'react-plotly.js';
import Plotly from 'plotly.js-dist';

export default function LineGraph() {

  const plotRef = useRef(null);
  const intervalRef = useRef(null);

  useEffect(() => {
    let count = 0;
    const samplesPer = 10;
    
    // Update the graph every 200ms with new data
    intervalRef.current = setInterval(() => {
      // Extend traces with the new data points
      const newData = Array.from({length: samplesPer}, () => Math.random());
      count += samplesPer;
      Plotly.extendTraces(
        plotRef.current.el, 
        { y: [newData] },
        [0]);

      // Start sliding the x axis once we reach >200 samples.
      if (count > 200) {
        Plotly.relayout(plotRef.current.el, {xaxis: {range: [count-200, count]}});
      }
    }, 250);

    return () => clearInterval(intervalRef.current); // Cleanup on component unmount
  }, []);

  return (
    <Plot
      ref={plotRef}
      data={[{ y: [], type: 'line' }]}
      layout={{ width: 500, height: 300, title: { text: 'Scrolling Plot' } }}
      useResizeHandler={true}
      style={{ width: '100%', height: '100%' }}
    />
  );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @nicolaskruchten@gavargas22@mthielvoldt@minaee

        Issue actions

          How to properly use extendTraces · Issue #85 · plotly/react-plotly.js