score:1

Accepted answer

problem was with your canvas id, you should pass dynamic ids to your chart.

for example:

you have to pass dynamic ids from your component to the canvas.

and you have instantiate your chart in ngafterviewinit ie. after view gets initialized.

your chart template should be like this:

<div class="chart-container">    
    <canvas [id]="chartid" >{{ barchart }}</canvas>    
  </div>
  barchart test

and in your component it should be like this:

export class barchartcomponent implements oninit {
  @input() chartid:string;
  barchart = [];
  constructor(
  ) { }

  ngoninit(): void {
    settimeout(() => {
      this.barchart = new chart(this.chartid, {
      type: 'bar',
      data: {
        backgroundcolor: "#000000",
        labels: ['4h', '4l', 'close'],
        datasets: [{
          label: '# of inc.',
          data: [2,3,1],
          bordercolor: '#000000',
          backgroundcolor: "#000000",
        }]
      },
      options: {
        maintainaspectratio: false,
        legend: {
          display: false
        },
        scales: {
          xaxes: [{
            display: true,
            gridlines: {
              // display: false,
              drawonchartarea: false,
              zerolinecolor: '#000000',
              color: '#000000',
              fontstyle: "bold",
            },
            ticks: {
              fontcolor: '#000000',
              fontfamily: 'lato',
              fontstyle: "bold",
              precision:0
            },
          }],
          yaxes: [{
            display: true,
            gridlines: {
              drawonchartarea: false,
              zerolinecolor: '#000000',
              color: '#000000',
              fontstyle: "bold",
            },
            ticks: {
              fontcolor: '#000000',
              fontfamily: 'lato',
              fontstyle: "bold",
              precision:0
            },
          }],
        }
      }
    }, 100);
    })
  
  }

}

here is the working demo for the same. demo


Related Query

More Query from same tag