Add labels dynamically in Charts.js

👍:0

What do you receive in the response variable?
If you have an array of labels, you can do something like this:

success:function(response){

    // Let's assume you got a JSON formatted response
    var labelsArray = JSON.parse(response);

                              var Datos = {
                                    labels : labelsArray,
                                    datasets : [
                                        {
                                            fillColor : 'rgba(153,102,255,0.6)', 
                                            strokeColor : 'rgba(57,194,112,0.7)', 
                                            highlightFill : 'rgba(153,102,255,0.6)', 
                                            highlightStroke : 'rgba(66,196,157,0.7)',
                                            data : response
                                        }

Of course your “labelsArray” must be a properly formatted array to work with Chart.js. In case you receive an object with indexes, you need to use other array functions in order to create your labels array.

Leave a comment