D3v4 timeParse始终返回null

时间:2016-12-11 12:58:57

标签: javascript json d3.js

我有这个D3js代码:

var dataset=JSON.parse('{"date": ["2016-11-28", "2016-11-29", "2016-12-01", "2016-12-02", "2016-12-03", "2016-12-04", "2016-12-05", "2016-12-06", "2016-12-07", "2016-12-08", "2016-12-09", "2016-12-10", "2016-12-11"], "bca": [{"JPY": 119, "USD": 13510, "SGD": 9455, "CNY": 1944}, {"JPY": 120, "USD": 13527, "SGD": 9491, "CNY": 1951}, {"JPY": 118, "USD": 13560, "SGD": 9459, "CNY": 1959}, {"JPY": 119, "USD": 13535, "SGD": 9498, "CNY": 1968}, {"JPY": 118, "USD": 13485, "SGD": 9470, "CNY": 1958}, {"JPY": 118, "USD": 13485, "SGD": 9470, "CNY": 1958}, {"JPY": 118, "USD": 13485, "SGD": 9470, "CNY": 1958}, {"JPY": 117, "USD": 13430, "SGD": 9435, "CNY": 1952}, {"JPY": 117, "USD": 13382, "SGD": 9426, "CNY": 1944}, {"JPY": 116, "USD": 13327, "SGD": 9379, "CNY": 1931}, {"JPY": 117, "USD": 13278, "SGD": 9375, "CNY": 1926}, {"JPY": 116, "USD": 13322, "SGD": 9360, "CNY": 1923}, {"JPY": 116, "USD": 13322, "SGD": 9360, "CNY": 1923}], "maybank": [{"JPY": 119, "USD": 13510, "SGD": 9463, "CNY": 1945}, {"JPY": 120, "USD": 13520, "SGD": 9487, "CNY": 1951}, {"JPY": 119, "USD": 13550, "SGD": 9509, "CNY": 1964}, {"JPY": 118, "USD": 13540, "SGD": 9466, "CNY": 1961}, {"JPY": 118, "USD": 13490, "SGD": 9477, "CNY": 1961}, {"JPY": 118, "USD": 13490, "SGD": 9477, "CNY": 1961}, {"JPY": 118, "USD": 13490, "SGD": 9477, "CNY": 1961}, {"JPY": 117, "USD": 13420, "SGD": 9428, "CNY": 1951}, {"JPY": 117, "USD": 13380, "SGD": 9426, "CNY": 1944}, {"JPY": 116, "USD": 13330, "SGD": 9373, "CNY": 1931}, {"JPY": 116, "USD": 13270, "SGD": 9368, "CNY": 1926}, {"JPY": 116, "USD": 13320, "SGD": 9358, "CNY": 1923}, {"JPY": 116, "USD": 13320, "SGD": 9358, "CNY": 1923}]}');
var svg = d3.select(".tes").append("svg").attr("width", 800).attr("height", 450);
var parsetime = d3.timeParse("%y-%m-%d");
var ya=[];
for (var i=0;i<dataset['date'].length;i++){
     ya.push({"x":parsetime(dataset['date'][i]),"y":dataset['maybank'][i]['JPY']});
}
var xS = d3.scaleTime().range([0,750]);
var yS = d3.scaleLinear().range([400,0]);
var valueline = d3.line().x(function(d){return xS(d.x);}).y(function(d){return yS(d.y);});
xS.domain([d3.extent(ya, function(d) { return d.x; })]);
yS.domain([0, d3.max(ya, function(d) { return d.y; })]);
svg.append("path").data([ya]).attr("class","line").attr("d", valueline);
svg.append('g').attr("transform", "translate(35,"+ 400 +")").call(d3.axisBottom(xS));
svg.append("g").attr("transform", "translate(" + 35 + ",0)").call(d3.axisLeft(yS));

我发现parseTime总是返回null。因此无法创建折线图。 parseTime是否必须在d3数据函数内,例如d3.tsv,d3.json等?但是根据我的json数据,很难做到。或任何其他建议使其有效?

1 个答案:

答案 0 :(得分:1)

您的格式说明符 [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] public global::System.Collections.Specialized.NameValueCollection strColExcelFamNames { get { return ((global::System.Collections.Specialized.NameValueCollection)(this["strCol"])); } set { this["strCol"] = value; } } 不正确。

根据D3 documentation%y-%m-%d是2位数年份的指令,%y是4位数年份的指令。您使用的是4位数年份,因此您需要%Y而不是%Y