d3柱形图响应鼠标事件(二)

发现一个问题,当d3为元素添加鼠标事件时,如果元素是被包含在symbol之中的,则鼠标事件不会得到响应。直接上代码,可以看到其中的rectback可以响应,但是柱形图中的rect并未得到响应

<html>  
  <head>  
        <meta charset="utf-8">  
        <title>8.1.1 鼠标交互</title>  
  </head> 
  <style>
  
.axis path,
.axis line{
	fill: none;
	stroke: black;
	shape-rendering: crispEdges;
}
 
.axis text {
	font-family: sans-serif;
	font-size: 11px;
}

  </style>
<body>
<svg contentScriptType="text/ecmascript" width="2000" xmlns:xlink="http://www.w3.org/1999/xlink" zoomAndPan="magnify" style="background-color:#000000;" contentStyleType="text/css" viewBox="0 0 2000.0 2000.0" height="2000" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg" version="1.0">
<defs>
<symbol typename="null" statusvalue="0" statusname="非活动状态" categoryname="chart" groupname="extobj" viewBox="0 0 339 400" id="柱图_2" preserveAspectRatio="none meet" alisaname="柱图">

<rect fill="steelblue" x="30" y="280.1796407185629" width="42.857142857142854" height="89.82035928143712"></rect>
<rect fill="rgb(70, 130, 180)" x="72.85714285714286" y="292.7544910179641" width="42.857142857142854" height="77.24550898203591"></rect>
<rect fill="rgb(70, 130, 180)" x="115.71428571428571" y="154.4311377245509" width="42.857142857142854" height="215.5688622754491"></rect>
<rect fill="rgb(70, 130, 180)" x="158.57142857142856" y="213.7125748502994" width="42.857142857142854" height="156.2874251497006"></rect>
<rect fill="rgb(70, 130, 180)" x="201.42857142857142" y="192.1556886227545" width="42.857142857142854" height="177.8443113772455"></rect>
<rect fill="rgb(70, 130, 180)" x="244.28571428571428" y="70" width="42.857142857142854" height="300"></rect>
<rect fill="steelblue" x="287.1428571428571" y="114.91017964071855" width="42.857142857142854" height="255.08982035928145"></rect>
<text fill="white" font-size="14px" text-anchor="middle" x="30" y="280.1796407185629" dx="21.428571428571427" dy="1em">50</text>
<text fill="white" font-size="14px" text-anchor="middle" x="72.85714285714286" y="292.7544910179641" dx="21.428571428571427" dy="1em">43</text>
<text fill="white" font-size="14px" text-anchor="middle" x="115.71428571428571" y="154.4311377245509" dx="21.428571428571427" dy="1em">120</text>
<text fill="white" font-size="14px" text-anchor="middle" x="158.57142857142856" y="213.7125748502994" dx="21.428571428571427" dy="1em">87</text>
<text fill="white" font-size="14px" text-anchor="middle" x="201.42857142857142" y="192.1556886227545" dx="21.428571428571427" dy="1em">99</text>
<text fill="white" font-size="14px" text-anchor="middle" x="244.28571428571428" y="70" dx="21.428571428571427" dy="1em">167</text>
<text fill="white" font-size="14px" text-anchor="middle" x="287.1428571428571" y="114.91017964071855" dx="21.428571428571427" dy="1em">142</text>
<g class="axis" transform="translate(30,370)" fill="none" font-size="10" font-family="sans-serif" text-anchor="middle">
<path class="domain" stroke="#000" d="M0.5,6V0.5H300.5V6"></path><g class="tick" opacity="1" transform="translate(21.428571428571427,0)">
<line stroke="#000" y2="6" x1="0.5" x2="0.5"></line>
<text fill="#000" y="9" x="0.5" dy="0.71em">0</text>
</g>
<g class="tick" opacity="1" transform="translate(64.28571428571428,0)">
<line stroke="#000" y2="6" x1="0.5" x2="0.5"></line><text fill="#000" y="9" x="0.5" dy="0.71em">1</text></g><g class="tick" opacity="1" transform="translate(107.14285714285714,0)"><line stroke="#000" y2="6" x1="0.5" x2="0.5"></line><text fill="#000" y="9" x="0.5" dy="0.71em">2</text></g><g class="tick" opacity="1" transform="translate(149.99999999999997,0)"><line stroke="#000" y2="6" x1="0.5" x2="0.5"></line><text fill="#000" y="9" x="0.5" dy="0.71em">3</text></g><g class="tick" opacity="1" transform="translate(192.85714285714283,0)"><line stroke="#000" y2="6" x1="0.5" x2="0.5"></line><text fill="#000" y="9" x="0.5" dy="0.71em">4</text></g><g class="tick" opacity="1" transform="translate(235.7142857142857,0)"><line stroke="#000" y2="6" x1="0.5" x2="0.5"></line><text fill="#000" y="9" x="0.5" dy="0.71em">5</text></g><g class="tick" opacity="1" transform="translate(278.57142857142856,0)"><line stroke="#000" y2="6" x1="0.5" x2="0.5"></line><text fill="#000" y="9" x="0.5" dy="0.71em">6</text></g></g><g class="axis" transform="translate(30,70)" fill="none" font-size="10" font-family="sans-serif" text-anchor="end"><path class="domain" stroke="#000" d="M-6,300.5H0.5V0.5H-6"></path><g class="tick" opacity="1" transform="translate(0,300)"><line stroke="#000" x2="-6" y1="0.5" y2="0.5"></line><text fill="#000" x="-9" y="0.5" dy="0.32em">0</text></g><g class="tick" opacity="1" transform="translate(0,264.07185628742513)"><line stroke="#000" x2="-6" y1="0.5" y2="0.5"></line><text fill="#000" x="-9" y="0.5" dy="0.32em">20</text></g><g class="tick" opacity="1" transform="translate(0,228.1437125748503)"><line stroke="#000" x2="-6" y1="0.5" y2="0.5"></line><text fill="#000" x="-9" y="0.5" dy="0.32em">40</text></g><g class="tick" opacity="1" transform="translate(0,192.21556886227546)"><line stroke="#000" x2="-6" y1="0.5" y2="0.5"></line><text fill="#000" x="-9" y="0.5" dy="0.32em">60</text></g><g class="tick" opacity="1" transform="translate(0,156.2874251497006)"><line stroke="#000" x2="-6" y1="0.5" y2="0.5"></line><text fill="#000" x="-9" y="0.5" dy="0.32em">80</text></g><g class="tick" opacity="1" transform="translate(0,120.35928143712576)"><line stroke="#000" x2="-6" y1="0.5" y2="0.5"></line><text fill="#000" x="-9" y="0.5" dy="0.32em">100</text></g><g class="tick" opacity="1" transform="translate(0,84.4311377245509)"><line stroke="#000" x2="-6" y1="0.5" y2="0.5"></line><text fill="#000" x="-9" y="0.5" dy="0.32em">120</text></g><g class="tick" opacity="1" transform="translate(0,48.50299401197606)"><line stroke="#000" x2="-6" y1="0.5" y2="0.5"></line><text fill="#000" x="-9" y="0.5" dy="0.32em">140</text></g><g class="tick" opacity="1" transform="translate(0,12.574850299401191)"><line stroke="#000" x2="-6" y1="0.5" y2="0.5"></line><text fill="#000" x="-9" y="0.5" dy="0.32em">160</text></g></g>
</symbol>
<symbol typename="null" statusvalue="0" statusname="非活动状态" categoryname="chart" groupname="extobj" id="柱图_3" viewBox="0 0 342 244" preserveAspectRatio="none meet" alisaname="柱图">

