Ngx-charts: dataLabelFormatting - use variables from the component

Created on 4 Jul 2019  路  2Comments  路  Source: swimlane/ngx-charts

Hi, using dataLabelFormatting property, I want to add additional data beside the original value of the bar.

I want to add the percent of the bar value, but for that I need the total of the values of the bars.

So I was using the dataLabelFormatting property like this:
[dataLabelFormatting] = "formatDataLabel"
and

public formatDataLabel(value) {
    return `${value}km虏`
};

But now for the percent I changed the code to this:

public formatDataLabel(value) {
    return `${value}km虏 (${((value * 100) / this.total).toFixed(2) + '%'})`
};

and the template like this:
[dataLabelFormatting] = "formatDataLabel.bind(this)"

Now when I try the code, everything working fine but the browser start to lag and freeze. As I think the template change is the problem.

So, Is there a solution for the lag and freeze problem, or another solution to add the percent besides the bar value?

I use Angular 7 with version 11.1.0 of ngx-charts.

Most helpful comment

You need to bind your function inside the component. Binding it in the template will cause performance issues because the binding is evaluated on every change detection cycle.

in component:

this.boundFormatDataLabel = this.formatDataLabel.bind(this);

in template:

[dataLabelFormatting] = "boundFormatDataLabel"

All 2 comments

You need to bind your function inside the component. Binding it in the template will cause performance issues because the binding is evaluated on every change detection cycle.

in component:

this.boundFormatDataLabel = this.formatDataLabel.bind(this);

in template:

[dataLabelFormatting] = "boundFormatDataLabel"

there is no dataLabelFormatting in ngx-charts-line-chart 馃槙

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ronybarbosa picture ronybarbosa  路  3Comments

Jacquers picture Jacquers  路  3Comments

stephanrauh picture stephanrauh  路  4Comments

tobigit picture tobigit  路  4Comments

amcdnl picture amcdnl  路  4Comments