xAxis 具有日期的序数比例。 dateLabelMap 包含预先计算的刻度日期的标签。当我将tickValues设置为 dateLabelMap 的键 - 日期 - tickFormat函数中的 d 有时是不在Object.keys中的日期( dateLabelMap )。这是怎么回事? tickFormat不完全依赖于 d 值的tickValues吗?如何修复此问题,以便 d 的每个值都是Object.keys( dateLabelMap )的每个值?
xAxis
.scale(x)
.tickValues(Object.keys(dateLabelMap))
.tickFormat(function(d,i) {
if (d in dateLabelMap) {
return dateLabelMap[d];
}
return "";
});
编辑:示例:如果我将代码更改为以下内容:
var correctDates = [];
var extraDates = [];
xAxis
.scale(x)
.tickValues(Object.keys(dateLabelMap))
.tickFormat(function(d,i) {
if (d in dateLabelMap) {
correctDates.push(d);
return dateLabelMap[d];
}
extraDates.push(d);
return "";
});
在最后一次迭代之后,我得到: Object.keys( dateLabelMap )包含:
0: "Fri Nov 01 2013 00:00:00 GMT-0400 (Eastern Daylight Time)"
1: "Sun Dec 01 2013 00:00:00 GMT-0500 (Eastern Standard Time)"
2: "Tue Dec 31 2013 00:00:00 GMT-0500 (Eastern Standard Time)"
3: "Sat Feb 01 2014 00:00:00 GMT-0500 (Eastern Standard Time)"
4: "Fri Feb 28 2014 00:00:00 GMT-0500 (Eastern Standard Time)"
5: "Tue Apr 01 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
6: "Wed Apr 30 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
7: "Fri May 30 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
8: "Thu Jun 26 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
9: "Tue Jul 29 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
10: "Fri Aug 29 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
11: "Wed Oct 01 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
12: "Thu Oct 30 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
13: "Wed Nov 26 2014 00:00:00 GMT-0500 (Eastern Standard Time)"
14: "Mon Dec 22 2014 00:00:00 GMT-0500 (Eastern Standard Time)"
15: "Thu Jan 29 2015 00:00:00 GMT-0500 (Eastern Standard Time)"
16: "Thu Feb 19 2015 00:00:00 GMT-0500 (Eastern Standard Time)"
correctDates 包含:
0: "Sun Dec 01 2013 00:00:00 GMT-0500 (Eastern Standard Time)"
1: "Tue Dec 31 2013 00:00:00 GMT-0500 (Eastern Standard Time)"
2: "Sat Feb 01 2014 00:00:00 GMT-0500 (Eastern Standard Time)"
3: "Fri Feb 28 2014 00:00:00 GMT-0500 (Eastern Standard Time)"
4: "Tue Apr 01 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
5: "Wed Apr 30 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
6: "Fri May 30 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
7: "Thu Jun 26 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
8: "Tue Jul 29 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
9: "Thu Oct 30 2014 00:00:00 GMT-0400 (Eastern Daylight Time)"
10: "Wed Nov 26 2014 00:00:00 GMT-0500 (Eastern Standard Time)"
11: "Mon Dec 22 2014 00:00:00 GMT-0500 (Eastern Standard Time)"
12: "Thu Jan 29 2015 00:00:00 GMT-0500 (Eastern Standard Time)"
13: "Thu Feb 19 2015 00:00:00 GMT-0500 (Eastern Standard Time)"
extraDates 包含:
0: Sat Nov 16 2013 00:00:00 GMT-0500 (Eastern Standard Time)
1: Thu Jul 09 2015 00:00:00 GMT-0400 (Eastern Daylight Time)
是否 correctDates 与Object.keys( dateLabelMap )相同且 extraDates 不存在?