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

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

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
Sign up to request clarification or add additional context in comments.

4 Comments

Hey thank you again, but I'm now getting a warning that I have code that is unreachable after my return statement. How can I fix this?
You have to post a minimum working version of your code, in a Plunker/CodePen/whatever...
hoping I got the formatting right... I do see the unreachable code after return error in the console, so it should be okay: jsfiddle.net/jw7wc650/3
Well I put my return in a map function map and that seems to have solved my issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.