我正在使用 d3.js 创建一个条形图。在每个条形图的上方,我将显示一些文本。当用户悬停在条形图上时,应该显示文本。当他们悬停离开时,文本将消失。为了做到这一点,我需要将
因此,我可以使用
谢谢! 编辑:为了增加更多背景信息,这是我目前的内容...
那段代码会生成:
<text>
和 <rect>
元素分组到一个 <g>
元素中。<g class="gbar">
<rect x="0" y="50" width="10" height="50" />
<text x="15" y="40">A</text>
</g>
<g class="gbar">
<rect x="11" y="75" width="10" height="25" />
<text x="16" y="65">B</text>
</g>
<g class="gbar">
<rect x="22" y="25" width="10" height="75" />
<text x="27" y="35">C</text>
</g>
因此,我可以使用
.gbar:hover rect, .gbar:hover text { ... }
CSS 样式来更改 <rect>
和 <text>
元素的颜色和不透明度。对于每个数据项,我如何使用 d3.js 将 <rect>
和 <text>
元素放入一个 <g>
元素中呢?谢谢! 编辑:为了增加更多背景信息,这是我目前的内容...
var svg = d3.select('.mygraph')
.append('svg')
.attr('height', 100);
svg.selectAll('rect')
.data(dataSet)
.enter()
.append('rect')
.attr('x', calcX)
.attr('y', calcY)
.attr('width', 10)
.attr('height', calcH);
svg.selectAll('text')
.data(dataSet)
.enter()
.append('text')
.text(function (d) {
return d.Text;
})
.attr('x', textX)
.attr('y', textY);
那段代码会生成:
<svg>
<rect x="0" y="50" width="10" height="50" />
<rect x="11" y="75" width="10" height="25" />
<rect x="22" y="25" width="10" height="75" />
<text x="15" y="40">A</text>
<text x="16" y="65">B</text>
<text x="27" y="35">C</text>
</svg>
我对 d3.js 还很陌生。