gpt4 book ai didi

javascript - 图表.js :set yAxis point to 0 when there is gap between two dates

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:20 25 4
gpt4 key购买 nike

我使用的是 Chart.js,在我的 xAxis 中,我有一些日期数组,其中包含 [2016:08:06,2016:08:10] 之类的间隔及其匹配值 [20,40]

问题是 Chart.js 显示给定日期数组之间的天数。我不想将我的数组设置为 [20,0,0,0,40],因为我有 3 天的间隔。我怎样才能自动将它们在 yAxis 中的匹配值设置为 0。

最佳答案

不久前我遇到了同样的问题,并通过编写一个简单的 javascript hack 来“修复”它。
1. 创建新的日期数组;
2. 将它与您当前的日期数组进行比较;
3.用零填充值数组中的相应空白;

它可能可以做得更简单、更漂亮,但这是我的代码:

var minDate = new Date(date[0]).getTime(),
maxDate = new Date(date[date.length - 1]).getTime();

var newDates = [],
currentDate = minDate,
d;

while (currentDate <= maxDate) {
d = new Date(currentDate);
newDates.push(d.getFullYear() + '-' + ("0" + (d.getMonth() + 1)).slice(-2) + '-' + ("0" + d.getDate()).slice(-2));
currentDate += (24 * 60 * 60 * 1000); // add one day
}

for (var i = 0; i < newDates.length; i++) {
if (newDates[i] == dates[i]) {
newCount.push(count[n]);
n++;
} else {
newCount.push("0");
dates.splice(i, 0, newDates[i]);
}
}

关于javascript - 图表.js :set yAxis point to 0 when there is gap between two dates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39217834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com