简体   繁体   English

如何在D3中的图表标签上触发onmouseover事件

[英]How to trigger onmouseover event on chart label in D3

I've got the following chart made with D3: 我已经用D3制作了以下图表:

D3饼图

The space in the middle displays information when the chart is hovered over. 当图表悬停时,中间的空间显示信息。

I have the following code to handle the event: 我有以下代码来处理事件:

var path=svg.selectAll('path')
    .data(pie(get(this, 'data')))
    .enter()
    .append('path')
    .attr({
        d:arc,
        fill:function(d,i){
            return color(d.data.name);
        }
    })
    .on("mouseover", function(d, i) {
        self.setTooltip(d.data.icon, d.data.name, d.data.value);
    })
    .on("mouseout", function(d, i) {
        self.clearTooltip();
    });

I also use the following code to generate Font Awesome icons on the chart: 我还使用以下代码在图表上生成Font Awesome图标:

var text=svg.selectAll('text.value-label')
        .data(pie(get(this, 'data')))
        .enter()
        .append("text")
        .transition()
        .duration(200)
        .attr("transform", function (d) {
            return "translate(" + arc.centroid(d) + ")";
        })
        .attr("dy", ".5em")
        .attr("text-anchor", "middle")
        .attr('class', 'value-label')
        .text(function(d){
            return d.data.icon;
        })
        .style({
            fill:'#fff',
            'font-size':'1.5em',
            'font-family': 'FontAwesome'

        })
        /* This isn't working
        .on("mouseover", function(d, i) {
            self.setTooltip(d.data.icon, d.data.name, d.data.value);
        })
        .on("mouseout", function(d, i) {
            self.clearTooltip();
        });
        */

Currently, the "tooltip"/information in the middle only shows when mouse pointer is over a coloured piece of the chart. 当前,中间的“工具提示” /信息仅在鼠标指针悬停在图表的彩色部分上时显示。 If the pointer is above the text (FontAwesome) label, tooltip remains hidden. 如果指针位于文本(FontAwesome)标签上方,则工具提示将保持隐藏状态。

How do I make the tooltip display when mouse pointer is anywhere within the boundaries of the chart piece, including the text label? 当鼠标指针位于图表块的边界内的任何地方(包括文本标签)时,如何显示工具提示?

You don't need to trigger the mouseover events again in your text elements. 您无需在文本元素中再次触发鼠标悬停事件。 Just set pointer-events to none in your text variable: 只需在文本变量中将pointer-events设置为none

var text = svg.selectAll('text.value-label')
    .data(pie(get(this, 'data')))
    .enter()
    .append("text")
    .attr("pointer-events", "none")
    //etc...

That way, the paths behind them will handle the mouseover events just as if there were no texts. 这样,它们后面的路径将像处理没有文本一样处理鼠标悬停事件。

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

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