且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用csv读取日期解析

更新时间:2023-11-26 23:01:58

由于您使用的是D3 v3.x,因此没有属性添加到数据数组(请看我在这里的答案: http://***.com/ a / 42994150/5768908 )。



但是,解决方案很简单。只需替换:

  dimension(data.columns)

与:

 维度(d3.keys(data [0])) 

这是一个演示:



  var data = d3.csv.parse(d3.select(#csv).text()); console.log(d3.keys (data [0]));  

  none;}  

 < script src =https: /d3js.org/d3.v3.min.js\"></script><pre id =csv> BookingID,Type,Status,Unit Booked,Unit Owner,Destination,Booking Date,Checkin,Checkout ,租户 - 名字,租客 - 姓氏,租房者 - 电子邮件地址,租房者 - 工作电话,租赁 - 家庭电话,#成人,#儿童,总逗留,总付款,总计到期156428 89,房屋,确认,GV T3#2106 ,,, 3/20/2016 7:00:00 PM,3/23/2016 3:00 PM PM,3/28/2016 11:00:00 AM,FirstName ,LastName,first& last @ gmail.com,+ 1(000)000-0000,+ 1(000)000-0000,2,0,895,895,0< / pre>  


Having a very hard time loading in my data correctly. Here is the header row with the first content row:

BookingID,Type,Status,Unit Booked,Unit Owner,Destination,Booking Date,Checkin,Checkout,Renter - FirstName,Renter - LastName,Renter - EmailAddress,Renter - WorkPhone,Renter - HomePhone,#Adults,#Children,Total Stay,Total Paid,Total Due
15642889,House,Confirmed,GV T3 #2106,,,3/20/2016 7:00:00 PM,3/23/2016 3:00:00 PM,3/28/2016 11:00:00 AM,FirstName,LastName,first&last@gmail.com,+1 (000) 000-0000,+1 (000) 000-0000,2,0,895,895,0

And the relevant lines of loading in and parsing my csv as I want:

var parseDate = d3.time.format("%m/%d/%Y %H:%M:%S %p").parse;
var data = d3.csv("Sales Export Friendly 3-19-17.csv", function(data) {
    return {
        unit: data["Unit Booked"],
        date: parseDate(data["Booking Date"]).getMonth() + 1,
        checkin: parseDate(data["Checkin"]).getMonth() + 1,
        LOS: parseDate(data["Checkout"]).valueOf() - parseDate(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))
        };
});

The idea is that I will then do something like this:

d3.parcoords()("#TopLeft").alpha(0.4)
        .data(data)
        .dimensions(data.columns)

If I try to console.log(data.columns); after my callback function, I get undefined. Here is what console.log(data); prints, which is quite odd looking:

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() }

And the error codes I am currently getting:

TypeError: data.slice is not a function

When calling data(data) on d3.parcoords and

TypeError: e is undefined

On this line:

checkin: parseDate(data["Checkin"]).getMonth() + 1,

I am terribly confused as to what is going wrong here. I am using d3 v3.

Since you are using D3 v3.x, there is no columns property added to the data array (have a look at my answer here: http://***.com/a/42994150/5768908).

However, the solution is simple. Just replace:

dimensions(data.columns)

With:

dimensions(d3.keys(data[0]))

Here is a demo:

var data = d3.csv.parse(d3.select("#csv").text());

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

pre{
  display: none;
}

<script src="https://d3js.org/d3.v3.min.js"></script>
<pre id="csv">BookingID,Type,Status,Unit Booked,Unit Owner,Destination,Booking Date,Checkin,Checkout,Renter - FirstName,Renter - LastName,Renter - EmailAddress,Renter - WorkPhone,Renter - HomePhone,#Adults,#Children,Total Stay,Total Paid,Total Due
15642889,House,Confirmed,GV T3 #2106,,,3/20/2016 7:00:00 PM,3/23/2016 3:00:00 PM,3/28/2016 11:00:00 AM,FirstName,LastName,first&last@gmail.com,+1 (000) 000-0000,+1 (000) 000-0000,2,0,895,895,0</pre>