1
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.

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

Frederic Bastiat
  • 695
  • 4
  • 12
  • 31

1 Answers1

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)...

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("Sales Export Friendly 3-19-17.csv", function(error, data) {
    console.log(data)//this works 
});

console.log(data)//this will not work
Community
  • 1
  • 1
Gerardo Furtado
  • 100,839
  • 9
  • 121
  • 171