Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I wish to pass values of an array to the data and label fields of the chart.js dataset.

Here the code from success of ajax call made to fetch json data. I fetch the json data and store it into an array.

Data = jQuery.parseJSON(result);
var count = Data.length;
var counter = 0;
while(count > 0) {
    LabelResult[counter] =[Data[counter].TIME];
    counter++;
    count --;
}

Now i wish to use this label values into the labels filed.

var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: [LabelResult],
        datasets: [{
            label: '# of Votes',
            data: [DataResult],
            borderWidth: 1
        }]
    }    
});

But there seems some issue and the data is not getting rendered on the chart

share|improve this question
    
values are getting inserted into LabelResult at LabelResult[counter] =[Data[counter].TIME]; – Salil Lambay Aug 10 at 9:55
    
yes this is how they are declared var LabelResult = []; var DataResult = []; – Salil Lambay Aug 10 at 10:00
    
it appears as if only one label having all the array valuessuch as a stack piled up. – Salil Lambay Aug 10 at 10:05
up vote 0 down vote accepted

LabelResult is an array, change

labels: [LabelResult]

to

labels: LabelResult

Also:

data: [DataResult]

to

data: DataResult

Like:

var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: LabelResult,
        datasets: [{
            label: '# of Votes',
            data: DataResult,
            borderWidth: 1
        }]
    }    
});
share|improve this answer
    
Thanks a lot :) that did the trick! – Salil Lambay Aug 10 at 10:07
    
in case of it seem to work but now in case of labels: LabelResult but not in case of data: DataResult – Salil Lambay Aug 10 at 14:31

I think you could try to remove some brackets.

while(count > 0){
     LabelResult[counter] = Data[counter].TIME; // here removed brackets
      counter++;
      count --;
}    

and

data: {
    labels: LabelResult, // here removed brackets
    datasets: [{
        label: '# of Votes',
        data: DataResult, // here removed brackets
        borderWidth: 1
    }]
},  

I hope that will works.

share|improve this answer
    
AWESOME! that worked like magic! – Salil Lambay Aug 10 at 10:06
    
in case of it seem to work but now in case of labels: LabelResult but not in case of data: DataResult – Salil Lambay Aug 10 at 14:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.