简体   繁体   English

单击按钮触发D3更新模式

[英]Trigger D3 update pattern on button click

I want to use buttons to trigger D3's update loop but am not sure how to do this. 我想使用按钮来触发D3的更新循环,但不确定如何执行此操作。 I would like the first button to update with data labelled "championsleague" and the second to update with "premierleague". 我希望第一个按钮更新为带有“ championsleague”标签的数据,第二个按钮更新为“ premierleague”标签。

This is what I've tried so far 这是我到目前为止尝试过的

<button class="opts" value="championsleague">Champions League</button>
<button class="opts" value="premierleague">Premier League</button>

// handle on click event
d3.select('.opts')
  .on('click', function() {
    var data = eval(d3.select(this).property('value'));
    updateLegend(data);
  })

JSFiddle JSFiddle

You are using d3.select() which only selects the first element. 您正在使用d3.select()仅选择第一个元素。 You need to selectAll the buttons with the specified class opts . 您需要selectAll与指定类的按钮opts

Check this snippet 检查此片段

d3.selectAll('.opts')
  .on('click', function() {
    var data = eval(d3.select(this).property('value'));
    updateLegend(data);
  });

Here is a working fiddle 这是一个工作的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM