我在d3 v4中實現了一個step-after interpolation graph。 時間被保存爲格式爲「hh:mm:ss.ss」的字符串。我通過將時間字符串轉換爲秒來傳遞timeScale整數範圍。儘管如此,我收到了「錯誤:屬性d:預期號碼」,MNaN,NaNLNaN,NaNL ......「。d3 v4 scaleTime不接受日期對象也不是整數
function render(data) {
const margin = {top: 20, right: 20, bottom: 30, left: 50};
width -= margin.left + margin.right;
height -= margin.top + margin.bottom;
const bboList = data.bboList;
bboList.push(bboList[bboList.length - 1]);
const timeStrToDate = (timeStr) => {
var time = timeStr.split(':');
let d = new Date();
d.setHours (+time[0]);
d.setMinutes(time[1]);
d.setSeconds(time[2]);
return d;
}
bboList.forEach(function(bbo) {
bbo.time = timeStringToSeconds(bbo.timeStr);
bbo.ask = +bbo.ask;
bbo.bid = +bbo.bid;
});
function timeStringToSeconds(timeString){
const hms = timeString.split(':');
return (+hms[0]) * 60 * 60 + (+hms[1]) * 60 + (+hms[2]);
}
const minTime = timeStringToSeconds(bboList[0].timeStr);
const maxTime = timeStringToSeconds(bboList[bboList.length - 1].timeStr);
let xScale = d3.scaleLinear()
.domain(d3.extent(data, function(bbo) { return bbo.time; }))
.range([minTime, maxTime]);
const yExtent = d3.extent(data, function(bbo) { return bbo.bid; });
let yScale = d3.scaleLinear()
.domain(yExtent)
.range([height, 0]);
const xAxis = d3.axisBottom().scale(xScale);
const yAxis = d3.axisLeft().scale(yScale);
chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
chart.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
const line = d3.line()
.x(function(bbo) {
return xScale(bbo.time);
})
.y(function(bbo) {
return yScale(bbo.bid);
});
chart.append("path")
.attr("class", "line")
.attr("d", line(bboList));
}
當我跟隨通過與調試器的代碼似乎每走一步我回來編號或由數字組成一BBO對象。我相信問題出在我的xScale上,但我不確定我在找什麼。任何指導我應該如何去調試這個代碼,或找到一個好的資源爲d3 v4將不勝感激(.line的文檔不清楚給我)
謝謝。
謝謝!你絕對正確的問題是d3.extent。我仍然有一些問題需要解決,但這對我來說是一個主要障礙,我非常感謝您的幫助。 –