I see that in plotly.js, there is the potential for hover events. However, aside from a FigureWidget (which requires jupyter), I’m not finding examples of using events in plotly.py.
Since the py library uses the same underlying js library, is it possible to build custom javascript on my webpage to capture event data from the plotly chart that was generated? For instance – if a user hovers over an image, or clicks in the chart – can I use a click event listener to capture x-axis, or y-axis value, or series name, or anything about the chart? If so, I would like to see an example or two that would help me out. if I add a click event listener to my webpage, and click in the chart – i just see target as ‘svg’. – Even tags that are generated via the plotly html do not seem to be accessible or interactive on the webpage.
I am generating the plot in python, and exporting to html. I am not using Dash. I would just like to capture interactive data from my plotly chart that was generated via python, and placed in the HTML document. Maybe it’s possible to do via javascript - even if the chart was originally created from python?
And even provided this worked, how could I fetch any chart data? Such as x-axis value or y-axis value where the user clicked? Or an attribute from a trace or image they clicked on?
I think first, I need to demonstrate the ability for JS to recognize the chart- and I’m not able to do that so far.
When plotly generates the HTML, it includes the chart HTML, and then JS at the end. I noticed, it doesn’t identify the chart as cleanly as “myDiv” in the plotly.js examples. Instead - here is the first few lines of the auto-generated JS that follows the HTML:
<script type ="text/javascript">
window.PLOTLYENV = window.PLOTLYENV || {};
if (document.getElementById("0a1fcd19-8e02-4e67-81b3-8a9f4d36785d")) {
Plotly.newPlot("0a1fcd19-8e02-4e67-81b3-8a9f4d36785d", [{
Notice this unique ID code. It’s different each time the chart is generated.
Also, I’m wondering if the code for events needs to be on the python side, so it’s bundled with the html+js — or if I’ll be able to get it to work in my html page after the chart code is inserted.
Here’s what I’m doing. I read that there’s a built-in way to add JS to bundle with the chart, specifically for js event handling called post_script. So I define the post script js, and pass along with the creation and that all seems to work. You can also control the name of the DIV, so I’m now defining that as well:
I don’t necessarily want it to be a string, I just need to access the value and work with the data. When i use JSON.stringify() then the event doesn’t fire anymore.
Ok, this gets me some progress. Here’s a clip what I see. It appears to include everything on the series clicked. Now i need to try to get data for the point that was clicked. like the x-axis value.