使用谷歌图表在x轴上绘制日期

时间:2015-04-09 07:21:42

标签: javascript json google-visualization

我的JSON数据是:

{ "cols": [ {"id":"","label":"Duration Time","pattern":"","type":"Date"}, {"id":"","label":"Idle Time","pattern":"","type":"timeofday"} ], "rows": [ {"c":[{"v": "2015-02-06" ,"f": null}, {"v": [00,00,10] ,"f": "00:00:10"}]}, {"c":[{"v": "2015-02-06" ,"f": null}, {"v": [00,00,07] ,"f": "00:00:07"}]}, {"c":[{"v": "2015-02-13" ,"f": null}, {"v": [00,00,04] ,"f": "00:00:04"}]}, {"c":[{"v": "2015-02-13" ,"f": null}, {"v": [00,00,18] ,"f": "00:00:18"}]} ]

每当我在x轴上绘制时间或数字的任何其他内容时,我都能够这样做。但是当我在绘制日期时,它没有显示任何内容。

我的部分脚本是:

    var piechartdata = new google.visualization.DataTable(jsonPieChartData);

    // Instantiate and draw our pie chart, passing in some options.
    var chart = new      google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(piechartdata, {
  width: 800,
  height: 600,
  pointSize:5,
  chartArea: { left:"10%",top:"10%",width:"80%",height:"80%" },
  legend: {position:'top'},

 hAxis: {
                title: "Date" 
         }
});
 }

我也试过这个JSON数据:

{ "cols": [{"id":"","label":"Date2","pattern":"","type":"Date"}, {"id":"","label":"Duration Time","pattern":"","type":"timeofday"} ], "rows": [ {"c":[{"v": "15,04,09","f": null },{"v": [00,00,10] ,"f": "00:00:10"}]}, {"c":[{"v": "15,04,10","f": null },{"v": [00,00,07] ,"f": "00:00:07"}]}, {"c":[{"v": "15,04,11","f": null },{"v": [00,00,44] ,"f": "00:00:44"}]}, {"c":[{"v": "15,04,20","f": null },{"v": [00,00,18] ,"f": "00:00:18"}]} ] }

有人能告诉我我的脚本或json数据有什么问题吗?

1 个答案:

答案 0 :(得分:0)

我认为您需要使用"date"代替"Date"

此外,您的v值似乎也无效。例如,它期望日期列在v值中的Date对象,但是您提供字符串。

更改这两个似乎适用于您的第二组数据:



google.load("visualization", "1", {
    packages: ["corechart"]
});
google.setOnLoadCallback(drawChart);

function drawChart() {
    var jsonPieChartData = {
        "cols": [{ "id": "", "label": "Date2", "pattern": "","type": "date"}, 
                 { "id": "", "label": "Duration Time", "pattern": "", "type": "timeofday" }],
        "rows": [{ "c": [{ "v": new Date(2015, 3, 9), "f": null }, { "v": [00, 00, 10], "f": "00:00:10"}]}, 
                 { "c": [{ "v": new Date(2015, 3, 10), "f": null }, { "v": [00, 00, 07], "f": "00:00:07"}]}, 
                 { "c": [{ "v": new Date(2015, 3, 11), "f": null }, { "v": [00, 00, 44], "f": "00:00:44"}]}, 
                 { "c": [{ "v": new Date(2015, 3, 20), "f": null }, { "v": [00, 00, 18], "f": "00:00:18"}]}]
    };

    var piechartdata = new google.visualization.DataTable(jsonPieChartData);

    // Instantiate and draw our pie chart, passing in some options.
    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(piechartdata, {
        width: 800,
        height: 600,
        pointSize: 5,
        chartArea: {
            left: "10%",
            top: "10%",
            width: "80%",
            height: "80%"
        },
        legend: {
            position: 'top'
        },

        hAxis: {
            title: "Date"
        }
    });
}

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="chart_div" style="width: 900px; height: 300px;"></div>
&#13;
&#13;
&#13;