I'm making some mpld3 plots with linked information. I would like the points (which currently show tooltips) be clickable. Right now, I can embed HTML links into the tooltips, but they're not clickable since the tooltips disappear if you try to hover over them. Is this possible?
Here's an example page showing what I have done and approximately what I have in mind: http://www.eso.org/~aginsbur/EAACTF/EAACTF_plots_long.html
EDIT: My solution, based on the accepted answer, is:
class ClickInfo(mpld3.plugins.PluginBase):
"""mpld3 Plugin for getting info on click
Comes from:
http://stackoverflow.com/a/28838652/814354
"""
JAVASCRIPT = """
mpld3.register_plugin("clickinfo", ClickInfo);
ClickInfo.prototype = Object.create(mpld3.Plugin.prototype);
ClickInfo.prototype.constructor = ClickInfo;
ClickInfo.prototype.requiredProps = ["id", "urls"];
function ClickInfo(fig, props){
mpld3.Plugin.call(this, fig, props);
};
ClickInfo.prototype.draw = function(){
var obj = mpld3.get_element(this.props.id);
urls = this.props.urls;
obj.elements().on("mousedown",
function(d, i){
window.open(urls[i], '_blank')});
}
"""
def __init__(self, points, urls):
self.points = points
self.urls = urls
if isinstance(points, matplotlib.lines.Line2D):
suffix = "pts"
else:
suffix = None
self.dict_ = {"type": "clickinfo",
"id": mpld3.utils.get_id(points, suffix),
"urls": urls}
which then gets used like this:
tooltip = mpld3.plugins.PointHTMLTooltip(points, labels,
voffset=10,
hoffset=10)
mpld3.plugins.connect(fig, tooltip)
mpld3.plugins.connect(fig, ClickInfo(points, urls))