我正在学习D3。我正在处理的示例之一包含在较旧版本的D3下编写的代码。我知道在5.7.0下数据加载的工作方式有所不同,但是对于此示例,我无法正确加载数据。当只加载数据(1个函数)时,我知道它是如何工作的,但是在此示例中,使用2个函数时,我无法解决该问题。
任何人都可以给我一些建议,以解决如何使其在最新版本的d3下工作吗? 这是代码:
function show() {
'use strict';
// load the data
var loadedData;
d3.csv('./data/businessFiltered.csv').then(
function(row) {
switch (row.yearsInBusiness) {
case "001" : row.yearsInBusinessLabel = "All"; break;
case "311" : row.yearsInBusinessLabel = "less then 2 years"; break;
case "318" : row.yearsInBusinessLabel = "2 to 3 years "; break;
case "319" : row.yearsInBusinessLabel = "4 to 5 years"; break;
case "321" : row.yearsInBusinessLabel = "6 to 10 years"; break;
case "322" : row.yearsInBusinessLabel = "11 to 15 years"; break;
case "323" : row.yearsInBusinessLabel = "more then 16 years"; break;
}
return row;
},
function (data) {
loadedData = data;
updateCircle();
}); ...
答案 0 :(得分:0)
看着the documentation for d3.csv(),看来您可以使用.get()
指定一个回调函数:
function show() {
'use strict';
// load the data
var loadedData;
d3.csv('./data/businessFiltered.csv')
.row(function(row) {
switch (row.yearsInBusiness) {
case "001" : row.yearsInBusinessLabel = "All"; break;
case "311" : row.yearsInBusinessLabel = "less then 2 years"; break;
case "318" : row.yearsInBusinessLabel = "2 to 3 years "; break;
case "319" : row.yearsInBusinessLabel = "4 to 5 years"; break;
case "321" : row.yearsInBusinessLabel = "6 to 10 years"; break;
case "322" : row.yearsInBusinessLabel = "11 to 15 years"; break;
case "323" : row.yearsInBusinessLabel = "more then 16 years"; break;
}
return row;
})
.get(function(error, data) {
loadedData = data;
updateCircle();
});
答案 1 :(得分:0)
经过更多的谷歌搜索和尝试之后,我似乎发现了在最新版本的D3(v5)中可以使用的功能:
function show() {
'use strict';
// load the data
var loadedData;
d3.csv('./data/businessFiltered.csv', function(row) {
switch (row.yearsInBusiness) {
case "001" : row.yearsInBusinessLabel = "All"; break;
case "311" : row.yearsInBusinessLabel = "less then 2 years"; break;
case "318" : row.yearsInBusinessLabel = "2 to 3 years "; break;
case "319" : row.yearsInBusinessLabel = "4 to 5 years"; break;
case "321" : row.yearsInBusinessLabel = "6 to 10 years"; break;
case "322" : row.yearsInBusinessLabel = "11 to 15 years"; break;
case "323" : row.yearsInBusinessLabel = "more then 16 years"; break;
};
return row;
})
.then(function(data) {
loadedData = data;
updateCircle();
});