4

在我的 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> 
4

1 回答 1

6

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

这是它的工作原理:http: //jsfiddle.net/ryleyb/RFeEt/

我所做的只是在 flot 创建的画布上绑定draganddragend函数,并设置一个全局变量以让您知道正在发生平移。平移结束后,它会在关闭panning变量之前等待 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>'); 
    }
}); 
于 2011-12-07T19:13:09.207 回答