2017-08-04 96 views
0

我想在簡單散點圖中使用滑塊(d3-slider)過濾來自csv的數據。從散點圖中的滑塊過濾csv數據D3js

我的問題是過濾器有點無政府狀態。當我使用滑塊時,它會顯示點,但不是我想要的點。我試過this但最後修改爲that code

這是我的代碼片段,問我,如果你希望所有:

d3.csv("cereal.csv", function(error, data) { 

var dateSlider = d3.select('#slider') 
    .call(d3.slider().axis(true).min(2006).max(2009).step(1) 
    .on("slide", function(evt, value) { 
     dots.each(function(d) { 
     this.style.opacity = d.Date != value ? 1 : 0; 
     }); 
    }) 
); 

    // change string (from CSV) into number format 
    data.forEach(function(d) { 
    d.Calories = +d.Calories; 
    d.Protein = +d.Protein; 
    }); 

    // don't want dots overlapping axis, so add in buffer to data domain 
    xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]); 
    yScale.domain([d3.min(data, yValue)-1, d3.max(data, yValue)+1]); 

    // x-axis 
    svg.append("g") 
     .attr("class", "x axis") 
     .attr("transform", "translate(0," + height + ")") 
     .call(xAxis) 

    // y-axis 
    svg.append("g") 
     .attr("class", "y axis") 
     .call(yAxis) 

    // draw dots 
    var dots = svg.selectAll(".dot") 
     .data(data) 
    .enter().append("circle") 
     .attr("class", "dot") 
     .attr("r", 7) 
     .attr("cx", xMap) 
     .attr("cy", yMap) 
     .style("fill", "steelblue") 
     .on("mouseover", function(d) {  
     console.log(d.Date) 
     }); 
}); 

這裏是我的CSV:

Date,CerealName,Manufacturer,Type,Calories,Protein 
2006,cereal-one,Nabisco,C,70,4 
2006,cereal-two,Quaker Oats,C,120,3 
2006,cereal-three,Kelloggs,C,74,4 
2006,cereal-four,Kelloggs,C,50,4 
2006,cereal-five,Ralston Purina,C,110,2 
2006,cereal-six,General Mills,C,110,4 
2006,cereal-seven,Kelloggs,C,110,6 
2007,cereal-height,General Mills,C,130,3 
2007,cereal-nine,Ralston Purina,C,90,2 
2007,cereal-ten,Post,C,90,3 
2007,cereal-eleven,Quaker Oats,C,120,1 
2008,cereal-twelve,General Mills,C,100,6 
2008,cereal-thirteen,General Mills,C,124,1 
2008,cereal-fourteen,General Mills,C,110,3 
2008,cereal-fifteen,General Mills,C,110,1 
2008,cereal-sixteen,Ralston Purina,C,100,1 
2009,cereal-seventeen,Kelloggs,C,100,2 
2009,cereal-heighteen,Kelloggs,C,108,1 
2009,cereal-nineteen,General Mills,C,114,1 
+0

您需要將日期轉換也編號 'data.forEach(函數(d){ d.Calories = + d.Calories; d.Protein = + d。蛋白質; d.Date = + d.Date; });' – Cyril

+0

Thx,做到了,但它沒有改變任何東西...... – JulienJ

回答

0

我設法做什麼,我想通過改變幾個事情,首先我已將日期轉換爲數字(感謝Cyril評論),並在我的.on(「幻燈片」)函數中更改了順序以使事情發揮作用。這就是現在我的代碼:

d3.csv("cereal.csv", function(error, data) { 

    var dateSlider = d3.select('#slider') 
    .call(d3.slider().axis(true).min(2006).max(2009).step(1) 
     .on("slide", function(evt, value) { 
     console.log(value); 
     dots.each(function(d) { 
      this.style.display = value != d.Date ? "none" : "inline"; 
     }); 
     }) 
    ); 

    // change string (from CSV) into number format 
    data.forEach(function(d) { 
    d.Calories = +d.Calories; 
    d.Protein = +d.Protein; 
    d.Date = +d.Date; 
    }); 

    // don't want dots overlapping axis, so add in buffer to data domain 
    xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]); 
    yScale.domain([d3.min(data, yValue)-1, d3.max(data, yValue)+1]); 

    // x-axis 
    svg.append("g") 
     .attr("class", "x axis") 
     .attr("transform", "translate(0," + height + ")") 
     .call(xAxis) 

    // y-axis 
    svg.append("g") 
     .attr("class", "y axis") 
     .call(yAxis) 

    // draw dots 
    var dots = svg.selectAll(".dot") 
     .data(data) 
    .enter().append("circle") 
     .attr("class", "dot") 
     .attr("r", 7) 
     .attr("cx", xMap) 
     .attr("cy", yMap) 
     .style("fill", "steelblue") 
     .on("mouseover", function(d) {  
     console.log(d.Date); 
     });