score:0

the plugin core api offers a range of hooks that may be used for performing custom code. you can use the afterdraw hook to draw images instead of tick labels directly on the canvas using canvasrenderingcontext2d.

you'll also have to instruct chart.js not to display the default tick labels. this can be done through the following definition inside the chart options.

scales: {
  xaxes: [{
    ticks: {
      display: false
    }
  }], 

further you need to define some padding at the bottom and right of the chart, otherwise you'll see only part of the images.

layout: {
  padding: {
    bottom: 30,
    right: 15
  }
},

please have a look at the runnable code snippet below.

const labels = ['2020-02-06', '2020-02-07', '2020-02-08', '2020-02-09'];
const images = ['https://i.stack.imgur.com/2rav2.png', 'https://i.stack.imgur.com/tq5da.png', 'https://i.stack.imgur.com/3krtw.png', 'https://i.stack.imgur.com/ilyvi.png'];
const values = [48, 56, 33, 44];

new chart(document.getelementbyid("mychart"), {
  type: "line",
  plugins: [{
    afterdraw: chart => {      
      var ctx = chart.chart.ctx; 
      var xaxis = chart.scales['x-axis-0'];
      var yaxis = chart.scales['y-axis-0'];
      xaxis.ticks.foreach((value, index) => {  
        var x = xaxis.getpixelfortick(index);      
        var image = new image();
        image.src = images[index],
        ctx.drawimage(image, x - 12, yaxis.bottom + 10);
      });      
    }
  }],
  data: {
    labels: labels,
    datasets: [{
      label: 'my dataset',
      data: values,
      fill: false,
      backgroundcolor: 'green',
      bordercolor: 'green'
    }]
  },
  options: {
    responsive: true,
    layout: {
      padding: {
        bottom: 30,
        right: 15
      }
    },
    scales: {
      yaxes: [{ 
        ticks: {
          beginatzero: true,
          stepsize: 20
        }
      }],
      xaxes: [{
        type: 'time',
        time: {
          unit: 'day',
          tooltipformat: 'mmm dd'
        },
        ticks: {
          display: false
        }   
      }],
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.3/chart.bundle.min.js"></script>
<canvas id="mychart" height="90"></canvas>


Related Query

More Query from same tag