Google图表 - 表格列

时间:2017-03-25 20:57:11

标签: google-visualization

我试图创建Google Charts折线图。从C#MVC控制器方法返回的Json我看起来如下:

enter image description here

我的JS代码如下所示:

function drawChart() {
    var jsonData = $.ajax({
        url: "/Misc/GetWeeklySalesData/",
        dataType: "json",
        async: false
    }).responseText;

    // Create our data table out of JSON data loaded from server.
    var data = new google.visualization.DataTable(jsonData);

    alert(data);

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.LineChart(document.getElementById('chartDiv'));
    chart.draw(data, { width: 400, height: 240 });

我收到的消息'表没有列'当它清楚的时候。

1 个答案:

答案 0 :(得分:3)

要直接从JSON创建数据表,它必须采用特定格式:

Format of the Constructor's JavaScript Literal data Parameter

否则,您可以创建一个空白数据表并手动加载行:

static void Main(string[] args)
    {
        while (true)
        {
            int temperature;
            Console.WriteLine("Enter the temp");
            string choice = Console.ReadLine();
            temperature = Convert.ToInt32(choice);
            if (temperature < 17 || temperature > 25)
            {
                Console.WriteLine("Temp is {0} and its too cold to take a bath", temperature);
                Console.WriteLine("Enter the temp again");
                //temperature = Convert.ToInt32(Console.ReadLine());
            }


            else if (choice == "Q")
                break;

            else
            {
                Console.WriteLine("Temp is MADE TO 20, thou it is {0}, its ok to bath", temperature);
                Console.WriteLine("Enter the temp");
                temperature = Convert.ToInt32(Console.ReadLine());
            }                
        }            
    }

注意:强烈建议使用 - &gt; $.ajax({ url: "/Misc/GetWeeklySalesData/", dataType: "json", }).done(function (jsonData) { var data = new google.visualization.DataTable(); data.addColumn('string', 'Week'); data.addColumn('number', 'Retail'); data.addColumn('number', 'Wholesale'); jsonData.forEach(function (row) { data.addRow([ row.Week, row.Retail, row.Wholesale ]); }); var chart = new google.visualization.LineChart(document.getElementById('chartDiv')); chart.draw(data, { width: 400, height: 240 }); }).fail(function (jq, text, err) { console.log(text + ' - ' + err); }); 。请改用async: false回调...