Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap X axis labels to multi-lines (X axis label formatting) in ng2-Charts?

How to wrap x axis labels in to multi-lines (2 lines) in a bar chart made by https://valor-software.com/ng2-charts/ ?

Expected Result should be like this. See the X axis labeling.

Ng2-charts is based on Chart.js and found following links (PRs) which helps to address this problem.

https://github.com/chartjs/Chart.js/commit/d1b411f4fc2d7b9cafa2d235c9ee008d149a22e3 https://github.com/chartjs/Chart.js/pull/2704

However is it possible to achieve the same in ng2-charts ? Anyone came across this issue when using ng2-charts; if so please let me know how you approach to solve this in angular way.

like image 389
Amila Iddamalgoda Avatar asked Jan 07 '19 17:01

Amila Iddamalgoda


1 Answers

Some workaround only if you want to wrap labels by spliting by space (" ").

scales: {         
      xAxes: [
        {
          ticks: {
            callback: function(label, index, labels) {
              if (/\s/.test(label)) {
                return label.split(" ");
              }else{
                return label;
              }              
            }
          }
        }
      ]
    }

Chart looks like this now. enter image description here

like image 94
Amila Iddamalgoda Avatar answered Oct 20 '22 10:10

Amila Iddamalgoda