在我的D3折线图中,我正在尝试创建鼠标悬停效果,如下例所示:http://bl.ocks.org/mbostock/3902569
在这个例子中,作者使用平分函数,据我所知,只支持线性尺度。 问题是,在我的图表中,我有一个带有不同的离散rangePoint元组的序数x轴。因此,如果它像下面的情况(m =鼠标位置),我想得到最接近的x值的像素位置,在这个例子中将是x2。
m
|
x1----------x2----------x3
有没有办法做到这一点?
答案 0 :(得分:6)
使用您的链接示例,这里是序数量级mousemove
函数的快速实现:
var tickPos = x.range();
function mousemove(d){
var m = d3.mouse(this),
lowDiff = 1e99,
xI = null;
// if you have a large number of ticks
// this search could be optimized
for (var i = 0; i < tickPos.length; i++){
var diff = Math.abs(m[0] - tickPos[i]);
if (diff < lowDiff){
lowDiff = diff;
xI = i;
}
}
focus
.select('text')
.text(ticks[xI]);
focus
.attr("transform","translate(" + tickPos[xI] + "," + y(data[xI].y) + ")");
}
完整代码:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.overlay {
fill: none;
pointer-events: all;
}
.focus circle {
fill: none;
stroke: steelblue;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 50
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundPoints([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) {
return x(d.x);
})
.y(function(d) {
return y(d.y);
});
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var data = [{
x: 'A',
y: Math.random() * 10
}, {
x: 'B',
y: Math.random() * 10
}, {
x: 'C',
y: Math.random() * 10
}, {
x: 'D',
y: Math.random() * 10
}, {
x: 'E',
y: Math.random() * 10
}, {
x: 'F',
y: Math.random() * 10
}, {
x: 'G',
y: Math.random() * 10
}, {
x: 'H',
y: Math.random() * 10
}, {
x: 'I',
y: Math.random() * 10
}, {
x: 'J',
y: Math.random() * 10
}];
var ticks = data.map(function(d) {
return d.x
});
x.domain(ticks);
y.domain(d3.extent(data, function(d) {
return d.y;
}));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
var focus = svg.append("g")
.attr("class", "focus")
.style("display", "none");
focus.append("circle")
.attr("r", 4.5);
focus.append("text")
.attr("x", 9)
.attr("dy", ".35em")
.style('')
svg.append("rect")
.attr("class", "overlay")
.attr("width", width)
.attr("height", height)
.on("mouseover", function() {
focus.style("display", null);
})
.on("mouseout", function() {
focus.style("display", "none");
})
.on("mousemove", mousemove);
var tickPos = x.range();
function mousemove(d){
var m = d3.mouse(this),
lowDiff = 1e99,
xI = null;
for (var i = 0; i < tickPos.length; i++){
var diff = Math.abs(m[0] - tickPos[i]);
if (diff < lowDiff){
lowDiff = diff;
xI = i;
}
}
focus
.select('text')
.text(ticks[xI]);
focus
.attr("transform","translate(" + tickPos[xI] + "," + y(data[xI].y) + ")");
}
</script>
答案 1 :(得分:1)
简单解决方案:
.on("mousemove", function () {
const x0 = x.invert(d3.mouse(d3.event.currentTarget)[0]),
i = bisectDate(data, x0, 1),
d0 = data[i - 1],
d1 = data[i];
const rangeValueOfFirst = x(d0.date),
rangeValueOfSecond = x(d1.date),
rangeValueOfMousePos = d3.mouse(d3.event.currentTarget)[0],
closestD = Math.abs(rangeValueOfMousePos - rangeValueOfFirst) > Math.abs(rangeValueOfMousePos - rangeValueOfSecond) ? d1 : d0;
const focus = d3.select(".focus");
focus.attr("transform", () => "translate(" + x(closestD.date) + "," + y(closestD.close) + ")");
focus.select("text").text(closestD.close);
});