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

标签 javascript html chart.js

我使用的是 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/

相关文章:

javascript - 图表在 # 个链接期间消失

javascript - 雷达 chartjs 上的渐变背景

javascript - Google 表格中的日期/时间未将正确的时间传输到 Google 日历中

javascript - jQuery 数据给我 undefined

javascript - 在 Backbone.js 中绑定(bind)一个回调到这个

javascript - 如何使用 HTML5 Canvas 和 JS 制作响应式角色

html - Bootstrap 4 : Flexbox Image on the left and text on the right

javascript - 在 ChartJS 中对标签进行换行时,工具提示中出现不需要的逗号

javascript - D3图表执行错误

javascript - 通过 AJAX 加载内容时的浏览器缓存问题