</symbol>
</defs>
<rect id="backrect" x="565" width="360" y="60.00001525878906" height="260.59869384765625" style="stroke-width:1.0;stroke:none;" fill="#f3efef" transform=""/>
<use id="use2" typename="null" diagram-rtdbids="er:e:der" xmlns:xlink="http://www.w3.org/1999/xlink" style="shape-rendering:auto;" minValue="0" minvalue="0" transform="" width="342.9999694824219" xlink:show="embed" xlink:type="simple" maxvalue="150" maxValue="150" symbolname="柱图" height="244.89190673828125" diagram-xnames="IA:IB:Ic" groupname="extobj" categoryname="chart" x="577" y="74.10810089111328" xlink:href="#柱图_3" xlink:actuate="onLoad"/>
<rect x="94" width="360" height="260.59869384765625" y="60.00001525878906" style="stroke-width:1.0;fill:#f3efef;stroke:none;" transform=""/>
<use id="use1" typename="null" diagram-rtdbids="11:12:13:15:16" xmlns:xlink="http://www.w3.org/1999/xlink" style="shape-rendering:auto;" minValue="-100" minvalue="-100" width="339.8670654296875" xlink:show="embed" xlink:type="simple" maxValue="150" maxvalue="150" symbolname="柱图" diagram-xnames="UA:UB:UC:UAB:UBC" height="260.6965026855469" groupname="extobj" categoryname="chart" x="108.72796630859375" y="66.76885986328125" xlink:href="#柱图_2" xlink:actuate="onLoad"/>
</svg>
<script src="../../d3/d3.v4.js" charset="utf-8"></script> 

<script>  

var dataset = [50, 43, 120, 87, 99, 167, 142];

var width  = 400;	//SVG绘制区域的宽度
var height = 400;	//SVG绘制区域的高度
	
var svg = d3.select("#柱图_2");	//选择<body>

//alert(svg.selectAll("rect"));
svg.selectAll("rect").each(function(){

d3.select(this).on("mouseover",function(d,i){
					d3.select(this)
					  .attr("fill","yellow");
				});
				
})

d3.select("#backrect")
             .on("mouseover",function(d,i){
					d3.select(this)
					  .attr("fill","yellow");
				});
</script> 

</body>

</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值