我有一个多线图,鼠标悬停上的每个点都显示工具提示。在鼠标移出时,工具提示会消失,并带有一些过渡效果。转换适用于鼠标悬停,但不适用于鼠标移出。这是代码段
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
to_graph.forEach(function(d) {
svg.selectAll("dot")
.data(d.data)
.enter()
.append("svg:circle")
.attr("r", 5)
.attr("cx", function(v) {
return x(v[0]);
})
.attr("cy", function(v) {
return y(v[1]);
})
.attr("stroke", "black")
.attr("stroke-width", "2")
.attr("fill", "white")
.on("mouseover", function(v) {
div.transition()
.duration(200)
.style("opacity", '.9');
var text = formatTime(v[0]) + "<br/>" + v[1] + "<br/>" + d.label;
div.html(text)
.style("left", (d3.event.pageX)+"px")
.style("top", (d3.event.pageY-28) + "px")
.attr("fill", "steelblue")
//.style("opacity", '.9'); <---- NOT USEFUL
})
.on("mouseout", function() {
console.log(div); // <----- div is defined
div.transition().duration(200)
.style("opacity", "0");
//d3.select(this)
// .attr("fill", "transparent");
});
});
我可能会遗漏一些非常明显的东西。当我删除mouseout上的转换时,它可以正常工作,但转换时不会。
谢谢!
以下是完整(简化)的图形代码:
// general format
var graph_data = {"data": [y1, y2, ...], "alerts": [null, y2, null, y3...]};
// establish margin and padding
var margin = {
top: 0,
right: 0,
bottom: 0,
left: 0
};
var width = 1000 - margin.left - margin.right;
var height = 400 - margin.top - margin.bottom;
var xpadding = 70,
ypadding = 70;
// svg to hold the graph
var svg = d3.select("#"+container)
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// x-axis scale and min-max values
var x = d3.time.scale()
.range([xpadding, width-xpadding*2])
.domain([dateFrom, dateTo]);
// y-axis scale and min-max values
var y = d3.scale.linear()
.range([height-ypadding, ypadding])
.domain([
0,
d3.max(graph_data.data)
]);
// xaxis
var xaxis = d3.svg.axis()
.scale(x)
.orient("bottom");
// yaxis
var yaxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (height - xpadding) + ")")
.call(xaxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + ypadding + ",0)")
.call(yaxis);
// points and tooltips on the chart for mouseover
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// draw alerts as dots
var formatTime = d3.time.format("%a %e %B %I %p");
var plot_data = formatData(graph_data.alerts, dateFrom);
// filter out null values in alerts
plot_data = plot_data.filter(function (d) {
return d[1] !== null;
});
svg.selectAll("dot")
.data(plot_data)
.enter()
.append("svg:circle")
.attr("r", 5)
.attr("cx", function(v) {
return x(v[0]);
})
.attr("cy", function(v) {
return y(v[1]);
})
.attr("stroke", "black")
.attr("stroke-width", "2")
.attr("fill", "red")
.on("mouseover", function(v) {
div.transition()
.duration(200)
.style("opacity", 0.9);
var text = "Alert<br/>"+ formatTime(v[0]) + "<br/>" + v[1];
div.html(text)
.style("left", (d3.event.pageX)+"px")
.style("top", (d3.event.pageY-28) + "px")
.attr("fill", "steelblue")
.style("opacity", 0.9);
})
.on("mouseout", function() {
console.log(div);
div.transition().duration(200)
.style("opacity", 0);
})
答案 0 :(得分:4)
我在http://jsfiddle.net/Xh2vj/1/略微更新了您的演示,并将转换时间设置为1000毫秒,以便更轻松地识别动画。工具提示淡入淡出 - 它对我有用:
没有太大变化,但在mouseover上删除了'即时弹出':
.on("mouseover", function(v) {
div.transition()
.duration(1000) //longer to easier be seen
.style("opacity", 0.9); //this is for the tooltip
var text = "Alert<br/>"+ formatTime(v[0]) + "<br/>" + v[1];
div.html(text)
.style("left", (d3.event.pageX)+"px")
.style("top", (d3.event.pageY-28) + "px")
.attr("fill", "steelblue");
// .style("opacity", 0.9); // not here
})
在最新的桌面浏览器上测试为Chrome 35,Safari 7和Firefox 29。 较老的Opera 12与鼠标一起挣扎,也许工具提示覆盖了点,因此鼠标不再计算。你用什么浏览器?