Skip to content

Unbind click event #107

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

Closed
caalberts opened this issue Dec 13, 2015 · 9 comments
Closed

Unbind click event #107

caalberts opened this issue Dec 13, 2015 · 9 comments
Assignees
Labels
feature something new

Comments

@caalberts
Copy link

Hi, I have a single page application which contains a plotly chart with click event handler in it. I have another function which removes the chart and plot a new chart after certain events.

Plotly.newPlot(plotSpace, data, layout)
plotSpace.on('plotly_click', click => {
  listAllTransactions(town, click.points[0].data.name, click.points[0].x)
})

Each time the plot is replaced with a new one, the event handler from the previous chart is carried over to the new chart, so a click on the new chart will trigger listAllTransactions twice: first with town from the first chart and second with town from the second chart.

How can I remove the event handler from the first chart? I've tried removeEventHandler to no avail.

@caalberts
Copy link
Author

A workaround I used was to completely remove plotSpace before recreating it for the new plot, thus removing the previous event listener.

Appreciate if anyone has a more elegant solution

@etpinard
Copy link
Contributor

@caalberts Thanks for writing in.

I'd vote for making Plotly.newPlot unbind all events from the graph div (plotSpace in your case).

@mdtusz @alexcjohnson @cldougl thoughts?

@mdtusz
Copy link
Contributor

mdtusz commented Dec 15, 2015

👍 Plotly.newPlot should be fresh.

@cldougl
Copy link
Member

cldougl commented Dec 15, 2015

👍 sounds like a good idea

@mdtusz mdtusz self-assigned this Dec 15, 2015
@mdtusz mdtusz added the feature something new label Dec 15, 2015
@mdtusz
Copy link
Contributor

mdtusz commented Dec 15, 2015

@caalberts should be in the next release after this pr: #117

@caalberts
Copy link
Author

👌 looking forward to it.

@mdtusz mdtusz closed this as completed Dec 17, 2015
@mdtusz
Copy link
Contributor

mdtusz commented Dec 17, 2015

Fixed in #117

@mdtusz mdtusz reopened this Dec 17, 2015
@mdtusz mdtusz closed this as completed Dec 17, 2015
@chriddyp
Copy link
Member

chriddyp commented Feb 14, 2017

Is there any other way to remove or rebind existing handlers without calling newPlot? This issue was the first hit in a google search. Also this stack overflow question is linking here: http://stackoverflow.com/questions/37372193/unbind-click-events-in-plotly

@alexcjohnson
Copy link
Collaborator

I don't see that we document or test this anywhere, but we do support removeListener and removeAllListeners from the npm events module:

function logIt(e) { console.log(e) }

gd.on('plotly_relayout', logIt)

Plotly.relayout(gd, {'xaxis.range': [-1,5]})
// here the listener is invoked
> Object {xaxis.range: Array[2]}
> Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: div#graph.dashboard-plot.js-plotly-plot}

gd.removeListener('plotly_relayout', logIt)

Plotly.relayout(gd, {'xaxis.range': [-2,6]})
// now the listener is not invoked
> Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: div#graph.dashboard-plot.js-plotly-plot}

ghost pushed a commit to DefinitelyTyped/DefinitelyTyped that referenced this issue Oct 9, 2018
* Add definitions for removeAllListeners funtion as detailed plotly/plotly.js#107 (comment)

* adding automargin property as per the documentation: https://plot.ly/javascript/reference/#layout-xaxis-automargin

* making the colorbar property optional

* Fix lint warnings

* updating tests

* create ColorBar interface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature something new
Projects
None yet
Development

No branches or pull requests

6 participants