使用 HTML 滑块更新变量

Update variable with HTML slider

这是我的代码,它将我的数据集过滤到一年,当我移动 html 滑块时,我想更新值。我有一个变量 year=1979,但是当我将它输入函数时它不起作用。

var yearData = data.filter(function(element) {return element.YEAR == "year"});

我想我遗漏了什么,谁能给我指出正确的方向?

我的fiddle:http://jsfiddle.net/vyab4kcf/6/

您呈现图表的代码仅应请求调用一次。 您需要存储请求的数据,然后执行与回调中相同的指令。

所以:

var request_data;
d3.csv('http://...', function(error, data){
  // store the data  
  request_data = data;
  // Run update for the first request
  update("1979");
});

// Listen to slider changes
d3.select("#sexYear").on("input", function() {
  update(this.value);
});  

function update(sexYear) { 
  
  // Chart rendering code
  // Do your filters on request_data here  
  
}