如何解决d3.js刷过的区域映射到域值
我遵循一些简单的值来使用d3.js创建折线图。只是遇到了一些有关拉丝区域“映射”到域值的问题。
var x = d3.scaleLinear().domain([-100,100]).range([0,500]),//domain and range for example
var brush = d3.brushX() // Add the brush feature using the d3.brush function
.extent( [ [0,0],[width,height2] ] ) // initialise the brush area
.on("brush end",brushed); // trigger the brushed function
function brushed() {
var s = d3.event.selection; //get the selection pixel range on screen,[200,300] for example
x.domain(s.map(x.invert,x)); // how does the s.map(x.invert,x) work
//then update the chart with the new domain
}
在brushed
中,s是两个元素数组,代表屏幕选择的x_start和x_end。
Console.log
将s.map(x.invert,x)
中的s
范围值转换为域值。
s.map(x.invert,x)
如何工作?
为什么不使用s.map(x.invert)
?似乎做同样的事情。括号中x
的目的是什么?
解决方法
您几乎是正确的。这与d3
无关,但与Array.prototype.map
无关。现在,文档对此说的是它接受两个参数,一个函数callback
和thisArg
。
let new_array = arr.map(function callback( currentValue[,index[,array]]) {
// return element for new_array
}[,thisArg])
thisArg
是可选的,但在给定的情况下,回调内的this
指向它。在这种情况下,这意味着this === x
。我已经检查了源代码,并且d3
在其任何this
函数中均未使用.invert
,所以无论哪种方式都是安全的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。