score:1

Accepted answer

you can use the plugin core api. it offers different hooks that may be used for executing custom code. in below code snippet, i use the afterdraw hook to draw a title on the canvas. that way you can draw as many titles as you want, define them of different style each and place them wherever it suits you.

please note that inside chart options, i also defined some layout padding. this prevents the title from overlapping the chart bars.

layout: {
  padding: {
    top: 40
  }
}

chart.plugins.register({
  afterdraw: chart => {
      var ctx = chart.chart.ctx;
      ctx.save();
      ctx.textalign = 'center';
      ctx.textbaseline = 'middle';
      ctx.font = "18px arial";
      ctx.fillstyle = "gray";
      ctx.filltext('top title', chart.chart.width / 2, 20);
      ctx.restore();
  }
});

new chart(document.getelementbyid('mychart'), {
  type: 'bar',
  data: {
    labels: ['a', 'b', 'c', 'd'],
    datasets: [{
      data: [10, 12, 8, 6],
      backgroundcolor: ['red', 'blue', 'green', 'orange']
    }]
  },
  options: {
    layout: {
      padding: {
        top: 40
      }
    },
    title: {
      display: true,
      text: 'custom chart title',
      position: 'bottom',
    },
    legend: {
      display: false
    },
    scales: {
      yaxes: [{
        ticks: {
          beginatzero: true
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.3/chart.min.js"></script>
<canvas id="mychart" height="100"></canvas>


Related Query

More Query from same tag