【发布时间】:2014-04-16 15:10:45
【问题描述】:
当用户在下拉菜单中选择一个项目时,我试图让我的 d3.js 折线图重新加载,并且数据与该项目相对应。
我的菜单是股票市场价值列表:
- YHOO
- FB
- ...
对于其中的每一个,我都有一个包含数据的 JSON 文件。 该图本身正在工作。
我把代码放在一个 [JSFiddle] 中,它不起作用,因为它应该使用 d3 和 knockout.js。
使用此Github Gist 可能会更容易。
无论如何,第 83 行之后的代码会针对下拉列表中的每个选项更改 newValue。
数据存储在yahoo.json 和fb.json 中。
每次用户在下拉菜单中选择一个带有与该选项关联的数据的新选项时,如何重新加载图表?
非常感谢。
编辑:临时破解
/*User's stock choice*/
var viewModel = {
choice: ["AAPL", "YHOO", "FB", "MSFT"],
selectedChoice: ko.observable("two"), /*Knockout.js is used for having an observable array*/
};
viewModel.selectedChoice.subscribe(function(newValue) {
console.log(newValue);
if (newValue === "YHOO") {
d3.select("svg").remove();
d3.json('yahoo.json', draw);
} else if (newValue === "FB") {
d3.select("svg").remove();
d3.json('fb.json', draw);
}
});
ko.applyBindings(viewModel);
【问题讨论】:
标签: javascript json d3.js