【发布时间】:2015-08-28 13:33:55
【问题描述】:
我有两个 SVG,当我 mouseover 另一个 SVG 的一个元素时,我想更改一个 SVG 的元素的属性。目前,我正在努力选择适当的元素(在代码下面更详细地解释)。这是它的 jsfiddle:jsfiddle,这是整个代码:
<!DOCTYPE html>
<html>
<head>
<title>two svgs</title>
<style>
.sweepline{
stroke:blue;
stroke-width:3;
}
#tooltip {
position: absolute;
width: 200px;
height: auto;
padding: 10px;
background-color: white;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
-webkit-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
-moz-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
pointer-events: none;
}
#tooltip.hidden {
display: none;
}
#tooltip p {
margin: 0;
font-family: sans-serif;
font-size: 16px;
line-height: 20px;
}
</style>
</head>
<body>
<div id = 'lines'></div>
<div id = 'chart'></div>
<div id="tooltip" class="hidden">
<p><strong>Name of line</strong></p>
<p>that work's: <span id="nameLine">100</span></p>
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 200
var height = 200
//names of the lines
var names = ['aaa', 'bbb', 'ccc']
//coordinates of the lines
var x1Val = [5,10,25]
var x2Val = [50,40,90]
var y1Val = [5,25,150]
var y2Val = [5,100,150]
//create SVG
var mySvg = d3.select("#lines").append("svg")
.attr("width", width)
.attr("height", height);
//add all the lines to the svg
for (i=0; i < x1Val.length; i++){
mySvg.append("line")
.attr("x1", x1Val[i])
.attr("y1", y1Val[i])
.attr("x2", x2Val[i])
.attr("y2", y2Val[i])
.attr("id", names[i])
.attr("class","sweepline")
//when 'touched', change color of line and add tooltip with name of line
.on("mouseover", function(d) {
d3.select(this).attr("class","sweepline").style("stroke", "red");
var xPosition = parseFloat(d3.select(this).attr("x1")) + 100;
var yPosition = parseFloat(d3.select(this).attr("y1")) + 50;
//Update the tooltip position and value
d3.select("#tooltip")
.style("left", xPosition + "px")
.style("top", yPosition + "px")
.select("#nameLine")
.text(d3.select(this).attr("id"));
//Show the tooltip
d3.select("#tooltip").classed("hidden", false);
})
//change the color back and hide tooltip
.on("mouseout", function() {
d3.select(this).attr("class","sweepline").style("stroke", "blue");
d3.select("#tooltip").classed("hidden", true);
})
}
//create second tooltip
var mySvg2 = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height);
mySvg2.append('circle')
.attr("cx", 30)
.attr("cy", 30)
.attr("r", 20)
.on("mouseover", function(d) {
d3.select(this).style("fill", "blue");
//d3.select('#lines').select(whatGoesInHere?).attr("class", "sweepline").style("stroke", "red");
});
</script>
</body>
</html>
首先我创建一个名为mySvg 的SVG,并分别使用x1Val、x2Val、y1Val 和y2Val 中提供的一些坐标添加线条。此 SVG 进入名为 lines 的 div。对于每一行,还有一个tooltip,它在我mouseover 时显示行的名称。一切正常。
然后我创建了第二个名为mySvg2 的SVG,它只包含一个圆圈并进入名为chart 的div。当我mouseover这个圈子时,我想将mySvg中的线条颜色更改为红色,但是我无法正确选择这些线条。我尝试了几个版本:
d3.select('#lines').select(whatGoesInHere?).attr("class", "sweepline").style("stroke", "red");
但我所有的方法都失败了。
我的问题是:当我在mouseovermySvg2 中的圆圈时,如何修改我的代码以更改mySvg 中一行或所有行的颜色?
【问题讨论】:
标签: javascript select d3.js