将数据传递给d3.js中的.call
Passing data to .call in d3.js
此实验基于Health&国富的例子。我试图让一个工具提示样式的标签显示出来,并在鼠标悬停在每个点上时浮动在它们上面。每个数据元素都有一个名为"name"的属性,我想在工具提示中显示它。为了简洁起见,我省略了大部分不相关的代码。
// Create all the dots, one for each data point.
var dot = svg.append("g")
.attr("class", "dots")
.selectAll(".dot")
.data(myData)
.enter().append("circle")
.attr("class", "dot")
.call(position)
.call(enableInteraction)
.sort(order);
// Create a tooltip element.
var tooltip = svg.append("text")
.attr("class", "tooltip");
// Assign each of the dots the various mouse events.
function enableInteraction(dot) {
dot.on("mouseover", mouseover)
.on("mouseout", mouseout)
.on("mousemove", mousemove);
function mouseover() {
tooltip.text(???); // How do I get the name into here?
}
function mouseout() {
tooltip.text("");
}
function mousemove() {
var cursor = d3.mouse(this);
tooltip.attr("x", cursor[0] + 5)
.attr("y", cursor[1] + 5);
}
}
我尝试使用一个函数来检索名称,并将其传递到enableInteraction()中,如下所示:
.call(enableInteraction, function(d) { return d.name; } )
但是传递的是函数对象,而不是它的返回值。
那么,如何在工具提示中显示每个数据元素的名称呢?
您可以使用currying技术将该信息获取到鼠标悬停事件处理程序中。我不确定获取名称的语法,但这是一个想法:
// this function returns a function
function moveoverHandler(dot) {
return function mouseover() {
// I'm not sure if this is how you get the "name" property from the "dot" object
// Please update this as needed
var name = dot.data("name");
tooltip.text(name);
}
}
然后像这样连接处理程序:
dot.on("mouseover", mouseover(dot));
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- react.js中的密钥绑定
- Node.js中的JavaScript原型对象效率
- dropdown.js中的复杂事件处理
- 一个正则表达式,用于从JS中的HTML标记中删除id、样式和类属性
- JS中的按钮和文本输入
- 手动触发ember.js中的属性更改
- Node JS中的排名系统算法
- 如何在渲染视图时将变量传递给Node.js中的脚本标记
- 渲染”;a“;React.js中的可选href
- Node.js中的谷歌地图几何库
- JS中的乘法不起作用
- 你能把grunt.js中的linter改成jslint吗
- 如何将输入范围的值传递给JS中的变量
- 使用指令的angular js中的Like和different
- Angularjs:修改js中的作用域,稍后在页面中使用
- 计算php或js中的距离
- 从JS中的字符串中读取数字的库
- 纸张.js中的线条变形
- 如何使用React JS中的循环,根据条件渲染或不渲染表数据