Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngx-charts results/data array update issue in angular

Am using ngx-charts-bar-horizontal in my angular 8 project. It is working fine when i load the results/data array in ngOninit function.

But I want to change the existing data on click of a button. it loads a new data to graph.

<ngx-charts-bar-horizontal

  [view]="view"
  [scheme]="colorScheme"
  [results]="single"
  [gradient]="gradient"
  [xAxis]="showXAxis"
  [yAxis]="showYAxis"
  [legend]="showLegend"
  [showXAxisLabel]="showXAxisLabel"
  [showYAxisLabel]="showYAxisLabel"
  [xAxisLabel]="xAxisLabel"
  [yAxisLabel]="yAxisLabel"
  (select)="onSelect($event)"
  (activate)="onActivate($event)"
  (deactivate)="onDeactivate($event)"
  [legendPosition]="'below'">
</ngx-charts-bar-horizontal>

This is working fine in ngOnInit



 single: any = [
    {
      "name": "API",
      "value": 90
    },
    {
      "name": "DB",
      "value": 12
    },
    {
      "name": "File",
      "value": 1
    },
    {
      "name": "Message/Event",
      "value": 3
    },
    {
      "name": "Poll",
      "value": 15
    }
  ];

On click of a button am callling an api to get new data and pushing to the results array.



this.dashboardservice.getCapabilitiesData(data).subscribe(response => {
          this.single=[];
          this.capabilitiesData = response;

          if (this.capabilitiesData.muelCapabilityGraph) {

            Object.keys(this.capabilitiesData.muelCapabilityGraph).forEach(key => {
              var obj = {
                "name": key,
                "value": this.capabilitiesData.muelCapabilityGraph[key]
              }
              this.single.push(obj);
            });

          }
          this.single=[...this.single];
          console.log("single-->"+JSON.stringify(this.single))
        });

this.single Has the new data but chart is blank.

Am not sure why the chart is not updated with new data. please help!

like image 602
Sharath Au Avatar asked Sep 05 '26 19:09

Sharath Au


1 Answers

I have forked a project from swimlane-ngxcharts and added a feature to add column/bar on button click ('Add data'). Please have a look. Code on stackblitz: link

like image 114
abhishek rana Avatar answered Sep 07 '26 13:09

abhishek rana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!