flotplotpan 与plotclick:平移开启时如何忽略点击事件?

发布于 2024-12-19 22:33:34 字数 2801 浏览 1 评论 0原文

在我的 Flot 应用程序中,我启用了plotclick 和plotpan 事件。然而, 我发现每次平移绘图时,plotclick 事件也会 触发?我想知道如何在平移操作时忽略点击事件 已开启?即,仅在平移时触发平移事件监听器,仅触发 单击时单击事件监听器。

<!DOCTYPE HTML> 
<html> 
 <head> 
    <meta http-equiv="Content-Type" content="text/html; 
charset=utf-8"> 
    <title>Flot Examples</title> 
    <link href="http://people.iola.dk/olau/flot/examples/layout.css" rel="stylesheet" type="text/css"> 
    <!--[if lte IE 8]><script language="javascript" type="text/ 
javascript" src="http://people.iola.dk/olau/flot/excanvas.min.js"></script><![endif]--> 
    <script language="javascript" type="text/javascript" src="http:// 
people.iola.dk/olau/flot/jquery.js"></script> 
    <script language="javascript" type="text/javascript" src="http:// 
people.iola.dk/olau/flot/jquery.flot.js"></script> 
    <script language="javascript" type="text/javascript" src="http:// 
people.iola.dk/olau/flot/jquery.flot.navigate.js"></script> 
<script type="text/javascript"> 
$(function () { 
    // generate data set from a parametric function with a fractal 
    // look 
    function sumf(f, t, m) { 
        var res = 0; 
        for (var i = 1; i < m; ++i) 
            res += f(i * i * t) / (i * i); 
        return res; 
    } 
    var d1 = []; 
    for (var t = 0; t <= 2 * Math.PI; t += 0.01) 
        d1.push([sumf(Math.cos, t, 10), sumf(Math.sin, t, 10)]); 
    var data = [ d1 ]; 
    var placeholder = $("#placeholder"); 
    var options = { 
        series: { lines: { show: true }, shadowSize: 0 }, 
        xaxis: { zoomRange: [0.1, 10], panRange: [-10, 10] }, 
        yaxis: { zoomRange: [0.1, 10], panRange: [-10, 10] }, 
        zoom: { 
            interactive: true 
        }, 
        pan: { 
            interactive: true 
        }, 
                grid : { 
                        autoHighlight : false, 
                        hoverable : true, 
                        clickable : true, 
                } 
    }; 
    var plot = $.plot(placeholder, data, options); 
    // show pan/zoom messages to illustrate events 
    placeholder.bind('plotpan', function (event, plot) { 
        var axes = plot.getAxes(); 
        $(".message").html("Panning to x: "  + 
axes.xaxis.min.toFixed(2) 
                           + " &ndash; " + axes.xaxis.max.toFixed(2) 
                           + " and y: " + axes.yaxis.min.toFixed(2) 
                           + " &ndash; " + axes.yaxis.max.toFixed(2)); 
    }); 
    placeholder.bind('plotclick', function (event, pos, item) { 
        alert('click'); 
    }); 
}); 

</script> 
<body> 
<div id="placeholder" style="width:600px;height:300px;"></div> 
 </body> 
</html> 

In my Flot app, I enabled both plotclick and plotpan events. However,
I found that every time when pan the plot, the plotclick event is also
triggered? I am wondering how to ignore click event when pan operation
is on? i.e., only trigger pan event listener when in pan, only trigger
click event listener when in click.

<!DOCTYPE HTML> 
<html> 
 <head> 
    <meta http-equiv="Content-Type" content="text/html; 
charset=utf-8"> 
    <title>Flot Examples</title> 
    <link href="http://people.iola.dk/olau/flot/examples/layout.css" rel="stylesheet" type="text/css"> 
    <!--[if lte IE 8]><script language="javascript" type="text/ 
javascript" src="http://people.iola.dk/olau/flot/excanvas.min.js"></script><![endif]--> 
    <script language="javascript" type="text/javascript" src="http:// 
