删除chart.js中的x轴标签/文本

Son*_*y G 53 javascript charts html5 chart.js

如何隐藏chart.js中显示的x轴标签/文本?

设置scaleShowLabels:false仅删除y轴标签.

<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>
Run Code Online (Sandbox Code Playgroud)

gia*_*min 103

更新chart.js 2.1及以上

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
                }
            }]
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

参考:chart.js文档

旧答案(当前版本为1.0 beta时编写)仅供参考,如下:

为避免显示标签,chart.js您必须设置scaleShowLabels : false并避免传递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>
Run Code Online (Sandbox Code Playgroud)

  • 请downvoter告诉我如何改进我的答案,谢谢 (7认同)
  • 如果我是对的,这个解决方案也会删除"背景网格"(我不知道正确的参考,图形背后的灰色条).是否有解决方案只删除OP请求的"标签"? (3认同)
  • 不起作用。注释掉标签会使图表在更新时引发错误。 (2认同)
  • 较新的highchart版本?这个问题是关于chart.js的。不是highcharts。 (2认同)

小智 45

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
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

Remove only x-axis labels

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


Kap*_*ook 16

(这个问题是In chart.js的副本,如果从移动设备访问,是否可以隐藏条形图的x轴标签/文本?)他们添加了选项,2.1.4(可能稍早)有它

var myLineChart = new Chart(ctx, {
    type: 'line',
    data: data,
    options: {
        scales: {
            xAxes: [{
                ticks: {
                    display: false
                }
            }]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


bal*_*ena 11

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);

}
Run Code Online (Sandbox Code Playgroud)


Mut*_*pan 7

现在面对删除Chartjs中的标签的问题.看起来文档得到了改进. http://www.chartjs.org/docs/#getting-started-global-chart-configuration

Chart.defaults.global.legend.display = false;
Run Code Online (Sandbox Code Playgroud)

此全局设置可防止图例显示在所有图表中.因为这对我来说已经足够了,所以我用它了.我不确定如何避免单个图表的传说.


小智 6

受christutty的回答启发,这是一个修改源但尚未经过彻底测试的解决方案.我还没有遇到任何问题.

在默认值部分中,在第71行附近添加此行:

// Boolean - Omit x-axis labels
omitXLabels: true,
Run Code Online (Sandbox Code Playgroud)

然后在第2215行附近,在buildScale方法中添加:

//if omitting x labels, replace labels with empty strings           
if(Chart.defaults.global.omitXLabels){
    var newLabels=[];
    for(var i=0;i<labels.length;i++){
        newLabels.push('');
    }
    labels=newLabels;
}
Run Code Online (Sandbox Code Playgroud)

这也保留了工具提示.


Sno*_*cat 6

对于那些不起作用的人,这是我在 X 轴上隐藏标签的方式-

options: {
    maintainAspectRatio: false,
    layout: {
      padding: {
        left: 1,
        right: 2,
        top: 2,
        bottom: 0,
      },
    },
    scales: {
      xAxes: [
        {
          time: {
            unit: 'Areas',
          },
          gridLines: {
            display: false,
            drawBorder: false,
          },
          ticks: {
            maxTicksLimit: 7,
            display: false, //this removed the labels on the x-axis
          },
          'dataset.maxBarThickness': 5,
        },
      ],
Run Code Online (Sandbox Code Playgroud)