[Chartjs]-How to introduce newline character in xAxes Label with chart js

1👍

For multiline labels you will need to proide an array for the label in which each entry is its own line. This can be achieved with the tick callback:

function newDate(milis) {
  return moment().add(milis, 'ms');
}

var config = {
  type: 'line',
  data: {
    labels: [newDate(-4), newDate(-3), newDate(2), newDate(3), newDate(4), newDate(5), newDate(6)],
    datasets: [{
      label: "My First dataset",
      data: [1, 3, 4, 2, 1, 4, 2],
    }]
  },
  options: {
    scales: {
      xAxes: [{
        ticks: {
          callback: (tick) => (tick.split('-'))
        },
        type: 'time',
        time: {
          unit: 'millisecond',
          stepSize: 5,
          displayFormats: {
            millisecond: 'HH:mm - YYYY/MM/DD'
          }
        }
      }],
    },
  }
};

var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, config);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.bundle.min.js"></script>
<canvas id="myChart"></canvas>

Leave a comment