无法在d3js条形图上定位工具提示



我有一个使用d3的条形图。当我把鼠标悬停在一个栏上时,我会显示一个工具提示。每当我有不同的数据集(条形图的数量(时,我都无法将工具提示指针定位到条形图上条形图的中心。我需要指针位于图表上任意数量条形图的条形图的中心。我使用的是x轴值,但是工具提示没有放在正确的位置。

以下是同一的片段

var width = 216;
var height = 200;
var barPadding = 18;
var barWidth = 58;
var dataSize = d3.selectAll(dataset).size();
var margin = {
top: 10,
right: 0,
bottom: 58,
left: 30
};
var width_box_sizing_border_box = width + margin.left + margin.right;
var height_box_sizing_border_box = height + margin.bottom + margin.top;

var graph;
var xScale;
var yScale;
var dataset;
var xTicks = 6;
var yTicks = 6;
var tooltipEl = function(d) {
return (
'<div>' + d.val + '</div>'
)
}
dataset = [{
desc: 'test1',
val: 40
}, {
desc: 'some dummy text here',
val: 120
}];
xScale = d3.scaleBand()
.domain(dataset.map(function(d) {
return d.desc;
}))
.range([margin.left, width - margin.right]);
yScale = d3.scaleLinear()
.range([height, 0])
.domain([0, 350]);
graph = d3.select("#graph")
.append("svg")
.attr("class", "bar-chart")
.attr("width", width_box_sizing_border_box)
.attr("height", height_box_sizing_border_box);
// Tool Tip
const div = d3
.select('#graph')
.append('div')
.attr('class', 'tooltip')
.style('opacity', 0);
graph.append("g")
.attr("class", "x-scale")
.attr("transform", "translate(0," + (height + margin.top) + ")")
.call(d3.axisBottom(xScale).ticks(xTicks))
.selectAll(".tick text")
.call(wrap, xScale.bandwidth());
graph.append("g")
.attr("class", "y-scale")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.call(d3.axisLeft(yScale).ticks(yTicks).tickPadding(10));
graph
.append("g")
.attr("transform", "translate(" + (xScale.bandwidth() / 2 - (barWidth - barPadding) / 2) + "," + margin.top + ")")
.attr('class', 'graph-placeholder')
.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("class", "bar1")
.attr("height", height)
.attr("width", barWidth - barPadding)
.attr('x', d => xScale(d.desc));
graph
.append("g")
.attr("transform", "translate(" + (xScale.bandwidth() / 2 - (barWidth - barPadding) / 2) + "," + margin.top + ")")
.attr('class', 'graph-main')
.selectAll("bar1")
.data(dataset)
.enter()
.append("rect")
.attr("class", "bar2")
.attr('x', d => xScale(d.desc))
.attr("y", function(d) {
return yScale(d.val);
})
.attr("height", function(d) {
return height - yScale(d.val);
})
.attr("width", barWidth - barPadding)
.on('mouseover', d => {
div
.html(tooltipEl(d));
div
.transition()
.duration(200)
.style('display', 'block')
.style('opacity', 1);
div
.style('left', xScale(d.desc) + 'px')
.style('top', (height + margin.top + 8) + 'px');
})
.on('mouseout', () => {
div
.transition()
.duration(500)
.style('opacity', 0)
.style('display', 'none')
});
graph
.append("g")
.attr("transform", "translate(" + (xScale.bandwidth() / 2 - (barWidth - barPadding) / 2) + "," + margin.top + ")")
.attr('class', 'bar-label')
.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(d => d.val + '%')
.attr("y", function(d) {
return yScale(d.val) - 5;
}).attr('x', function(d) {
return xScale(d.desc) + ((barWidth - barPadding) / 2 - d3.select(this).node().getBBox().width / 2);
});
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1,
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}
.bar-chart {
background-color: #ccc;
}
.bar2 {
fill: steelblue;
}
.bar1 {
fill: #f2f2f2;
}
text {
font-size: 12px;
text-anchor: middle;
}
.bar-label text {
text-anchor: start;
}
path.domain {
stroke-width: 0;
display: none;
}
.tooltip {
background: #FFFFFF;
box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.33);
font-family: "Segoe UI";
line-height: normal;
padding: 15px;
width: 400px;
position: absolute;
display: none;
}
.tooltip__container {
display: flex;
}
.tooltip::before {
content: "";
position: absolute;
left: 22px;
top: -8px;
transition: all 0.5s ease;
border: 8px solid #fff;
box-shadow: -5px -5px 5px rgba(0, 0, 0, 0.1);
transform: rotate(45deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<div class="container">
<div id="graph"></div>
</div>

Fiddle

-已编辑这应该足够通用。在2到8巴之间进行测试,似乎很到位。

.on('mouseover', d => {
div
.html(tooltipEl(d));
div
.transition()
.duration(200)
.style('display', 'block')
.style('opacity', 1);
div
.style('left', xScale(d.desc) + (xScale.bandwidth() / 2 - (barWidth - barPadding) / 2) - 1 + 'px')
.style('top', (height + margin.top + 8) + 'px');
})

最新更新