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

536
Views
How to refresh or update bar chart data of Ng2-charts or chart.js angular 12?

component.html

<div class="chart-wrapper">
  <canvas baseChart [datasets]="barChartDatafour" [labels]="barChartLabelsfour"
    [options]="barChartOptionsfour" [plugins]="barChartPluginsfour" [legend]="barChartLegendfour"
    [chartType]="barChartTypefour" [colors]="chartColorsfour">
  </canvas>
</div>

component.ts

submit() {
  this.newStartDate = this.datePipe.transform(this.model.startDate, 'yyyy-MM-dd');
  this.newEndDate = this.datePipe.transform(this.model.endDate, 'yyyy-MM-dd');
    this.http.post(this.url + "/portwise", {
      startDate: this.newStartDate,
      endDate: this.newEndDate
    }).subscribe((data2: any) => {
      let resp12 = Array.from(Object.keys(data2), l => data2[l]);
      this.typeResponse = resp12[0];
      localStorage.setItem('state', JSON.stringify(this.typeResponse.map(j => j.state)));
      localStorage.setItem('port', JSON.stringify(this.typeResponse.map(j => j.port)));
      localStorage.setItem('ship', JSON.stringify(this.typeResponse.map(j => j.ship)));
    }
  )
}

public barChartOptionsfour: ChartOptions = {
  responsive: true,
  scales: {
    xAxes: [{
      ticks: {
        autoSkip: false,

      }
    }], yAxes: [{
      ticks: {
        min: 0
      }
    }]
  },
  plugins: {
    datalabels: {
      anchor: 'end',
      align: 'end',
    }
  }
};
public barChartLabelsfour: Label[] = JSON.parse(localStorage.getItem('state'));
public barChartTypefour: ChartType = 'bar';
public barChartLegendfour = true;
public barChartPluginsfour = [pluginDataLabels];

public barChartDatafour: ChartDataSets[] = [
  {
    data: JSON.parse(localStorage.getItem('transport')), label: 'port'
  },
  {
    data: JSON.parse(localStorage.getItem('traffic')), label: 'ship'
  }
];


public chartColorsfour: Array<any> = [
  {
    backgroundColor: '#2196f3',
    borderColor: '#2196f3',
    pointBackgroundColor: '#2196f3',
    pointBorderColor: '#fff',
    pointHoverBackgroundColor: '#fff',
    pointHoverBorderColor: '#2196f3'
  },
  {
    backgroundColor: '#61a0a8',
    borderColor: '#61a0a8',
    pointBackgroundColor: '#61a0a8',
    pointBorderColor: '#fff',
    pointHoverBackgroundColor: '#fff',
    pointHoverBorderColor: '#61a0a8'
  }
];

I am new in angular. In the above code I want to update or refresh data of bar chart but I don't understand how to do that? Data are coming but it display when I refresh the page. Now, I am looking for when I click on button then the data will show. So, How can I do this?

Thank You

about 4 years ago · Juan Pablo Isaza
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!