Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Fix graph issues #178

Merged
merged 6 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ venv/
*.pyc
*.log
.DS_Store

dist/
30 changes: 10 additions & 20 deletions src/components/Graph.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,29 @@ export default class PlotlyGraph extends Component {
constructor(props) {
super(props);
this.bindEvents = this.bindEvents.bind(this);
this.state = {hasPlotted: false};
this._hasPlotted = false;
}

plot(props) {
const {id, figure, animate, animation_options, config} = props;
const {hasPlotted} = this.state;
const gd = document.getElementById(id);
if (animate && hasPlotted && figure.data.length === gd.data.length) {
if (animate && this._hasPlotted && figure.data.length === gd.data.length) {
return Plotly.animate(id, figure, animation_options);
} else {
return Plotly.react(id, figure.data, figure.layout, config).then(() => {
this.bindEvents(props);
this.setState({hasPlotted: true});
return Plotly.react(id, figure.data, figure.layout, config).then(() => {
if (!this._hasPlotted) {
this.bindEvents();
Plotly.Plots.resize(document.getElementById(id));
this._hasPlotted = true;
}
});
}
}

bindEvents(props) {
const {id, fireEvent, setProps, clear_on_unhover} = props;
bindEvents() {
const {id, fireEvent, setProps, clear_on_unhover} = this.props;

const gd = document.getElementById(id);
gd.removeAllListeners();

gd.on('plotly_click', (eventData) => {
const clickData = filterEventData(gd, eventData, 'click');
Expand Down Expand Up @@ -163,19 +164,8 @@ export default class PlotlyGraph extends Component {

const figureChanged = this.props.figure !== nextProps.figure;

/*
* Rebind events in case fireEvent or setProps
* wasn't defined on initial render
* TODO - Is it safe to rebind events?
*/
const shouldBindEvents = (
(!this.props.setProps && nextProps.setProps) ||
(!this.props.fireEvent && nextProps.fireEvent)
);
if (figureChanged) {
this.plot(nextProps);
} else if (shouldBindEvents) {
this.bindEvents(nextProps);
}
}

Expand Down