我在同一页面的d3中制作了两个条形图,只显示了第二个条形图。因此,用于第二个条形图的js文件在一个函数中有一个错误,即全局变量为GenericMapping->GenericAll
。
undefined
这是我的js。第二个js文件是相同的格式,除了另一个变量"收藏夹"
全局变量var file_name = "story_fandom.tsv";
var visData;
//Step 1. Once every information is loaded to the browser
$(document).ready(function(){
//Step 2. Load tsv file
d3.tsv(file_name, function(error, data) {
//If no data found: throw error
if (error) throw error;
//If the data is loaded successfully, now we are ready to draw things with D3.
else{
for(var i=0;i<data.length;i++){
data[i].stories = +data[i].stories;
}
visData = data;
console.log(visData);
drawBar();
}
});
});
//Step 5. code drawBar
function drawBar(){
$(".vis2").empty();
var data = [];
for(var i=0;i< visData.length;i++){
temp={};
temp.color = visData[i].color;
temp.fandom = visData[i].fandom;
temp.amount = visData[i].stories;
data.push(temp);
}
在visData
的for循环中显示undefined
。我的错是什么?
继承人jsfiddle https://jsfiddle.net/khyatijparekh/dmx2nun7/
答案 0 :(得分:0)
检查文件是否实际传递数据。我猜测在一个案例中回调中的数据对象是未定义的。
//Step 2. Load tsv file
d3.tsv(file_name, function(error, data) {
//New debug line
console.log('data is' + data);
//If no data found: throw error
if (error) throw error;
//If the data is loaded successfully, now we are ready to draw things with D3.
else{
for(var i=0;i<data.length;i++){
data[i].stories = +data[i].stories;
}
visData = data;
console.log(visData);
drawBar();
}
});