Skip to content
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

React component for plotly charts #2633

Closed
wants to merge 1 commit into from

Conversation

washort
Copy link

@washort washort commented Jun 28, 2018

This uses the official Plotly React components to do charts.
I'm not sure if this has compatibility issues with existing custom charts, since we don't use them.

const Plot = createPlotlyComponent(Plotly);


const timeSeriesToPlotlySeries = (ss) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW the parens are optional when there is only one variable. This could be rewritten as:

const timeSeriesToPlotlySeries = ss => {

There are also many places below where this comment applies, like line 24 and line 26. But it's obviously just style, and it's up to you.

Copy link
Author

Choose a reason for hiding this comment

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

Curious. eslint has caught this many times for me, not sure how I missed this.

Copy link
Author

Choose a reason for hiding this comment

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

-- Ah. the arrow-parens eslint rule expects parens for braceful arrow functions and no parens for braceless ones.

x: null,
ys: null,
};
this.refreshCustom = this.refreshCustom.bind(this);
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a neat way around this that I recently discovered myself. You can omit this line if you define refreshCustom like this:

refreshCustom = (figure, plotlyElement) => {
  // ...
}

See this blog post section for more info. As with everything, there are pros and cons with this approach, but if you find that you have a lot of this.whatever = this.whatever.bind(this) lines in your code, it's a trick worth considering.

if (!nextProps.options) { return {}; }
if (nextProps.options.globalSeriesType === 'custom') {
const [x, ys] = timeSeriesToPlotlySeries(nextProps.series);
return { x, ys, revision: prevState.revision + 1 };
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice use of ES6 default prop values! 😃

} catch (err) {
if (this.props.options.enableConsoleLogs) {
// eslint-disable-next-line no-console
console.log(`Error while executing custom graph: ${err}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice use of template strings! 😃

}

render() {
if (!this.props.options) { return ''; }
Copy link
Contributor

Choose a reason for hiding this comment

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

By convention, I try to return null when I don't want a render function to do anything. I don't think anything weird will happen if you return an empty string, but I guess it could theoretically have some weird side effects.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, if your ESLint rules allow it, this could be rewritten as:

if (!this.props.options) return '';

or

if (!this.props.options) return null;

className="plotly-chart-container"
revision={this.state.revision}
style={{ width: '100%', height: '100%' }}
useResizeHandler
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh wow, I didn't even know you could do this. Nice find!

}

static getDerivedStateFromProps(nextProps, prevState) {
if (!nextProps.options) { return {}; }
Copy link
Contributor

Choose a reason for hiding this comment

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

getDerivedStateFromProps should return null if state doesn't need to be updated.

@washort
Copy link
Author

washort commented Jun 28, 2018

Thanks @openjck, you caught some stuff I learned about later but never came back and applied to this earlier code :-)

@washort
Copy link
Author

washort commented Sep 5, 2018

I know this won't be ready to merge until after the 5.0 release, but if you'd have time to look it over beforehand I'd appreciate it.

@openjck
Copy link
Contributor

openjck commented Sep 5, 2018

I know this won't be ready to merge until after the 5.0 release, but if you'd have time to look it over beforehand I'd appreciate it.

I'm not sure who that's addressed to. If it would help, I'd be happy to re-review.

@jezdez
Copy link
Member

jezdez commented Sep 24, 2018

@washort The tests are failing in the build stage, I tried to rebuild them a few times but it's not a transient error.

@washort
Copy link
Author

washort commented Oct 19, 2018

This is superseded by #2988.

@washort washort closed this Oct 19, 2018
@jezdez jezdez deleted the react-plotly branch February 25, 2019 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants