当chartJS中关联值为null时跳过标签 [英] Skip labels when associated value is null in chartJS

查看:95
本文介绍了当chartJS中关联值为null时跳过标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果图表上有12个标签(每个月一个),如果其他值为null,如何显示前6个标签?
我暂时使用 max:'Jun',但我想实现此自动化:当'July'从null更改为值时,喜欢最大成为七月。
可以使用ChartJS吗?

If I have 12 labels on a chart (one for each month), how can I just show the first 6 if other values are null ? I use max: 'Jun'for the moment but I'd like to automate this: when 'July' changes from null to a value, I'd like the max to become 'Jul'. Is it possible with ChartJS ?

data: {
        labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
        datasets: [
            {
                spangaps: true,
                label: 'Exceptionnel',
                data: [1, 2, 3, 4, 5, 6, null, null, null, null, null, null]
            },
        options: {
               scales: {
            xAxes: [{
                offset: true,
                gridLines: {
                    display: true
                },
                ticks: {
                    fontStyle: 'bold',
                    max: 'Jun'
                }
            }]}],


推荐答案

检查我的回答此处。您可以简化它:

Check my answer here. You can simplify it:

var labels = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"];
var data = [5, 6, 4, null, 5, null];
for (let i = 0; i <= data.length; i++){
    if (data[i] === null) {
        data.splice(i, 1);
        labels.splice(i, 1);
        i--;
    }
}

但是,如果您确定一旦达到空值,其余的也是空的,您不仅可以拼接一个,还可以拼接其余的。技巧还在于删除标签,以免显示空数据。

However, if you are sure that once you reach a null value the rest are also null, you can splice not just one but the remaining as well. The trick is to also remove the labels so that no empty data is shown.

PS:我建议不要从后端获取带有空数据的标签。

PS: I would advise not fetching labels with empty data from your back end if possible.

这篇关于当chartJS中关联值为null时跳过标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