在 D3 中如何为当前特定路径启用鼠标悬停事件?
In D3 How to enable mouse over event for current particular path?
我正在使用 d3.js
创建世界地图。在该地图中,我需要为每个国家/地区绑定 mouseover
事件。
例如: 如果我 mouseover
印度,我需要仅更改印度的填充(背景)颜色。
我实施了 mouseover
event.But 我的问题是每当我 mouseover
在国家(印度)影响所有 countries.I 的功能意味着填充颜色影响所有countries.But只需要影响当前国家
我也尝试过使用 this
,但没有成功。
.on("mouseover", function(){d3.select(this).style("fill", "aliceblue");})
请帮助任何人解决我的问题。
我的完整代码
var width = 1000,
height = 500;
var projection = d3.geo.robinson()
.scale(150)
//.translate(100,100)
.precision(.5);
var path = d3.geo.path()
// .attr("class","path")
.projection(projection);
var svg = d3.select('#'+id)
.append('svg')
.attr("width", width)
.attr("height", height)
.attr("style", "background:" + json.bc);
//shape
d3.json("world.json", function(error, world) {
svg
.datum(topojson.feature(world, world.objects.countries))
.append("path")
.on("mouseover", function(){d3.select(this).style("fill", "red");})
.on("mouseout", function(){d3.select(this).style("fill", "white");})
.attr("style", "fill:" + json.cbc)
.attr("class", "country")
.attr("d", path)
;
});
鼠标悬停前
鼠标悬停后
此代码:
svg
.datum(topojson.feature(world, world.objects.countries))
.append("path")
...
says --> 我有一个数据,从中画一条路径。
改成这样:
svg.selectAll(".countries")
.data(topojson.feature(world, world.objects.countries).features)
.enter()
.append("path")
...
这表示 --> 我有多个数据(特征),将数据绑定到我的选择 (selectAll) 并为每个组件画一条路径。
例子here.
我正在使用 d3.js
创建世界地图。在该地图中,我需要为每个国家/地区绑定 mouseover
事件。
例如: 如果我 mouseover
印度,我需要仅更改印度的填充(背景)颜色。
我实施了 mouseover
event.But 我的问题是每当我 mouseover
在国家(印度)影响所有 countries.I 的功能意味着填充颜色影响所有countries.But只需要影响当前国家
我也尝试过使用 this
,但没有成功。
.on("mouseover", function(){d3.select(this).style("fill", "aliceblue");})
请帮助任何人解决我的问题。
我的完整代码
var width = 1000,
height = 500;
var projection = d3.geo.robinson()
.scale(150)
//.translate(100,100)
.precision(.5);
var path = d3.geo.path()
// .attr("class","path")
.projection(projection);
var svg = d3.select('#'+id)
.append('svg')
.attr("width", width)
.attr("height", height)
.attr("style", "background:" + json.bc);
//shape
d3.json("world.json", function(error, world) {
svg
.datum(topojson.feature(world, world.objects.countries))
.append("path")
.on("mouseover", function(){d3.select(this).style("fill", "red");})
.on("mouseout", function(){d3.select(this).style("fill", "white");})
.attr("style", "fill:" + json.cbc)
.attr("class", "country")
.attr("d", path)
;
});
鼠标悬停前
鼠标悬停后
此代码:
svg
.datum(topojson.feature(world, world.objects.countries))
.append("path")
...
says --> 我有一个数据,从中画一条路径。
改成这样:
svg.selectAll(".countries")
.data(topojson.feature(world, world.objects.countries).features)
.enter()
.append("path")
...
这表示 --> 我有多个数据(特征),将数据绑定到我的选择 (selectAll) 并为每个组件画一条路径。
例子here.