问题描述
很难正确加载数据。以下是第一个内容行的标题行: BookingID,类型,状态,单位预订,单位所有者,目的地,预订日期,签到,结帐,租借者 - 名字,租客 - 姓氏,租房人 - 电子邮件地址,租借者 - 工作电话,租用 - 家庭电话,#成人,#儿童,总逗留,总付款,总额到期
15642889, GV T3#2106 ,,, 3/20/2016 7/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
根据需要加载和解析我的csv的相关行:
var parseDate = d3 .time.format(%m /%d /%Y%H:%M:%S%p)。
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 [ ],
avgNight:(+ data [Total Stay])/((new Date(data [Checkout]))valueOf() - new Date(data [Checkin])valueOf ))/(24 * 60 * 60 * 1000))
};
});
这个想法是,我会这样做:
d3.parcoords()(#TopLeft)alpha(0.4)
.data(data)
.dimensions(data.columns )
如果我尝试console.log(data.columns);在我的回调函数之后,我得到undefined。这是什么console.log(data);打印,这是相当奇怪的:
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()}
我目前正在收到的错误代码:
TypeError:data.slice不是函数
在d3上调用数据(数据)时。 parcoords和
TypeError:e未定义
在这一行:
checkin:parseDate(data [Checkin])。 getMonth()+ 1,
我对这里出了什么问题非常困惑。我使用d3 v3。
由于您使用的是D3 v3.x,因此没有列
属性添加到数据数组(请看我在这里的答案:)。
但是,解决方案很简单。只需替换:
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&[email protected],+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://stackoverflow.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&[email protected],+1 (000) 000-0000,+1 (000) 000-0000,2,0,895,895,0</pre>
这篇关于用csv读取日期解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!