admin 管理员组

文章数量: 1086019

This is part of chart code. I am trying to show the image on the yaxis. My first problem is that the img.onload does not hit because It does not set the width and height. 2nd it just show [Object HTMLImageElement].

      yAxes: [{
        display: true,
        scaleLabel: {
          display: false,
        },
        ticks: {
          min: 0,
          max: MaxLength,
          stepSize: 1,
            callback: (value) => {
            if (value !== 0) {

                  const img = new Image();
                  img.src = getimagePath(value); 
                  
                  img.onload = () => {
                    img.width = 4;
                    img.height = 4;
                  };
                  return img;
              }
            return ``;
          },
        },
        gridLines: {
          display: false,
        }
      }]

This is part of chart code. I am trying to show the image on the yaxis. My first problem is that the img.onload does not hit because It does not set the width and height. 2nd it just show [Object HTMLImageElement].

      yAxes: [{
        display: true,
        scaleLabel: {
          display: false,
        },
        ticks: {
          min: 0,
          max: MaxLength,
          stepSize: 1,
            callback: (value) => {
            if (value !== 0) {

                  const img = new Image();
                  img.src = getimagePath(value); 
                  
                  img.onload = () => {
                    img.width = 4;
                    img.height = 4;
                  };
                  return img;
              }
            return ``;
          },
        },
        gridLines: {
          display: false,
        }
      }]
Share Improve this question edited Mar 27 at 10:00 Naren Murali 60k5 gold badges44 silver badges77 bronze badges asked Mar 27 at 9:58 Muhammad NadeemMuhammad Nadeem 111 bronze badge 2
  • Can you attach the observed and expected output? – Naveed Ahmed Commented Mar 27 at 10:02
  • i can not attach the file yet. – Muhammad Nadeem Commented Mar 28 at 15:29
Add a comment  | 

2 Answers 2

Reset to default 0
yAxes: [{
  display: true,
  scaleLabel: { display: false },
  ticks: {
    min: 0,
    max: MaxLength,
    stepSize: 1,
    callback: (value) => {
      if (value !== 0) {
        const imagePath = getimagePath(value);
        return `<img src="${imagePath}" width="20" height="20" style="vertical-align: middle;" />`;
      }
      return ``;
    }
  },
  gridLines: {
    display: false,
  }
}]

This will helps you

Please just set the property values without the onLoad and return the image.

callback: (value) => {
  if (value !== 0) {
    const img = new Image();
    img.src = getimagePath(value); 
    img.width = 4;
    img.height = 4;
    return img;
  }
  return '';
}

本文标签: angularjsChartJS to show image on yaxisStack Overflow