【问题标题】:Displaying the axis values for a mouseover event in a chart image在图表图像中显示鼠标悬停事件的轴值
【发布时间】:2011-01-05 17:35:58
【问题描述】:

我将一个图表图像(带有 x 轴和 y 轴)返回到创建我的 matplotlit 的浏览器。 现在我想在有鼠标悬停事件时显示轴值。

【问题讨论】:

    标签: javascript graphics matplotlib


    【解决方案1】:

    当我最近做这样的事情时,我从 matplotlib 返回了一个 PNG 到浏览器。然后,我使用与绘制的点相对应的像素值将图像映射应用于 PNG。我使用了一个 onmouseover 事件来弹出一个“工具提示”(实际上只是一个绝对定位的 div),其中包含有关该点的元数据。

    article 为我的努力奠定了基础,但我记得在他的实现中存在一些问题(主要是由于 matplotlib api 的变化)。如果这个问题在星期一仍然存在,我将使用我的实现中的特定代码更新这个答案(我目前无法访问我的工作机器)。

    编辑:作为承诺的示例代码

    import matplotlib.pyplot as plt
    
    dpi = 96
    fig = plt.figure(figsize=(8,8),dpi=dpi)
    imgWidth = fig.get_figwidth() * dpi ## this is the actual pixel size of the plot
    imgHeight = fig.get_figheight() * dpi ## this is the actual pixel size
    
    my_lines = []
    my_lines.append(plt.plot(Xs,Ys,marker='o')[0]) # add a line object to plot
    
    mapHTML = '<MAP name="curveMap">'
    for lineObj in my_lines:
      ## convert the points to pixel locations, for pop-ups
      lineObj._transform_path()
      path, affine = lineObj._transformed_path.get_transformed_points_and_affine()
      path = affine.transform_path(path)
      for real,pixel in zip(lineObj.get_xydata(),path.vertices):
        mapHTML+='''<AREA shape=\"circle\" coords=\"%i,%i,5\" href=\"javascript: void(0);\" onmouseout=\"outFly();\" onmouseover=\"javascript: popFly\(event,\\'%s\\',%i,%i\)\" />''' % (pixel[0],imgHeight-pixel[1],lineName,real[0],real[1])
    mapHTML += '</MAP>'
    fig.savefig(file(plot_file,"w"),format='png',dpi=dpi)
    plt.close(fig)
    plotHTML = '''<img src="/getPlot?uniq=%f" width="%i" height="%i" ismap usemap="#curveMap" onload="imageLoadCallback();" id="curPlot" />''' % (time.time(),imgWidth,imgHeight)
    return "({'plotHTML':'%s','mapHTML':'%s'})" % (plotHTML,mapHTML)
    

    您会看到我正在将图像写入 png 文件,然后返回 JSON。我使用 javascript 使用新的 img 和图像映射来更新 DIV。

    【讨论】:

      【解决方案2】:

      这是一个使用 jQuery 的解决方案:

      $('#myChart').mousemove(function(e){
          var x = e.pageX - this.offsetLeft;
          var y = e.pageY - this.offsetTop;
          // Do something with x and y;
      });
      

      请参阅http://docs.jquery.com/Tutorials:Mouse_Position 了解更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-27
        • 1970-01-01
        • 1970-01-01
        • 2014-01-07
        • 2013-11-15
        • 2012-12-16
        相关资源
        最近更新 更多