TypeError:data.slice不是加载csv d3问题的函数

时间:2022-09-28 14:01:24
var sales = d3.csv("Sales Export Friendly 3-19-17.csv", function(error, data) {
    //var parseDate = d3.time.format("%m/%d/%Y %H:%M:%S %p").parse;
    return {
        unit: data["Unit Booked"],
        date: new Date(data["Booking Date"]).getMonth() + 1,
        checkin: new Date(data["Checkin"]).getMonth() + 1,
        LOS: new Date(data["Checkout"]).valueOf() - new Date(data["Checkin"]).valueOf()/(24*60*60*1000),
        total: +data["Total Stay"],
        avgNight: (+data["Total Stay"]) / ((new Date(data["Checkout"]).valueOf() - new Date(data["Checkin"]).valueOf())/(24*60*60*1000))
        }
});

console.log(sales);
console.log(d3.keys(sales[0]));


parcoords = d3.parcoords()("#TopLeft");

parcoords

the console logging statement on sales returns

销售退货时的控制台日志记录语句

Object { header: Cn/u.header(), mimeType: Cn/u.mimeType(), responseType: Cn/u.responseType(), response: Cn/u.response(), get: Cn/</u[n](), post: Cn/</u[n](), send: Cn/u.send(), abort: Cn/u.abort(), on: M/<(), row: e/o.row() }

I'm not sure how this weird object happened.

我不确定这个奇怪的物体是怎么发生的。

And the following console.log statement returns an empty array.

以下console.log语句返回一个空数组。

Last, I get a TypeError: data.slice is not a function when calling parcoords

最后,我得到一个TypeError:data.slice在调用parcoords时不是函数

1 个解决方案

#1


2  

Until your ante-penultimate question, you were doing it right:

直到你的倒数第二个问题,你做得对:

d3.csv(url, function(data){
    //code here
});

Then, I don't know why, in your previous question (only now I'm noticing it) and in this question, you started doing this:

然后,我不知道为什么,在你之前的问题中(只是现在我注意到了)并且在这个问题中,你开始这样做:

var data = d3.csv(url, function(data){
    //the rest of the code

Which will not work.

哪个不行。

This is the problem: d3.csv doesn't return anything! Technically speaking, it returns an object related to the request (run the snippet to see it)...

这是问题:d3.csv不返回任何内容!从技术上讲,它返回一个与请求相关的对象(运行代码段来查看它)...

var test = d3.csv("https://gist.githubusercontent.com/mbostock/3887051/raw/805adad40306cedf1a513c252ddd95e7c981885a/data.csv", function(data){
});

console.log(test);
<script src="https://d3js.org/d3.v4.min.js"></script>

... which is not what you want.

......这不是你想要的。

Thus, change back to what you were doing...

因此,改回你正在做的事情......

d3.csv("Sales Export Friendly 3-19-17.csv", function(error, data) {
    //code here
})

... and drop that var sales.

...并删除该销售额。

Also, have in mind that d3.csv is asynchronous. So, you have to console.log your variables inside the callback:

另外,请记住d3.csv是异步的。所以,你必须在回调中使用console.log你的变量:

d3.csv("Sales Export Friendly 3-19-17.csv", function(error, data) {
    console.log(data)//this works 
});

console.log(data)//this will not work

#1


2  

Until your ante-penultimate question, you were doing it right:

直到你的倒数第二个问题,你做得对:

d3.csv(url, function(data){
    //code here
});

Then, I don't know why, in your previous question (only now I'm noticing it) and in this question, you started doing this:

然后,我不知道为什么,在你之前的问题中(只是现在我注意到了)并且在这个问题中,你开始这样做:

var data = d3.csv(url, function(data){
    //the rest of the code

Which will not work.

哪个不行。

This is the problem: d3.csv doesn't return anything! Technically speaking, it returns an object related to the request (run the snippet to see it)...

这是问题:d3.csv不返回任何内容!从技术上讲,它返回一个与请求相关的对象(运行代码段来查看它)...

var test = d3.csv("https://gist.githubusercontent.com/mbostock/3887051/raw/805adad40306cedf1a513c252ddd95e7c981885a/data.csv", function(data){
});

console.log(test);
<script src="https://d3js.org/d3.v4.min.js"></script>

... which is not what you want.

......这不是你想要的。

Thus, change back to what you were doing...

因此,改回你正在做的事情......

d3.csv("Sales Export Friendly 3-19-17.csv", function(error, data) {
    //code here
})

... and drop that var sales.

...并删除该销售额。

Also, have in mind that d3.csv is asynchronous. So, you have to console.log your variables inside the callback:

另外,请记住d3.csv是异步的。所以,你必须在回调中使用console.log你的变量:

d3.csv("Sales Export Friendly 3-19-17.csv", function(error, data) {
    console.log(data)//this works 
});

console.log(data)//this will not work