简体   繁体   English

Flot-检测左键单击和右键单击

[英]Flot - Detect left-click and right-click

I'm drawing a line graph using Flot. 我正在使用Flot绘制线形图。 It's updated in real-time and has multiple series. 它是实时更新的,并且有多个系列。 I want to be able to detect left and right mouse clicks on individual series in the graph. 我希望能够检测到图形中各个系列的左右鼠标单击。

Currently, I can detect a left mouse click, but right-click just brings up my browser's right-click menu. 目前,我可以检测到鼠标左键单击,但是右键单击只会显示浏览器的右键单击菜单。

Here's what I've got so far: 到目前为止,这是我得到的:

    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);

Is there a way to detect right-clicks on a series in the graph? 有没有一种方法可以检测图中系列的右键单击?

Looking at the flot source, this is not a built-in feature. 从flot源看,这不是内置功能。 It is coded to handle only the mousemove, mouseleave, and click events on the grid. 它被编码为仅处理网格上的mousemove,mouseleave和click事件。 If I were you I would look into modifying the flot source directly and replacing the click event with the mousedown event. 如果您是我,我会考虑直接修改flot源,并用mousedown事件替换click事件。 Once you do that it should be easy to handle left vs right vs center clicks. 完成后,应该很容易处理左键,右键和中心键。

EDITS 编辑

I realize this is an old answer but a thought occurred to me. 我意识到这是一个旧答案,但是我想到了一个想法。 A work around for this, without modifying the source, is to use the plothover to monitor whether the mouse is over a point and then bind a generic mousedown handler to the plot div. 解决此问题的方法是,不修改源代码,而是使用plothover监视鼠标是否在某个点上,然后将通用mousedown处理程序绑定到plot 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);
      }
   }
});

See fiddle here . 在这里看小提琴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM