Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart Js Change Label orientation on x-Axis for Line Charts

I am using chart.js.

Similar to this Question, I would like to rotate my x-axis labels 90 degrees. Currently my labels are rotated about 80 degrees with default settings.

enter image description here

Could somebody help me adapt the bar-chart solution for rotating labels, so that I can use it on line-charts?

like image 827
Delcon Avatar asked Jan 26 '16 20:01

Delcon


People also ask

How do you rotate the X axis labels?

Rotate X-Axis Tick Labels in Matplotlib There are two ways to go about it - change it on the Figure-level using plt. xticks() or change it on an Axes-level by using tick. set_rotation() individually, or even by using ax.

How do I change the orientation of a label?

Change Text Direction Using A Text Box Select the text orientation you need and select okay. b) left click inside the text box, then right click, and select “Borders and Alignment” from the options that appear. Select the text orientation you need and select okay.

How do I change the label on vertical axis?

In the Select Data Source dialog box, under Horizontal (Categories) Axis Labels, click Edit. In the Axis label range box, do one of the following: Specify the worksheet range that you want to use as category axis labels. , and then select the range that you want to use on the worksheet.


2 Answers

If you are using chart.js 2.x, just set maxRotation: 90 and minRotation: 90 in ticks options. It works for me! And if you want to all x-labels, you may want to set autoSkip: false. The following is an example.

var myChart = new Chart(ctx, {     type: 'bar',     data: chartData,     options: {         scales: {             xAxes: [{                 ticks: {                     autoSkip: false,                     maxRotation: 90,                     minRotation: 90                 }             }]         }     } }); 
like image 169
tabetomo Avatar answered Sep 19 '22 13:09

tabetomo


for x axis use this

 options: {       legend: {         display: false       },       scales: {         xAxes: [           {           ticks: {                 autoSkip: false,                 maxRotation: 0,                 minRotation: 0             }           }         ]       }     } 

and can filter the label with a for loop:

      arrayLabels.forEach((date, i) => {     let label = "";     if (i % step == 0 && fecha) {       label = moment(date, "DD/MM").format("DD MMM");     }     labels.push(label);   });    chartOptions.data.labels = labels; 

enter image description here

like image 32
Edgar Olivar Avatar answered Sep 18 '22 13:09

Edgar Olivar