Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
chart js update X-axis value based on user selection from drop-down in Angular

token.html

 <mat-form-field>
            <mat-select formControlName="selectedValue" >
                <mat-option (click)="TokenPrice()" *ngFor="let c of value" [value]="c.viewValue">{{c.value}}</mat-option>
            </mat-select>
        </mat-form-field>

token.ts

 value:coins[] = [
    {value:'USDT', viewValue:'0xdAC17F958D2ee523a2206206994597C13D831ec7'},
    {value:'BUSD', viewValue:'0xdAC17F958D2ee523a2206206994597C13D831ec7'},
    {value:'SHIB', viewValue:'0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE'},
    {value:'WBTC', viewValue:'0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599'},
  ]

getting selected value as

  get selectedValue():FormControl{
    return this.loginForm.get("selectedValue") as FormControl
  }

for every drop-down selection, returns token price.Token Price keeps changing based on selection.

  async TokenPrice(){
    this.TokenUsdPrice = await Promise.all( await this.blocks.map(async (e,i)=>
    await Moralis.Web3API.token.getTokenPrice({address:this.selectedValue.value,to_block:e.block})
  ))  
    this.filtereTokenPrice();
  }

I want to display TokenPrice(Dynamic) in X-axis and Dates(constant) in Y-axis.

  async filtereTokenPrice(){
    this.usd = await this.TokenUsdPrice.map(item=>item.usdPrice);//get usdprice
    this.chart = new Chart('canvas',{
      type:'line',
     data:{
      labels:this.service.getdates(),
      datasets:[
        {
          data:this.usd,
          borderWidth:1,
          fill:false
        }
      ]
     }
    })
    
  }

this works only for 1 selection and displays as needed. When I select other options I get

 Error: Canvas is already in use. Chart with ID '0' must be destroyed before the canvas can be reused.
Error: Canvas is already in use. Chart with ID '0' must be destroyed before the canvas can be reused.

How do I update X-axis values for every other selction.If more information or stackslibz needed please let me know. Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

.destroy()

Use this to destroy any chart instances that are created. This will clean up any references stored to the chart object within Chart.js, along with any associated event listeners attached by Chart.js. This must be called before the canvas is reused for a new chart.

https://www.chartjs.org/docs/latest/developers/api.html#destroy

  async filtereTokenPrice(){
    this.usd = await this.TokenUsdPrice.map(item=>item.usdPrice);//get usdprice
     this.chart.destroy(); // <----- 
      this.chart = new Chart('canvas',{
      type:'line',
     data:{
      labels:this.service.getdates(),
      datasets:[
        {
          data:this.usd,
          borderWidth:1,
          fill:false
        }
      ]
     }
    })
    
  }
<canvas id="canvas"></canvas>
@ViewChild('chart') chart;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda