Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda