[Chartjs]-Chart.js one of the chart from mixed chart is not plotting

1👍

The easiest workaround would be to use a callback function (to merge the labels back) for tooltips title, as such :

tooltips: {
   callbacks: {
      title: function(t, d) {
         return d.labels[t[0].index].replace(/\n/, ' ');
      }
   }
}

but first, you should break the x-axis labels using x-axis’ ticks callback function, instead of using two-dimensional labels array (as suggested on the thread you mentioned), like so :

xAxes: [{
   ticks: {
      callback: function(tick) {
         return tick.split(/\n/);
      }
   }
}]

* put a new-line character where you wanna break line in your label, e.g.: Jan\n2017

ᴅᴇᴍᴏ ⧩

var chart = new Chart(ctx, {
   type: 'line',
   data: {
      labels: ['Jan\n2017', 'Feb\n2017', 'Mar\n2017', 'Apr\n2017', 'May\n2017'],
      datasets: [{
         label: 'LINE',
         data: [3, 1, 4, 2, 5],
         backgroundColor: 'rgba(0, 119, 290, 0.2)',
         borderColor: 'rgba(0, 119, 290, 0.6)',
         fill: false
      }]
   },
   options: {
      scales: {
         xAxes: [{
            ticks: {
               callback: function(tick) {
                  return tick.split(/\n/);
               }
            }
         }],
         yAxes: [{
            ticks: {
               beginAtZero: true,
               stepSize: 1
            }
         }]
      },
      tooltips: {
         callbacks: {
            title: function(t, d) {
               return d.labels[t[0].index].replace(/\n/, ' ');
            }
         }
      }
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>

Leave a comment