2011-10-04 79 views
0

我正在使用Flot绘制折线图。它实时更新并有多个系列。我希望能够在图形中检测单个系列上的鼠标左键和右键单击。Flot - 检测左键单击并右键单击

目前,我可以检测到一个鼠标左键单击,但右键单击只需调出我的浏览器的右键菜单。

这里是我到目前为止有:

function myClick(event, pos, obj) { 
     if (!obj) { 
      return; 
     } 
     alert('clicked!'); 
    } 


    var placeholder = $("#placeholder"); 


    // setup plot 
    var options = { 
     ... 
     grid: {clickable: true, hoverable: true}, 
     ... 
    }; 


    var initialData = getMyData(); 

    var plot = $.plot(placeholder, initialData , options); 

    placeholder.bind("plotclick", myClick); 

有没有一种方法来检测图中的一系列右键点击?

+0

http://stackoverflow.com/questions/1206203/how-to-distinguish-between-left-and-right-mouse-click-with-jquery/2725963#2725963 – NimChimpsky

+0

这不是一个真正的问题,你问是否可以检测到右键点击,这已被覆盖[很多次](http://stackoverflow.com/questions/2405771/is-right-click-a-javascript-event)。 [搜索是你的朋友](http://stackoverflow.com/search?q=%5Bjavascript%5D+detect+right+clicks&submit=search)! – Alex

+0

对不起,我应该更清楚一点。我想要在页面上的任何位置检测一系列flot图的左右点击。 – Peter

回答

2

看看flot源,这不是一个内置的功能。它被编码为只处理网格上的mousemove,mouseleave和click事件。如果我是你,我会直接修改flot源并用mousedown事件替换click事件。一旦你这样做,应该很容易处理左,右和中心点击。

编辑

我意识到这是一个古老的答案,但一个念头出现在我。解决此问题的方法是在不修改源代码的情况下使用plothover来监视鼠标是否超过某个点,然后将通用mousedown处理程序绑定到绘图div。

var currentPoint = null; 
$("#placeholder").bind("plothover", function (event, pos, item) { 
    if (item) { 
     currentPoint = item; 
    } else { 
     currentPoint = null;    
    } 
}); 

$('#placeholder').mousedown(function(event) { 
    if (currentPoint) 
    { 
     switch (event.which) { 
     case 1: 
      alert('Left mouse button pressed on ' + currentPoint.series.label); 
      break; 
     case 2: 
      alert('Middle mouse button pressed on ' + currentPoint.series.label); 
      break; 
     case 3: 
      alert('Right mouse button pressed on ' + currentPoint.series.label); 
      break; 
     default: 
      alert('You have a strange mouse on ' + currentPoint.series.label); 
     } 
    } 
}); 

查看小提琴here