plotly / react-plotly.js

A plotly.js React component from Plotly 📈
MIT License
1.02k stars 136 forks source link

Hook-based API #275

Open JamesRamm opened 2 years ago

JamesRamm commented 2 years ago

As discussed in #242 here is a simple hook-based API for react-plotly.

Usage

The hook gives you a ref and two streams: updates and appendData.

Here is an example:


function MyChart(props) {
   const { ref, updates, appendData } = usePlotly();

  // Here is a function that will change the data. You must pass a partial Figure object (plotly DSL object) which will be
  // merged with all previous calls to `updates`
  const changeData = () => updates({ data: [ { y: [Math.random() * 10], type: 'scatter' } ] })

 // Here we start extending traces using the `appendData` stream
 const extendData = setInterval(() => {
      appendData({ data: { y: [[Math.random() * 10]]}, tracePos: [0] });
   }, 500);

   return (
   <div> 
      <div ref={ref}  style={{ width: '500px', height: '300px' }}/> 
      <button onClick={changeData}>React!</button>
      <button onClick={extendData}>Extend!</button>
   </div>);
}

updates can be treated as a function that you can give partial Figure definitions to and it will update the graph using Plotly.react appendData can also be used as a function which is directly mapped on to Plotly.extendTraces every time it is called.

Advantages

govindthakur25 commented 2 years ago

Suggestion: It would be good to have a section in the README for this hook, with a few working codepen examples.

JamesRamm commented 2 years ago

@govindthakur25 I have added a README section.

@himbeles What is needed to get this merged? Can we go ahead?

JamesRamm commented 2 years ago

Bump Not sure what is needed to get this merged...

himbeles commented 2 years ago

@govindthakur25

I have added a README section.

@himbeles

What is needed to get this merged? Can we go ahead?

Just to clarify, I'm not a maintainer of this project.

Dyocius commented 2 years ago

@nicolaskruchten Do you know who the maintainer of this repo is?

JamesRamm commented 2 years ago

I am just going to put some names of previous committers here in the hope of getting some movement:

@alexcjohnson @nicolaskruchten @bpostlethwaite

Sorry for the spam but it seems there is no-one actively looking at this repo?

alexcjohnson commented 2 years ago

@JamesRamm apologies for the silence from us maintainers. This looks great! I'll make a few comments on the code itself, but the one extra thing I'll ask is some basic tests - either add to https://github.com/plotly/react-plotly.js/blob/master/src/__tests__/react-plotly.test.js or put a new file next to it for this purpose. We do have this repo connected to CircleCI https://app.circleci.com/pipelines/github/plotly/react-plotly.js - hopefully the config changes I just made there will enable it to (a) run on this and other forked PRs and (b) show up in the status of this PR 🤞

Michael-fore commented 1 year ago

Y'all... uhhh... gonna merge this?

@alexcjohnson

alexcjohnson commented 1 year ago

There are a number of unresolved review comments before we can merge