2013-05-17 6 views
5

私はドロップダウンの選択に基づいて私のプロット内の特定のポイントを強調したいと思う基本散布図を構築しています。私のコードは次のようになります。ドロップダウンメニューに基づいてデータを返しますか?

fill_arr = fill.range(); 
labels = ["A", "B", "C", "D"]; 
options = [0, 1, 2, 3]; 

// Build the dropdown menu 
d3.select("#drop") 
    .append("select") 
    .selectAll("option") 
    .data(options) 
    .enter() 
    .append("option") 
    // Provide available text for the dropdown options 
    .text(function(d) {return labels[d];}) 

d3.select('select') 
    .on("change", function() { 
    // HOW CAN I GET THE OPTION THAT THE USER HAS SELECTED FROM THE DROPDOWN? 
    key = 0 // <- I can do this manually, but I want to get the label the user has selected 
    d3.selectAll('circle') 
     .transition() 
     .duration(300) 
     .ease("quad") 
     .attr('r', 5) 
     .attr('cx', function(d) {return d.x;}) 
     .attr('cy', function(d) {return d.y;}) 
     // if a data point is selected highlight other 
     // data points of the same color 
     .style('fill', function(d, i) { 
      if (d.label == key) 
      {return fill_arr[key]} 
      else {return "#ccc"} 
     ;}) 
    }); 

私の問題は、私は、ユーザーがドロップダウンリストから選択したかを決定する方法がわからないということです。ユーザーが選択したオプション、["A", "B", "C", "D"]を確認するにはどうすればよいですか?それはthis.selectedIndexを使用して

+0

可能なDUP:http://stackoverflow.com/questions/11903709/adding-drop-down-menu-using-d3-js助けを – mccannf

+0

感謝。私は確かに、残念ながら、ユーザーが選択したデータを取得する方法を見つけることができないような疑問を見ました。 – user1728853

+2

'key = this.selectedIndex'は機能しますか? – mccannf

答えて

3

アクセス:

d3.select('select') 
    .on("change", function() { 

    key = this.selectedIndex; 

    d3.selectAll('circle') 
     .transition() 
     .duration(300) 
     .ease("quad") 
     .attr('r', 5) 
     .attr('cx', function(d) {return d.x;}) 
     .attr('cy', function(d) {return d.y;}) 
     // if a data point is selected highlight other 
     // data points of the same color 
     .style('fill', function(d, i) { 
      if (d.label == key) 
      {return fill_arr[key]} 
      else {return "#ccc"} 
     ;}) 
}); 
関連する問題