0

我想用谷歌图表 api 显示柱形图,这是我的代码。我想在底部的 x 轴上显示月份名称,并在图形左侧的 y 轴上显示值,但是我无法得到它

google.load("visualization", "1", { packages: ["corechart"] });
    google.setOnLoadCallback(drawChart);
function drawVisualization() {
// Create and populate the data table.
var JSONObject = { "cols": [
{ "id": "", "label": "Jan", "Pattern": "", "type": "number" },    
{ "id": "", "label": "Feb", "Pattern": "", "type": "number" },
{ "id": "", "label": "Mar", "Pattern": "", "type": "number" },
{ "id": "", "label": "Apr", "Pattern": "", "type": "number" },
          { "id": "", "label": "May", "Pattern": "", "type": "number" },
           { "id": "", "label": "Jun", "Pattern": "", "type": "number" },
            { "id": "", "label": "Jul", "Pattern": "", "type": "number" },
            { "id": "", "label": "Aug", "Pattern": "", "type": "number" },
            { "id": "", "label": "Sep", "Pattern": "", "type": "number" },
            { "id": "", "label": "Oct", "Pattern": "", "type": "number" },
            { "id": "", "label": "Nov", "Pattern": "", "type": "number" },
            { "id": "", "label": "Dec", "Pattern": "", "type": "number" }
            ], "rows":
            [{ "c": [
            { "v": "0"},
             { "v": "0", "f": null },
              { "v": "0", "f": null },
               { "v": "0", "f": null },
                { "v": "0", "f": null },
                 { "v": "0", "f": null },
                  { "v": "2000", "f": null },
                   { "v": "0", "f": null },
                   { "v": "0", "f": null },
                   { "v": "1000", "f": null },
                    { "v": "0", "f": null },
                    { "v": "0", "f": null }
                    ]
  }]
            };
            var data = new google.visualization.DataTable(JSONObject);

   // Create and draw the visualization.
   new google.visualization.ColumnChart(document.getElementById('visualization')).
   draw(data,
       {title:"Yearly Coffee Consumption by Country",
        width:600, height:400,
         hAxis: {title: "Year"}}
   );
  }

​</p>

4

1 回答 1

0

您需要更改数据的布局。Visualization API ColumnCharts(和最相似的 Viz API 图表)期望数据在第一列中具有域轴(ColumnCharts 的 x 轴)值,并且在以下列中具有每个数据系列的值(其中每列是一个数据系列)。鉴于您的要求和现有数据,我认为您需要一个包含两列数据的 DataTable:“月”和“值”(或您决定调用的任何名称)。这是它可能看起来的示例:

var JSONObject = {
    "cols": [
        {"type": "string", "label": "Month"},
        {"type": "number", "label": "Value"}
    ],
    "rows": [
        {"c":[{"v": "Jan"}, {"v": 0}]},
        {"c":[{"v": "Feb"}, {"v": 0}]},
        {"c":[{"v": "Mar"}, {"v": 0}]},
        {"c":[{"v": "Apr"}, {"v": 0}]},
        {"c":[{"v": "May"}, {"v": 0}]},
        {"c":[{"v": "Jun"}, {"v": 0}]},
        {"c":[{"v": "Jul"}, {"v": 2000}]},
        {"c":[{"v": "Aug"}, {"v": 0}]},
        {"c":[{"v": "Sep"}, {"v": 0}]},
        {"c":[{"v": "Oct"}, {"v": 1000}]},
        {"c":[{"v": "Nov"}, {"v": 0}]},
        {"c":[{"v": "Dec"}, {"v": 0}]}
    ]
};
于 2013-10-28T13:44:42.470 回答