2

因此,我尝试使用 d3 在 javascript 中加载 csv 文件,并尝试使用进度条来显示加载时的进度。这就是我所拥有的:

d3.csv("my.csv",function(data){
        //do stuff with data
    }).on("progress", function(p){
        //update progress bar
        console.log(d3.event.loaded);
    });

这就是它输出的全部内容:

13248560

这是文件中的总字节数。我想知道如何获取它已加载的值,以便我的进度条可以正确加载。有人知道如何完成这项任务吗?

4

1 回答 1

2

I think that it is normal that the event d3.event.loaded returns the total size. Did you try the following to get the progress from the variable p in the function on "progress"?

Then to get the actual progress, you can use the following question: How to get progress from XMLHttpRequest

The final result should look like something like this:

d3.csv("my.csv",function(data){
        //do stuff with data
    }).on("progress", function(event){
        //update progress bar
        if (d3.event.lengthComputable) {
          var percentComplete = Math.round(d3.event.loaded * 100 / d3.event.total);
          console.log(percentComplete);
       }
    }
});
于 2013-01-22T10:23:44.680 回答