0
我有兩個鏈接到相同數據源的條形圖。將鼠標懸停在影響其他圖表的圖表上d3.js
當我將鼠標懸停在第一個圖表上的一個條上時,我的目標是能夠影響第二個圖表上的關聯條(例如突出顯示這些條)。
目標與here相似。但使用我現有的代碼,只要將鼠標懸停在某個圖表上的某個條上,就會突出顯示兩個圖表中的所有條形圖。
有沒有人有解決這個問題?由於
這裏是我的代碼:
<!DOCTYPE html>
<html>
<head>
<style>
#chart_01 {
height: 40px;
position: relative;
width: 360px;
}
#chart_02 {
height: 40px;
position: relative;
width: 360px;
}
</style>
<meta charset = "UTF-8">
<script src = "https://d3js.org/d3.v3.min.js" charset = "utf-8"></script>
</head>
<body>
<div id = "chart_01">
<h2>Chart 01</h2>
<script>
var source = [{
x: 3,
y: 6
}, {
x: 8,
y: 40
}, {
x: 9,
y: 10
}];
var canvas_01 = d3.select("body")
.append("svg")
.attr("width", 500)
.attr("height", 200)
canvas_01.selectAll(".sweetpoint")
.data(source)
.enter()
.append("rect")
.classed("sweetpoint", true)
.attr("width", function(data_){return data_.x * 40;})
.attr("height", 10)
.attr("y", function(data_, index_){return index_ * 30;})
.attr("fill", "#e4e4e4")
.on("mouseover", function(data_, index_){
d3.selectAll(".sweetpoint").style("fill", "#696969");
})
.on("mousemove", function(data_, index_){
})
.on("mouseout", function(data_, index_){
d3.selectAll(".sweetpoint").style("fill", "#e4e4e4");
})
</script>
</div>
<div id = "chart_02">
<h2>Chart 02</h2>
<script>
var canvas_02 = d3.select("body")
.append("svg")
.attr("width", 500)
.attr("height", 200)
canvas_02.selectAll(".sweetpoint")
.data(source)
.enter()
.append("rect")
.classed("sweetpoint", true)
.attr("width", function(data_){return data_.x * 10;})
.attr("height", 10)
.attr("y", function(data_, index_){return index_ * 30;})
.attr("fill", "#e4e4e4")
.on("mouseover", function(data_, index_){
d3.selectAll(".sweetpoint").style("fill", "#696969");
})
.on("mousemove", function(data_, index_){
})
.on("mouseout", function(data_, index_){
d3.selectAll(".sweetpoint").style("fill", "#e4e4e4");
})
</script>
</div>
</body>
</html>
它!謝謝米哈伊爾。 – Fxs7576