Assign different color to each bar in a google chart

后端 未结 1 1384
孤城傲影
孤城傲影 2020-12-22 12:12

I am trying to assign different colors to each bar in a columnchart, but when I try every bar gets the same color.

Code:

  // Load the Visualization          


        
相关标签:
1条回答
  • 2020-12-22 12:46

    Change to:

    ...
    // Create the data table.
    var data = new google.visualization.DataTable();
    
    var raw_data = [
        ['A', 40],
        ['B', 17],
        ['C', 7]
    ];
    
    data.addColumn('string', 'Columns');
    for (var i = 0; i  < raw_data.length; ++i) {
        data.addColumn('number', raw_data[i][0]);
    }
    data.addRows(1);
    data.setValue(0, 0, 'row');
    for (var i = 0; i  < raw_data.length; ++i) {    
        data.setValue(0, i+1, raw_data[i][1]);
    }
    
    // Set chart options
    ...
    

    === UPDATE ===

    A simplified version:

    ...
    // Create the data table.
    var data = new google.visualization.DataTable();
    
    var raw_data = {
        'A':   40
        , 'B': 17
        , 'C':  7
    };
    
    data.addRows(1);
    var i = 0;
    for (var index in raw_data) {
        data.addColumn('number', index);
        data.setValue(0, i++, raw_data[index]);
    }
    
    // Set chart options
    ...
    
    0 讨论(0)
提交回复
热议问题