Google Chart - 如何在 ComboChart 中仅更改一列的颜色?

Google Chart - How to change color of one column only in ComboChart?

我不确定在使用 ComboChart 时如何更改特定列(在下面的示例中名为 'Different color here')的颜色?玩 { role: 'style' } 没有给我想要的结果。

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>
      Google Visualization API Sample
    </title>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1', {
        packages: ['corechart']
      });

    </script>
    <script type="text/javascript">
      function drawVisualization() {
        // Create and populate the data table.
        var data = google.visualization.arrayToDataTable([
          ['Month', 'Test', 'Avg.'],
          ['1', 165, 145],
          ['2', 135, 145],
          ['Different color here', 157, 145],
          ['4', 139, 145],
          ['5', 136, 145]
        ]);

        // Create and draw the visualization.
        var ac = new google.visualization.ComboChart(document.getElementById('visualization'));
        ac.draw(data, {
          title: 'TITLE',
          width: 600,
          height: 400,
          vAxis: {
            title: "AAA"
          },
          hAxis: {
            title: "BBB"
          },
          seriesType: "bars",
          series: {
            0: {
              color: "yellow"
            },
            1: {
              type: "line",
              color: "green"
            },

          },

        });
      }

      google.setOnLoadCallback(drawVisualization);

    </script>
  </head>

  <body style="font-family: Arial;border: 0 none;">
    <div id="visualization" style="width: 600px; height: 400px;"></div>
  </body>

</html>

JSFIDDLE: http://jsfiddle.net/2emzos38/1/

样式角色应遵循应用样式的系列。

    var data = google.visualization.arrayToDataTable([
      ['Month', 'Test', {role: 'style', type: 'string'}, 'Avg.'],
      ['1', 165, null, 145],
      ['2', 135, null, 145],
      ['Different color here', 157, 'magenta', 145],
      ['4', 139, null, 145],
      ['5', 136, null, 145]
    ]);

请参阅以下工作片段...

<!--
You are free to copy and use this sample in accordance with the terms of the
Apache license (http://www.apache.org/licenses/LICENSE-2.0.html)
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>
      Google Visualization API Sample
    </title>
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {
        packages: ['corechart']
      });

    </script>
    <script type="text/javascript">
      function drawVisualization() {
        // Create and populate the data table.
        var data = google.visualization.arrayToDataTable([
          ['Month', 'Test', {role: 'style', type: 'string'}, 'Avg.'],
          ['1', 165, null, 145],
          ['2', 135, null, 145],
          ['Different color here', 157, 'magenta', 145],
          ['4', 139, null, 145],
          ['5', 136, null, 145]
        ]);

        // Create and draw the visualization.
        var ac = new google.visualization.ComboChart(document.getElementById('visualization'));
        ac.draw(data, {
          title: 'TITLE',
          width: 600,
          height: 400,
          vAxis: {
            title: "AAA"
          },
          hAxis: {
            title: "BBB"
          },
          seriesType: "bars",
          series: {
            0: {
              color: "yellow"
            },
            1: {
              type: "line",
              color: "green"
            },

          },

        });
      }

      google.charts.setOnLoadCallback(drawVisualization);

    </script>
  </head>

  <body style="font-family: Arial;border: 0 none;">
    <div id="visualization" style="width: 600px; height: 400px;"></div>
  </body>

</html>


注意:

jsapi 加载程序已过时,不应再使用。

改为使用 loader.js
这只会更改 loadsetOnLoadCallback 语句。 见上面的片段。