点击标签 - 饼图jQuery Flot

时间:2014-08-05 11:52:02

标签: javascript jquery charts flot

我正在使用jQuery flot饼图,我在交互式饼图中使用了嵌入式标签。当我单击饼图中的某个扇区时,它会显示警告,但是当我点击嵌入在扇区中的标签时,它不会响应点击操作。这有什么解决方案吗?

我的源代码

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://static.pureexample.com/js/flot/excanvas.min.js"></script>
<script src="http://static.pureexample.com/js/flot/jquery.flot.min.js"></script>
<script src="http://static.pureexample.com/js/flot/jquery.flot.pie.min.js"></script>

<!-- CSS -->
<style type="text/css">
#flotcontainer {
    width: 600px;
    height: 400px;
    text-align: left;
}
</style>

<!-- Javascript -->
<script type="text/javascript">
$(function () { 
    var data = [
        {label: "data1", data:10},
        {label: "data2", data: 20},
        {label: "data3", data: 30},
        {label: "data4", data: 40},
        {label: "data5", data: 50},
        {label: "data6", data: 60},
        {label: "data7", data: 70}
    ];

    var options = {
            series: {
                pie: {
                    show: true,
                    radius: 1,
                    tilt: 0.5,
                    label:{                        
                        radius: 3/4,
                        formatter: function (label, series) {
                            return '<div style="border:1px solid gray;font-size:8pt;text-align:center;padding:5px;color:white;">' + label + '<br/>' +   
                            Math.round(series.percent) + '%</div>';
                        },
                        background: {
                            opacity: 0.5,
                            color: '#000'
                        }
                    }
                }
            },
            legend: {
                show: false
            },
            grid: {
                hoverable: true,
                clickable: true
            }
         };

    $.plot($("#flotcontainer"), data, options);  

    $("#flotcontainer").bind("plothover", function(event, pos, obj){
        if (!obj){return;}
        percent = parseFloat(obj.series.percent).toFixed(2);

        var html = [];
        html.push("<div style=\"flot:left;width:105px;height:20px;text-align:center;border:1px solid black;background-color:", obj.series.color, "\">",
                  "<span style=\"font-weight:bold;color:white\">", obj.series.label, " (", percent, "%)</span>",
                  "</div>");

        $("#showInteractive").html(html.join(''));        
    });
   $("#flotcontainer").bind("plotclick", function(event, pos, obj){
        if (!obj){return;}
        percent = parseFloat(obj.series.percent).toFixed(2);
        alert(obj.series.label + " ("+ percent+ "%)");

    });


});
</script>

<!-- HTML -->
<div>
    Hover percent : <span id="showInteractive"></span>
</div>
<div id="flotcontainer"></div>

如果您想尝试一下,可以使用此编辑器

http://plnkr.co/edit/LvUuMYIYUAi3RZRYGSjV?p=preview

2 个答案:

答案 0 :(得分:5)

此处的另一个选择是手动传递事件:

$('.pieLabel').bind('click',function(e){
    $("#flotcontainer .flot-overlay").trigger(e);
});

现在,任何对饼图标签的点击都会在flot的overlay画布上手动调用点击事件。这将调用plotclick事件。

更新了plunkr

答案 1 :(得分:3)

您可以添加$(".pieLabel").click(function() { alert("clicked"); });以获取饼图标签上的点击次数

样品:

$("#flotcontainer").bind("plotclick", function(event, pos, obj){
    if (!obj){return;}
    alert('hello');

});

$(".pieLabel").click(function() { alert("clicked"); });

使用系列信息的其他解决方案:在定义图例(在div上单击)时添加对自定义函数的调用,并传递正确的参数:

series: {
    pie: {
        show: true,
        radius: 1,
        tilt: 0.5,
        label:{                        
            radius: 3/4,
            formatter: function (label, series) {
                return '<div onclick="legendClicker(' + series.percent + ');" style="border:1px solid gray;font-size:8pt;text-align:center;padding:5px;color:white;">' + label + '<br/>' +   
                Math.round(series.percent) + '%</div>';
            },
            background: {
                opacity: 0.5,
                color: '#000'
            }
        }
    }
},

功能:

function legendClicker(info) {
  // Do what you want
  alert("legend click / " + info);
}
相关问题