How can I add different colors to bar in Column chart

后端 未结 2 1039
遇见更好的自我
遇见更好的自我 2021-01-19 18:27

How can i add different colors to Bars in Column chart. Adding colors field in option is not working. Please help.

Below is the code snippet:

tdata.a         


        
2条回答
  •  渐次进展
    2021-01-19 18:49

    the colors option applies colors to each series
    so if you have 3 colors
    you would need 3 y-axis columns

    as follows...

    google.charts.load('current', {
      packages: ['corechart']
    }).then(function () {
      var data = google.visualization.arrayToDataTable([
        ['x', 'y0', 'y1', 'y2'],
        ['A', 100, 120, 130]
      ]);
    
      var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
      chart.draw(data, {
        colors: ['red', 'green', 'blue']
      });
    });
    
    


    to color individual columns within a series,
    use a 'style' column role

    as follows...

    note: using a 'style' column role will invalidate the legend

    google.charts.load('current', {
      packages: ['corechart']
    }).then(function () {
      var data = google.visualization.arrayToDataTable([
        ['x', 'y', {role: 'style', type: 'string'}],
        ['A', 100, 'red'],
        ['B', 120, 'green'],
        ['C', 130, 'blue']
      ]);
    
      var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
      chart.draw(data, {
        legend: 'none'
      });
    });
    
    

提交回复
热议问题