2015-03-02 112 views
3

我正在製作一些帶有鏈接信息的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)) 
+0

這對我的工作很好,但它陷在Firefox的彈出式窗口攔截。 – Michael 2017-03-05 21:34:38

回答

5

我想你可以創建一個新的插件來做到這一點。這裏是an example plugin that pops up an alert when a point is clicked

你可以修改它通過改變打開一個新頁面alert(...);window.open(url, '_blank')});

class ClickInfo(mpld3.plugins.PluginBase): 
    """mpld3 Plugin for getting info on click  """ 

    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} 


fig, ax = plt.subplots() 
points = ax.scatter(np.random.rand(50), np.random.rand(50), 
        s=500, alpha=0.3) 
urls = ["http://example.com/#%d"%i for i in range(50)] 

plugins.connect(fig, ClickInfo(points, urls)) 
mpld3.display() 
+0

我需要進行一些修改,使其更接近https://mpld3.github.io/_modules/mpld3/plugins.html#PointHTMLTooltip,即用於matplotlib點和後綴的東西,但除此之外它效果很好。謝謝! – keflavich 2015-03-03 20:11:45

+0

@keflavich你會鏈接到你的最終解決方案?我正在處理類似的問題,並遇到了使window.open()正常工作的問題 – DathosPachy 2015-08-11 00:08:39

+0

@keflavich謝謝,這似乎工作!它看起來像錯誤試圖使用'plt.plot(...)'而不是'plt.scatter(...)'。另外,如果有其他人閱讀這些內容,請注意遵循許多示例的樣式,倒數第二行將是'plugins.connect(fig,ClickInfo(points [0],url))'(注意括號-zero在'points'之後。)括號似乎是使用'plot'而不是'scatter'所必需的。 – DathosPachy 2015-08-11 13:37:43