Javascript d3.js刷涂区域映射到域值
我按照一些简单的值使用d3.js创建折线图。只是遇到一些关于刷区域“映射”到域值的问题Javascript d3.js刷涂区域映射到域值,javascript,d3.js,Javascript,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
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
}
在刷式中,s是两个元素数组,表示屏幕选择的x_开始和x_结束
Console.log
显示s.map(x.invert,x)
将s
中的范围值反转为域值
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
}
s.map(x.invert,x)
是如何工作的
为什么不使用
s.map(x.invert)
?它似乎也在做同样的事情。括号中的x
的目的是什么?你几乎是对的。这与d3
无关,而是与。现在,文档所说的是它接受两个参数,一个函数回调
,和thisArg
let new_array=arr.map(函数回调(currentValue[,index[,array])){
//新数组的返回元素
}[,thisArg])
thisArg
是可选的,但是当给定时,this
在回调中指向它。在本例中,这意味着this===x
。我已经检查了源代码,d3
在它们的任何函数中都不使用此
。反转
函数,因此无论哪种方式都是安全的