简体   繁体   中英

How do I integrate Plotly realtime updating graphs with React JS without rerender of component

I would like to use both React JS and plotly in my web app. I want plotly to graph realtime data, but I want to avoid a complete rerender of the graph everytime a new point is added. Plotly graphs have an update function that makes it so the entire graph isn't destroyed/rerendered.

So I have a few questions. 1. Is it possible to use react components to represent a plotly graph and have it update realtime without a full rerender of the graph? 2. If the above is not possible, is there a work around?

Thanks

You can use shouldComponentUpdate to prevent your component from re-rendering when the data you use to update the plot changes. I'm currently doing exactly this with a canvas based project so it should work with plotly as well.

Edit: My shouldComponentUpdate looks like this, basically I only need to re-render the canvas if the size it's given changes not when the data changes.

  shouldComponentUpdate(nextProps) {
    if (this.props.height !== nextProps.height ||
       this.props.width !== nextProps.width) {
      return true;
    }

    return false;
  }

Answering your question:

1 - Yes you can, Plot.ly provide a NPM lib for that, react-plotly.js, you can see more details in the docs

To work with real time update, you need to prepare your React Component to handle that, managing the React Lifecycle properly and work with Plot.ly graph update, more explained in this post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM