Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Hook-based API #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JamesRamm wants to merge 5 commits into plotly:master
base: master
Choose a base branch
Loading
from JamesRamm:patch-1
Open

Hook-based API #275

JamesRamm wants to merge 5 commits into plotly:master from JamesRamm:patch-1

Conversation

Copy link

@JamesRamm JamesRamm commented Feb 9, 2022
edited
Loading

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

neka-nat, govind-ootb, timsntech, rburchDev, chris-lear, Yohannfra, govindthakur25, vinhtru, nfelix25, renegoretzka, and 8 more reacted with thumbs up emoji alxpez reacted with hooray emoji
Copy link

@himbeles himbeles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor fix regarding requirement versions

Copy link

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

Adds example of `usePlotly`
Copy link
Author

@govindthakur25
I have added a README section.

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

govindthakur25, Dyocius, vinhtru, and peop81 reacted with thumbs up emoji

Copy link
Author

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

peop81 reacted with thumbs up emoji

Copy link

@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.

Copy link

Dyocius commented Jun 30, 2022

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

Copy link
Author

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?

Copy link
Collaborator

@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 🤞

"react": ">0.13.0"
"react": ">0.13.0",
"flyd": ">=0.2.8",
"ramda": ">=0.28.0"
Copy link
Collaborator

@alexcjohnson alexcjohnson Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flyd and ramda seem like they'd be better as dependencies rather than peerDependencies - and then let's change from >= to ^

Copy link

@floriancargoet floriancargoet Oct 13, 2022
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are 2 news deps really required?
ramda seems to be used just to write this code in the functional style.
The author himself said in the related issue that it could be replaced with:

function getSizeForLayout(entries) {
 const { width, height } = entries[0].contentRect;
 return { layout: { width, height } };
}

Copy link
Collaborator

@alexcjohnson alexcjohnson Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question @floriancargoet - ramda has a couple of other uses in this PR (and we do use it a good deal elsewhere at Plotly), but to my mind your getSizeForLayout rewrite is more readable than the full functional version.

Copy link
Author

@JamesRamm JamesRamm Oct 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While yes getSizeForLayout can be written easily without ramda (happy to do this), the main reason it was included was for mergeDeepRight which is a relatively complex function to implement.
We could go for an independent implementation but my argument would be that ramda is relatively small and compatible with tree shaking

Copy link
Collaborator

@alexcjohnson alexcjohnson Oct 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, deep merge is annoying to reimplement. I have no problem adding this (and flyd) but I do think they belong in dependencies.

Plotly.react(internalRef, state);
});

const endAppend = appendData.map(({ data, tracePos }) => Plotly.extendTraces(internalRef, data, tracePos));
Copy link
Collaborator

@alexcjohnson alexcjohnson Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you've implemented extendTraces here, let's finish it by including maxPoints (the optional 4th argument to extendTraces)

JamesRamm reacted with thumbs up emoji
const appendData = useMemo(stream, []);
const plotlyState = useMemo(
() => scan(mergeDeepRight, { data: [], config: {}, layout: {} }, updates),
[]
Copy link

@terebentina terebentina Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't updates be added to the dependencies list here since it's used inside the hook?

Copy link
Author

@JamesRamm JamesRamm Oct 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since updates is memoized with an empty array, it will only be computed once on mount, so adding it to the dependencies array or not for plotlyState will have no effect....

endS.end(true);
};
}
}, [internalRef, plotlyState, updates, appendData]);
Copy link

@terebentina terebentina Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't observer be added as a dependency here since it's used inside the hook?

nlappas reacted with thumbs up emoji
Copy link

Michael-fore commented May 19, 2023
edited
Loading

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

@alexcjohnson

Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@alexcjohnson alexcjohnson alexcjohnson left review comments

+3 more reviewers

@floriancargoet floriancargoet floriancargoet left review comments

@terebentina terebentina terebentina left review comments

@himbeles himbeles himbeles requested changes

Reviewers whose approvals may not affect merge requirements
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /