如何从chartjs隐藏零数据值?

问题描述 投票:1回答:1

我已经使用chartjs构建了图表。我正在尝试从图表中删除那些为零的数据值。我编写了一个仅显示非零值的函数,但似乎不起作用。谁能帮忙?

下面是我的Javascript

var ctx = document.getElementById('myChart2').getContext('2d');
            var myChart = new Chart(ctx, {
                type:<?php echo $chartype; ?>,
                data : {
                  labels: [<?php echo $datelist_2; ?>], <!--label name->
                  datasets: [
                      { 
                        label: "Sales",
                        data: [<?php echo $atlasdata2; ?>],
                        backgroundColor: 'rgb(248, 108, 108, 0.6)'
                      }                   
                      ]
                },
                .......
                .......
                .......
                      plugins: {
                              datalabels: {
                                 display: true,
                                 align: 'center',
                                 anchor: 'center',
                                 formatter: function(value, index, values) {
                                     if(value != 0){
                                         value = value.toString();
                                         value = value.split(/(?=(?:...)*$)/);
                                         value = value.join(',');
                                         return value;
                                     }
                                    }
                              }
                           }
                    }

                });

How my chart looks like currently?

javascript php chart.js
1个回答
0
投票

[JavaScript在涉及值和布尔检查时有点有趣。我建议阅读有关Java语言中的Truthy and Falsey

您应使用身份不等式运算符!==

if(value !== 0){
    value = value.toString();
    value = value.split(/(?=(?:...)*$)/);
    value = value.join(',');
    return value;
}

也值得理解the differences between == and ===

© www.soinside.com 2019 - 2024. All rights reserved.