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

时间:2019-12-11 21:28:53

标签: charts google-api google-visualization

使用ComboChart时,我不确定如何更改特定列的颜色(在下面的示例中称为“此处的不同颜色”)?与{ 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/

1 个答案:

答案 0 :(得分:0)

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

    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语句。 参见上面的代码段。