Ng2-charts: Load async labels

Created on 14 Feb 2017  路  8Comments  路  Source: valor-software/ng2-charts

Is it possible to load labels dynamically ? Is there a way to draw the chart on demand (once we get all the datas + labels) ?

The datas and labels are loaded together in async but only the data are shown, not the labels.
http://plnkr.co/edit/pK5KBaDrK6fwakc4WWrY?p=preview

Thanks for your help

Most helpful comment

I needed the same thing but it is not supported yet... meanwhile I have changed the chart.js file located in ng2-charts/charts/chart.js, method ngOnchanges line 23:

BaseChartDirective.prototype.ngOnChanges = function (changes) {
        if (this.initFlag) {
            // Check if the changes are in the data or datasets
            if (changes.hasOwnProperty('labels') || changes.hasOwnProperty('data') || changes.hasOwnProperty('datasets')) { // I've changed this condition
                if (changes['data']) {
                    this.updateChartData(changes['data'].currentValue);
                }
                else {
                    this.updateChartData(changes['datasets'].currentValue);
                }
                // I've added this
                if (changes['labels']) {
                    this.updateChartLabels(changes['labels'].currentValue);
                }

                this.chart.update();
            }
            else {
                // otherwise rebuild the chart
                this.refresh();
            }
        }
    };

Then I declared the "updateChartLabels" method below updateChartData() (it can be anywhere really):

BaseChartDirective.prototype.updateChartLabels = function (newLabelsValues) {
        this.chart.data.labels = newLabelsValues;
    };

I know you shouldn't be editing the generated chart.js file but this is just a workaround.

Hope this help.
Regards

All 8 comments

I needed the same thing but it is not supported yet... meanwhile I have changed the chart.js file located in ng2-charts/charts/chart.js, method ngOnchanges line 23:

BaseChartDirective.prototype.ngOnChanges = function (changes) {
        if (this.initFlag) {
            // Check if the changes are in the data or datasets
            if (changes.hasOwnProperty('labels') || changes.hasOwnProperty('data') || changes.hasOwnProperty('datasets')) { // I've changed this condition
                if (changes['data']) {
                    this.updateChartData(changes['data'].currentValue);
                }
                else {
                    this.updateChartData(changes['datasets'].currentValue);
                }
                // I've added this
                if (changes['labels']) {
                    this.updateChartLabels(changes['labels'].currentValue);
                }

                this.chart.update();
            }
            else {
                // otherwise rebuild the chart
                this.refresh();
            }
        }
    };

Then I declared the "updateChartLabels" method below updateChartData() (it can be anywhere really):

BaseChartDirective.prototype.updateChartLabels = function (newLabelsValues) {
        this.chart.data.labels = newLabelsValues;
    };

I know you shouldn't be editing the generated chart.js file but this is just a workaround.

Hope this help.
Regards

I had this issue as well as couple others. Your workaround works like a charm for me! thanks didi3r!

@didi3r your solution worked nicely.

@didi3r this worked for me too

@didi3r The workaround is great and resolved my issue as well.

@didi3r Perfect, it resolved the issue for me too

@didi3r Thanks!

Closed by ef5d2f4

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shyamal890 picture shyamal890  路  4Comments

vmironovs picture vmironovs  路  3Comments

Maistho picture Maistho  路  3Comments

SteeledSlagle13 picture SteeledSlagle13  路  3Comments

mrpotato3 picture mrpotato3  路  5Comments