Remove x-axis label/text in chart.js

151,240

Solution 1

UPDATE chart.js 2.1 and above

var chart = new Chart(ctx, {
    ...
    options:{
        scales:{
            xAxes: [{
                display: false //this will remove all the x-axis grid lines
            }]
        }
    }
});


var chart = new Chart(ctx, {
    ...
    options: {
        scales: {
            xAxes: [{
                ticks: {
                    display: false //this will remove only the label
                }
            }]
        }
    }
});

Reference: chart.js documentation

Old answer (written when the current version was 1.0 beta) just for reference below:

To avoid displaying labels in chart.js you have to set scaleShowLabels : false and also avoid to pass the labels:

<script>
    var options = {
        ...
        scaleShowLabels : false
    };
    var lineChartData = {
        //COMMENT THIS LINE TO AVOID DISPLAYING THE LABELS
        //labels : ["1","2","3","4","5","6","7"],
        ... 
    }
    ...
</script>

Solution 2

This is for chart.js ^3.0.0

Remove x-axis labels and grid chart lines

var chart = new Chart(ctx, {
    ...
    options:{
        scales:{
            x: {
                display: false
            }
        }
    }
});

Remove only x-axis labels

var chart = new Chart(ctx, {
    ...
    options: {
        scales: {
            x: {
               ticks: {
                   display: false
              }
           }
        }
    }
});

Solution 3

(this question is a duplicate of In chart.js, Is it possible to hide x-axis label/text of bar chart if accessing from mobile?) They added the option, 2.1.4 (and maybe a little earlier) has it

var myLineChart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        scales: {
            xAxes: [{
                ticks: {
                    display: false
                }
            }]
        }
    }
}

Solution 4

var lineChartData = {
    labels: ["", "", "", "", "", "", ""] // To hide horizontal labels
	,datasets : [
		{
			label: "My First dataset",
			fillColor : "rgba(220,220,220,0.2)",
			strokeColor : "rgba(220,220,220,1)",
			pointColor : "rgba(220,220,220,1)",
			pointStrokeColor : "#fff",
			pointHighlightFill : "#fff",
			pointHighlightStroke : "rgba(220,220,220,1)",
			
			data: [28, 48, 40, 19, 86, 27, 90]
		}
	]
}



window.onload = function(){
	var options = {
		scaleShowLabels : false // to hide vertical lables
	};
	var ctx = document.getElementById("canvas1").getContext("2d");
	window.myLine = new Chart(ctx).Line(lineChartData, options);

}

Solution 5

Faced this issue of removing the labels in Chartjs now. Looks like the documentation is improved. http://www.chartjs.org/docs/#getting-started-global-chart-configuration

Chart.defaults.global.legend.display = false;

this global settings prevents legends from being shown in all Charts. Since this was enough for me, I used it. I am not sure to how to avoid legends for individual charts.

Share:
151,240

Related videos on Youtube

Sonny G
Author by

Sonny G

Updated on September 08, 2021

Comments

  • Sonny G
    Sonny G over 2 years

    How do I hide the x-axis label/text that is displayed in chart.js ?

    Setting scaleShowLabels:false only removes the y-axis labels.

    <script>
        var options = {
            scaleFontColor: "#fa0",
            datasetStrokeWidth: 1,
            scaleShowLabels : false,
            animation : false,
            bezierCurve : true,
            scaleStartValue: 0,
        };
        var lineChartData = {
            labels : ["1","2","3","4","5","6","7"],
            datasets : [
                {
                    fillColor : "rgba(151,187,205,0.5)",
                    strokeColor : "rgba(151,187,205,1)",
                    pointColor : "rgba(151,187,205,1)",
                    pointStrokeColor : "#fff",
                    data : [1,3,0,0,6,2,10]
                }
            ]
    
        }
    
    var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData,options);
    
    </script>
    
  • giammin
    giammin almost 9 years
    please downvoter tell me how to improve my answer, thanks
  • IOrlandoni
    IOrlandoni almost 9 years
    Does not work. Commenting the labels out makes the chart throw an error on update.
  • giammin
    giammin almost 9 years
    @OhCaN this answer was written when the current version of chartjs was v1.0 beta i have running implementations with that code and they all work fine.
  • Nicholas-c
    Nicholas-c almost 9 years
    Worked perfectly; You are a life saver!
  • Charlie Martin
    Charlie Martin over 8 years
    Newer highchart version? This question is about chart.js. Not highcharts.
  • Thiago Duarte
    Thiago Duarte over 8 years
    This would remove the y axis scale also.
  • Samuele
    Samuele about 8 years
    I solved it inserting those lines BEFORE :this.buildScale(data.labels); Line: 2375. var newLabels=[]; for(var i=0;i<data.labels.length;i++){ newLabels.push(''); } Then change also with: this.buildScale(newLabels);
  • StackUnder
    StackUnder almost 8 years
    Using Version: 2.1.6, this one did the trick. Also, without using global you could: options:{ legend: { display: false, },
  • István Pálinkás
    István Pálinkás over 7 years
    If I'm right, this solution also removes the "background grid" ( I don't know the proper reference, the gray bars behind the graph ). Is there a solution that only removes the "labels" as OP requested?
  • Legends
    Legends over 7 years
    Nothing of the above mentioned works for my bar chart, chart.js 2.4.0. --> and also avoid to pass the labels. This should really be only a switch, so passing labels shouldn't be a concern here. Waiting for chart.js 3.x :)
  • Legends
    Legends over 7 years
    perfect ! that's what I am looking for. But it has to be set before the chart is rendered. Doesn't work afterwards...
  • Darin Cardin
    Darin Cardin over 6 years
    I am using ng2-charts 1.5.0 and this code works fine.
  • Billu
    Billu over 6 years
    @giammin, your script is not working in my case, can we discuss.
  • giammin
    giammin over 6 years
    @user123 this is a really old answer and i have not used highcharts since them... i dont think i can help you, sorry
  • Matt K
    Matt K almost 6 years
    As @StevenPalinkas noted this will remove all the x-axis grid lines as well as the label text. Not ideal.
  • giammin
    giammin almost 6 years
    @MattK you are right. I updated my answer accordingly. Thanks
  • Rachel Martin
    Rachel Martin over 4 years
    scaleFontSize is not a valid property
  • Anton Krug
    Anton Krug about 3 years
    So it's like @giammin answer, but for the newer versions?
  • Denismr7
    Denismr7 about 3 years
    Yes, I use chart.js 3.0.2 and that works for me