【发布时间】:2018-12-24 13:27:31
【问题描述】:
这是制作饼图的代码。
我在饼图中添加了 2 个功能。第一个是当鼠标悬停在弧度上时增加弧度。
其次,当我悬停时,工具提示会显示鼠标的 y 位置,并且此工具提示会随鼠标移动。
但我的问题是。工具提示文本不会随着鼠标的移动而移动。
我已经看到了各种堆栈溢出链接,但我无法找到解决方案。下面我还附上了链接,这样问题就不会被标记为重复。
请不要给我新的工具提示代码。更正这个。
以下是我已经访问过的链接
Why is my D3.js tooltip not working
D3 - Positioning tooltip on SVG element not working
Tooltips not showing on mouse move and mouse over
jsfiddle 的链接:- https://jsfiddle.net/uoh1bsrp/
谢谢,
舒巴姆·夏尔马
width = 600
height = 600
svg = d3.select('body').append('svg').attr('width',width).attr('height',height).append('g').attr('transform', 'translate(' + (width / 2) + ',' + (height / 2) + ')');
a = [2,5,7]
pie = d3.pie()
radius = 150
arc = d3.arc().innerRadius(0)
.outerRadius(radius);
arcs = svg.selectAll('arc').data(pie(a)).enter().append('g')
arcs.append("path").attr('d',function(d){return arc(d)})
tooltip = svg.append('text')
color = ['red','green','blue']
path = d3.selectAll('path').style('fill',function(d,i){return color[i]})
path.on('mouseover',handlemouseover)
path.on('mouseout',handlemouseout)
path.on('mousemove',handlemousemove)
function handlemouseover(){
d3.select(this).attr('d',function(d){return d3.arc().innerRadius(0)
.outerRadius(180)(d)});
}
function handlemousemove(){
// svg.append('text').text(function(){return 'shubham'}).style('top',(d3.event.layerY + 10)+'px').style('left',(d3.event.layerX + 10) + 'px')
tooltip.text(function(){return d3.event.layerX}).style('top',(d3.event.layerY + 10)+'px').style('left',(d3.event.layerX + 10) + 'px').style('display','block');
}
function handlemouseout(){
d3.select(this).attr('d',function(d){return d3.arc().innerRadius(0).outerRadius(radius)(d)});
tooltip.style('display', 'none');
}
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-path.v1.min.js"></script>
<script src="https://d3js.org/d3-shape.v1.min.js"></script>
</head>
【问题讨论】:
-
但是代码在哪里?
-
我已经添加了。之前不小心被删除了。
-
能否请您创建一个 jsfiddle。
-
这是 jsfiddle jsfiddle.net/uoh1bsrp
-
@Andrew 请看一下。
标签: javascript html css d3.js graph