0

我确实将我的代码与 Google 的示例代码进行了比较,但我找不到任何不同之处。我不知道为什么我的图表只显示 2 个数据列而不是 3 个。是因为我的浏览器(我使用的是 Chrome)吗?我尝试使用 IE 并遇到了同样的问题。

谷歌的示例代码:Example

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Chart</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">google.load('visualization', '1', {packages: ['corechart']});</script>

<script type="text/javascript">
function drawVisualization2() {
var data = google.visualization.arrayToDataTable([
['Day', 'V5161.198', 'V5161.200', 'V5161.202'],
['27/09/2013', 4.0, 9.0, 4.0],
['29/09/2013', 5.0, 8.0, 4.0]
]);
var options = {title : 'Daily usage of heaters',
vAxis: {title: "Minutes"},
hAxis: {title: "day"},
seriesType: "bars",
series: {2: {type: "line"}} // This is not the root of the problem. If I change it to 3, the chart can be displayed normally but it will not have the average line anymore.
};
var chart = new google.visualization.ComboChart(document.getElementById('chart2_div'));chart.draw(data, options);}google.setOnLoadCallback(drawVisualization2);
</script>
</head>
<body>
<div id="chart2_div" style="width: 1100px; height: 500px;"></div>
</body>
</html>

谁能给我一个建议?
非常感谢。

4

1 回答 1

0

您在此行的代码注释中说:series: {2: {type: "line"}}

这不是问题的根源。如果我将其更改为 3,图表可以正常显示,但不再有平均线。

橙线不是三个字段值的平均值。它是格式化为一行(不是条形)的第三列数据。如果您想要一条平均线,则此代码有效:

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Chart</title>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">google.load('visualization', '1', {packages: ['corechart']});</script>

    <script type="text/javascript">
      function drawVisualization2() {
        var data = google.visualization.arrayToDataTable([
          ['Day', 'V5161.198', 'V5161.200', 'V5161.202'],
          ['27/09/2013', 4.0, 9.0, 4.0],
          ['29/09/2013', 5.0, 8.0, 4.0]
        ]);

        data.addColumn('number', 'Average');

        var average = 0;

        for (var i = 0; i < data.getNumberOfRows(); i++){
          average = 0;
          for (var j = 1; j < data.getNumberOfColumns(); j++){
            average = average + data.getValue(i, j);
          }
          average = average / 3;
          data.setValue(i,4,average);
        }

        var options = {title : 'Daily usage of heaters',
                       vAxis: {title: "Minutes"},
                       hAxis: {title: "day"},
                       seriesType: "bars",
                       series: {3: {type: "line"}} // This is not the root of the problem. If I change it to 3, the chart can be displayed normally but it will not have the average line anymore.
                      };
        var chart = new google.visualization.ComboChart(document.getElementById('chart2_div'));chart.draw(data, options);}google.setOnLoadCallback(drawVisualization2);
    </script>
  </head>
  <body>
    <div id="chart2_div" style="width: 1100px; height: 500px;"></div>
  </body>
</html>
于 2013-09-30T01:01:27.547 回答