@Component({ selector: 'app-chart', templateUrl: './chart.component.html', styleUrls: ['./chart.component.css'] }) exportclassChartComponentimplementsOnInit { transactions: any[] | undefined; //This is the transactions coming from the API displayData: any[] = []; // this is what I am graphing @Input() objectType: string | undefined; @Input() objectId: number | undefined; isLoaded:boolean=false;
getTransactions() { this.TransactionService.getTransactionsByOidAndOtype(this.objectId!, this.objectType!) .subscribe(result => { this.transactions = result.model; // // we now have the transactions, build the graphic // this.buildGraphic(); }) }
buildGraphic() { // here is how to map data coming from transactions // over to displayData with the correct names console.log ("transactions - ", this.transactions); this.transactions!.map(elem =>this.displayData!.sort(this.order) .push({name: elem.theDate.toLocaleString("en-US").substring(0,10), value: elem.amount}) ) console.log ("displayData - ", this.displayData); this.isLoaded=true; }
You pass it two variables: objectType and objectId
ngOnInit calls a function (getTransactions()) to get transactions based on objectType and objectId. This function will retrieve a set of transactions meant to be graphed.
Actually – ngx-chart will chart graphs {name, value), so an array has been setup {displayData: any[] = [];} to hold this data.
This calls the map method – which does a loop – one per element. The sort method is used to sort this elment. The push method will convert the transaction into a {name, value} and add that into the displayData array.
After that I set {isLoaded = true} which populates the chart control
import { ChartComponent } from'src/app/chart/chart.component'; : exportclassVendorDetailsComponentimplementsOnInit { : objectType: string; // used by the chart control :
constructor( : ) { : this.objectType="vendor"; }
I think the BankAccountService contains integration hooks for the chart component. I needed it for other purposes.