在函数中传递附加参数时遇到问题

问题描述 投票:0回答:1

我正在尝试在 d3 中编写一个函数,该函数将检查数据的“时间”并查看它是否在其他两个时间之间,并进行相应的过滤。

//start with a function to check if time for each data point is in between two other times
function timeInterval(data, start, end) {
    var time = data.Time

    if (start <= time <= end) {           
        return "inline";
    } else {
        return "none";
    };   
 }

//set the display of points to either inline or none
function updateTime(value) {
d3.selectAll(".events")
    .style("display", timeInterval("20:00", "24:00"));    
}


//When radio button on, call updateTime function
d3.selectAll("#timeFilterRadioButton").on("change", function() {
updateTime()
});

我的问题是我无法使用开始和结束参数调用

timeInterval
。调用
timeInterval("20:00", "24:00")
会导致时间不确定。我可以成功传递数据的唯一方法是调用不带任何参数的函数:

 function updateTime(value) {
     d3.selectAll(".events")
        .style("display", timeInterval); //calling timeInterval in this manner, I'm able to console.log the time property of the data   
 }

我哪里出错了?

javascript json function d3.js
1个回答
3
投票

只需使用匿名函数来获取数据并将您的

timeInterval
函数包装在其中:

function updateTime(value) {
    d3.selectAll(".events")
        .style("display", function(d) {
            //your datum is here---^
            return timeInterval(d, "20:00", "24:00")
            //use it here-------^
        });
};

不相关,但是这个:

if (start <= time <= end)

无论

true
,都会为所有
start <= time
返回
end

© www.soinside.com 2019 - 2024. All rights reserved.