简体   繁体   English

在D3饼图中添加工具提示的问题

[英]Issue with adding tooltip in D3 pie chart

I am using D3.js to build Pie chart. 我正在使用D3.js构建饼图。 I am following this tutorial to build Pie chart in D3. 我正在按照教程在D3中构建饼图。 Now I want to add tooltip in each of segment of pie chart. 现在,我想在饼图的每个分段中添加工具提示。

I am trying to use callback to perform some event. 我正在尝试使用回调执行一些事件。 Like this .. 像这样 ..

<script>
  var pie = new d3pie("pieChart", {
"header": {
    "title": {
        "text": " ",
        "fontSize": 24,
        "font": "open sans"
    },
    "subtitle": {
        "text": " ",
        "color": "#999999",
        "fontSize": 12,
        "font": "open sans"
    },
    "titleSubtitlePadding": 9
},
"footer": {
    "color": "#999999",
    "fontSize": 10,
    "font": "open sans",
    "location": "bottom-left"
},
"size": {
    "canvasWidth": 590
},
"data": {
    "sortOrder": "value-desc",
    "content": [
        {
            "label": "Google",
            "value": 264131,
            "color": "#D78902"
        },
        {
            "label": "Twitter",
            "value": 118812,
            "color": "#04C3FD"
        },
        {
            "label": "Facebook",
            "value": 157618,
            "color": "#0457FD"
        },
        {
            "label": "Yahoo",
            "value": 114384,
            "color": "#FD0404"
        }
    ]
},
"labels": {
    "outer": {
        "pieDistance": 32
    },
    "inner": {
        "hideWhenLessThanPercentage": 3
    },
    "mainLabel": {
        "fontSize": 11
    },
    "percentage": {
        "color": "#ffffff",
        "decimalPlaces": 0
    },
    "value": {
        "color": "#adadad",
        "fontSize": 11
    },
    "lines": {
        "enabled": true
    }
},
"effects": {
    "pullOutSegmentOnClick": {
        "effect": "linear",
        "speed": 400,
        "size": 8
    }
},
"misc": {
    "gradient": {
        "enabled": true,
        "percentage": 100
    }
},
callbacks: {
    onMouseoverSegment: function(info) {
        alert("hi");
    }
}
});
</script>

Here if you can see I have added onMouseoverSegment event in callbacks which will trigger alert on mouseover. 在这里,如果您可以看到我在回调中添加了onMouseoverSegment事件,该事件将在鼠标悬停时触发警报。

Now The real thing. 现在是真实的东西。 I want to show tooltip here with respective value on mouseover and remove that tooltip on mouseout. 我想在此处显示带有鼠标悬停时相应值的工具提示,并在鼠标悬停时删除该工具提示。 How can I do this ? 我怎样才能做到这一点 ? please help. 请帮忙。

Check JSFIDDLE 检查JSFIDDLE

I haven't used the d3pie library you used as d3 already has a pie layout which works well. 我没有使用过您使用的d3pie库,因为d3已经具有一个饼状布局,可以很好地工作。 Here is a JSfiddle with a working version with your data. 这是一个包含数据的工作版本的JSfiddle I couldn't at a glance see how to extend that library. 我一眼看不到如何扩展该库。 It might be possible to use this example in the library at tooltips. 可以在工具提示中的库中使用此示例。

  g.append("path")
  .attr("d", arc)
  .style("fill", function(d) { return d.data.color; })
  .append('title')
  .text(function(d){return d.data.value})

This code adds the title text to each svg path object which are the segments. 此代码将标题文本添加到每个svg路径对象(即段)。 If you want more fancy non-browser default tooltips look at this book on d3 which has a section on tooltips. 如果您想了解更多非浏览器默认的工具提示,请参阅d3上的这本书,其中有关于工具提示的部分。

Fancy tooltips use event listeners using the selection.on() method with the 'mouseover' and 'mouseout' events. 花哨的工具提示将事件侦听器与select.on()方法一起用于“ mouseover”和“ mouseout”事件。 This can also be used for highlighting and other ways and are described in the above book. 这也可以用于突出显示和其他方式,并在上本书中进行了描述。 Which is well worth a read and explains a lot about how d3.js works. 值得一读并解释了d3.js的工作原理。

The D3Pie library already has tooltips in-built. D3Pie库已经内置了工具提示。 And they're pretty customizable too. 而且它们也非常可定制。 All you need to do is enable them and they should appear just fine 您需要做的就是启用它们,它们应该看起来很好

Here's how it looks (from the d3pie website) 外观如下(来自d3pie网站)

在此处输入图片说明

and here's the code you should add to enable it 这是您应该添加以启用它的代码

"tooltips": {
            "enabled": true,
            "type": "placeholder",
            "string": "{label}: {value} ({percentage}%)",
            "styles": {
                "backgroundColor": "#040404",
                "borderRadius": 5
            }
        }

See the api here for more customizations you can do 请参阅此处的api 以了解更多可定制的信息

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

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