using array values in chart.js data and label field

前端 未结 2 632
鱼传尺愫
鱼传尺愫 2021-01-01 02:34

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

相关标签:
2条回答
  • 2021-01-01 02:45

    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.

    0 讨论(0)
  • 2021-01-01 02:57

    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
            }]
        }    
    });
    
    0 讨论(0)
提交回复
热议问题