Search code examples
arrayschart.jsbar-chartemptydatatext

Remove empty spaces created by null values in Chart.js


I have the following data in the following chart (with undesired empty spaces marked with blue arrows):

const dataone = [1, 2, 5, 4, 2];
const datatwo = [2, 2, 3, 4, 5];
const datathree = [2, 3, 3, 2, 4];
const datafour = [null, null, 4, null, null];
const datafive = [null, 2, null, null, null];

enter image description here

How can I remove the empty spaces and still have the null values in the arrays?

You can see and edit all the code here: https://codesandbox.io/s/github/pedroRelvas/NoNullSpacesInChart/tree/master/?fontsize=14&hidenavigation=1&theme=dark


Solution

  • It's quite simple, just do this in each dataset you want to remove (if there is no such values in the dataset array of course! If there is, is 100% understandable to have empty spaces):

    data:{
       datasets: [{
                ...
                hidden: true,
                ...
            },],
    }