我对以下代码有疑问。鼠标悬停的图形周围应出现黄色边框。'mouseover'/'mouseout' 事件仅针对 Chart 类的最后一个实例触发。将鼠标悬停在 Chart 的第一个和第二个实例上会在 Chart 的最后一个实例上产生 mouseover/mouseout 事件。我在 Firefox、Safari 和 Chrome 下测试了这段代码,结果相同。为什么-我做错了什么?
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta charset="utf-8">
</head>
<style>
rect { fill: #fff0e0; stroke: #000; stroke-width: 2.0px; }
.line { fill: none; stroke: steelblue; stroke-width: 3.0px; }
</style>
<body>
<script src="d3.v2.js"></script>
<script>
var kev_vs_rho= [{
values: [{x: 0.01, y: 0.2058},{x: 0.03, y: 0.2039},{x: 0.99, y: 23.2020}] }];
kev_vs_rho.minX=0.01; kev_vs_rho.maxX=0.99;
kev_vs_rho.minY=0.01; kev_vs_rho.maxY=33.66;
</script>
<script>
var kev_vs_sec= [{
values: [{x: 1.5, y: 0.2058},{x: 9.494, y: 1.6468},{x: 1000.0, y:23.4699}] }];
kev_vs_sec.minX=1.50; kev_vs_sec.maxX=1000.00;
kev_vs_sec.minY=0.01; kev_vs_sec.maxY=33.66;
</script>
<div id="chart1"> </div> <hr/>
<div id="chart2"> </div> <hr/>
<div id="chart3"> </div>
<script>
"use strict";
function Chart ( _width, _height, _data, _anchor ) {
self = this;
this.data = _data;
this.anchor = _anchor;
this.margin = {top: 30, right: 30, bottom: 30, left: 80};
this.width = _width - this.margin.left - this.margin.right;
this.height = _height - this.margin.top - this.margin.bottom;
this.xscale = d3.scale.linear()
.domain([this.data.minX, this.data.maxX])
.range([0, this.width]);
this.yscale = d3.scale.linear()
.domain([this.data.minY, this.data.maxY])
.range([this.height, 0]);
this.lineA = d3.svg.line()
.x(function (d) { return self.xscale(d.x); })
.y(function (d) { return self.yscale(d.y); });
this.div = d3.select(this.anchor).append("div");
this.svg = this.div.append("svg")
.datum(this.data[0].values)
.attr("width", this.width + this.margin.left + this.margin.right)
.attr("height", this.height + this.margin.top + this.margin.bottom)
.append("g")
.attr("transform", "translate(" + this.margin.left + "," + this.margin.top + ")");
this.svg.append("rect")
.attr("width", this.width)
.attr("height", this.height);
this.lineB = this.svg.append("path")
.attr("class", "line")
.attr("d", self.lineA);
d3.select(this.anchor)
.on("mouseover", function () {
self.div.style("background", "yellow");
})
.on("mouseout", function () {
self.div.style("background", null);
})
};
Chart.prototype.redraw = function() {
this.lineB
.datum(this.data[0].values)
.attr("d", this.lineA);
};
var chart3 = new Chart( 960, 200, kev_vs_rho, "#chart3");
var chart2 = new Chart( 960, 200, kev_vs_sec, "#chart2");
var chart1 = new Chart( 960, 200, kev_vs_rho, "#chart1");
chart1.redraw();
chart2.redraw();
chart3.redraw();
</script>
</body>
</html>