我正在製作一些帶有鏈接信息的mpld3圖。我希望點(目前顯示工具提示)是可點擊的。現在,我可以將HTML鏈接嵌入到工具提示中,但如果您嘗試將鼠標懸停在工具提示上,它們就不會點擊,因爲工具提示消失了。這可能嗎?可點擊的點和可點擊的工具提示
這裏展示我做了什麼,並約我心裏有一個示例頁面: http://www.eso.org/~aginsbur/EAACTF/EAACTF_plots_long.html
編輯:我的解決方案的基礎上,接受的答案是:
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}
然後把它像這樣使用:
tooltip = mpld3.plugins.PointHTMLTooltip(points, labels,
voffset=10,
hoffset=10)
mpld3.plugins.connect(fig, tooltip)
mpld3.plugins.connect(fig, ClickInfo(points, urls))
這對我的工作很好,但它陷在Firefox的彈出式窗口攔截。 – Michael 2017-03-05 21:34:38