people.iola.dk/olau/flot/jquery.js"></script> 
    <script language="javascript" type="text/javascript" src="http:// 
people.iola.dk/olau/flot/jquery.flot.js"></script> 
    <script language="javascript" type="text/javascript" src="http:// 
people.iola.dk/olau/flot/jquery.flot.navigate.js"></script> 
<script type="text/javascript"> 
$(function () { 
    // generate data set from a parametric function with a fractal 
    // look 
    function sumf(f, t, m) { 
        var res = 0; 
        for (var i = 1; i < m; ++i) 
            res += f(i * i * t) / (i * i); 
        return res; 
    } 
    var d1 = []; 
    for (var t = 0; t <= 2 * Math.PI; t += 0.01) 
        d1.push([sumf(Math.cos, t, 10), sumf(Math.sin, t, 10)]); 
    var data = [ d1 ]; 
    var placeholder = $("#placeholder"); 
    var options = { 
        series: { lines: { show: true }, shadowSize: 0 }, 
        xaxis: { zoomRange: [0.1, 10], panRange: [-10, 10] }, 
        yaxis: { zoomRange: [0.1, 10], panRange: [-10, 10] }, 
        zoom: { 
            interactive: true 
        }, 
        pan: { 
            interactive: true 
        }, 
                grid : { 
                        autoHighlight : false, 
                        hoverable : true, 
                        clickable : true, 
                } 
    }; 
    var plot = $.plot(placeholder, data, options); 
    // show pan/zoom messages to illustrate events 
    placeholder.bind('plotpan', function (event, plot) { 
        var axes = plot.getAxes(); 
        $(".message").html("Panning to x: "  + 
axes.xaxis.min.toFixed(2) 
                           + " – " + axes.xaxis.max.toFixed(2) 
                           + " and y: " + axes.yaxis.min.toFixed(2) 
                           + " – " + axes.yaxis.max.toFixed(2)); 
    }); 
    placeholder.bind('plotclick', function (event, pos, item) { 
        alert('click'); 
    }); 
}); 

</script> 
<body> 
<div id="placeholder" style="width:600px;height:300px;"></div> 
 </body> 
</html> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-12-26 22:33:34

我想不出一个好的方法来做到这一点,所以请仔细检查提供给您的任何其他答案。具体来说,我怀疑将事件直接绑定在画布上是一个好主意,并且使用 setTimeout 调用来处理平移和单击是很糟糕的。

这是它的实际工作情况: http://jsfiddle.net/ryleyb/RFeEt/

我所做的就是绑定 在 flot 创建的画布上拖动dragend 函数,并设置一个全局变量以让您知道正在发生平移。平移结束后,它会等待 100 毫秒,然后关闭平移变量,这将阻止您的点击操作发生。

var panning = false;
$('#placeholder canvas').bind('drag',function(){
    panning = true; 
});
$('#placeholder canvas').bind('dragend',function(){
    setTimeout(function() {panning = false; }, 100);
});
placeholder.bind('plotclick', function (event, pos, item) { 
    if (!panning){
        $('.message').append('plotclick triggered<br>'); 
    }
}); 

I can't come up with a good way to do this, so examine closely any other answers that are given to you. Specifically, I doubt that binding the events directly on the canvas is a good idea and using a setTimeout call to deal with the panning and click is just bad.

Here's it working in action: http://jsfiddle.net/ryleyb/RFeEt/

All I did was bind the drag and dragend functions on the canvas that flot creates, and set a global variable to let you know that a pan is happening. After the pan ends, it waits 100ms before turning off the panning variable, which will stop your click action from being happening.

var panning = false;
$('#placeholder canvas').bind('drag',function(){
    panning = true; 
});
$('#placeholder canvas').bind('dragend',function(){
    setTimeout(function() {panning = false; }, 100);
});
placeholder.bind('plotclick', function (event, pos, item) { 
    if (!panning){
        $('.message').append('plotclick triggered<br>'); 
    }
}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文