0

JSON をパラメーターとして、Google 視覚化 API から ColumnChart の DataTable を作成しようとしています。

次の JSON オブジェクトを生成しました。

{
    "cols": [
        {"id":"date", "label":"date","pattern":"", "type":"date"},
        {"id":"jobsonstatus", "label":"jobsonstatus", "pattern":"", "type":"number"}
    ],
    "rows": [
        {
            "c": [
                {"v": "01-02-2013", "f": null},
                {"v": 128, "f": null}
            ]
        },
        {
            "c": [
                {"v": "08-02-2013", "f": null},
                {"v": 185, "f": null}
            ]
        },
        {
            "c": [
                {"v": "15-02-2013", "f": null},
                {"v": 142, "f": null}
            ]
        },
        {
            "c": [
                {"v": "22-02-2013", "f": null},
                {"v": 86, "f": null}
            ]
        }
    ]
}

これの何が問題なのですか?

多分それは何か他のものです。コードの残りの部分は次のとおりです。

    var chart;      //The chart
var options;    //The options for the chart
var data;       //The data inside the chart

//This function will set up the actual chart but doesn't draw it.           
function init(){
    chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
    options = {
        width:600, height:450,      
        legend: {'position': 'top'},
        allowHtml: true
    };                      
}

function changeData(dataPar){
    data = new google.visualization.DataTable(dataPar);
    drawChart();                  
}

function drawChart(){   
    chart.draw(data, options);
}                   

JSON オブジェクトは関数 changeData() でスローされます。

そして、私はこれをajax部分に持っています:

    dataType: "json",       
    success: function(res, textStatus, jqXHR){
       changeData(res);
    }

背景が赤いエラーが表示されます。

a[dc] は関数ではありません

何が問題なのか、何が解決策になるのか誰にもわかりませんか?

4

1 に答える 1

1

わかりました、私はそれを自分で理解しました。

列では、列「日付」のタイプを日付に設定しましたが、指定された値が文字列であるため、文字列である必要がありました。

したがって、Google ビジュアライゼーション チャートから奇妙なエラーが発生した場合は、 JSONLintで有効と思われる json 部分にエラーがある可能性があります。

json 部分をデバッグするための私の提案は、最初に関数 addColumn() および addRows() を使用してダミーの DataTable を作成し、次に実行することです。

console.log([DataTable var].toJSON()).

この出力を、以前のスクリプトとデバッグで作成した JSON と比較します。

次の JSON オブジェクトを取得しました。

{
    "cols": [
        {"id": "date", "label": "date","pattern": "","type": "string"},
        {"id": "newjobs","label": "newjobs","pattern": "","type": "number"}
    ],
    "rows": [
        {"c": [
                {"v": "01-02-2013","f": null},
                {"v": 132,"f": null}
            ]},
        {"c": [
                {"v": "08-02-2013","f": null},
                {"v": 78,"f": null}
            ]},
        {"c": [
                {"v": "15-02-2013","f": null},
                {"v": 105,"f": null}
            ]
        },
        {"c": [
                {"v": "22-02-2013","f": null},
                {"v": 8,"f": null}
            ]}
    ]
}
于 2013-03-13T09:18:59.987 に答える