Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!