【问题标题】:How to convert json to google chart data?如何将 json 转换为谷歌图表数据?
【发布时间】:2018-07-14 15:30:52
【问题描述】:

我有一个函数在我的 MVC Web 应用程序的控制器中返回 json

public ActionResult GetWeeklyStats()
{
     using (DBContext db = new DBContext())
     {
          var result = db.sp_StoredProcedureName.ToList();
          return Json(result, JsonRequestBehavior.AllowGet);
     }
}

返回的数据如下:

   [
       {
          "WeekNo" : 1,
          "Count" : 230
       },
       {
          "WeekNo" : 2,
          "Count" : 240
       },
       {
          "WeekNo" : 3,
          "Count" : 250
       },
       {
          "WeekNo" : 4,
          "Count" : 260
       }
  ];

我正在尝试在我的视图中设置我的谷歌图表数据。这是我认为的ajax调用:

 $.ajax({
        type: "GET",
        dataType: 'json',
        contentType: 'application/json',
        url: '@Url.Action("GetWeeklyStats", "myControllerName")',
        async: false,
        success: function (data) {
            //set google data here ......
            //how to do it here???
        }
    });

谷歌数据应该是这样的:

// Create the data table.
var gcData = new google.visualization.DataTable();
gcData.addColumn('number', 'WeekNo');
gcData.addColumn('number', 'Count');
gcData.addRows([
    [1, 230],
    [2, 240],
    [3, 250], 
    [4, 260]
]);

【问题讨论】:

    标签: json ajax linq model-view-controller google-visualization


    【解决方案1】:

    使用可以使用addRow方法将数据中每个对象的值相加

    看下面的sn-p...

    $.ajax({
        type: "GET",
        dataType: 'json',
        contentType: 'application/json',
        url: '@Url.Action("GetWeeklyStats", "myControllerName")',
        async: false,
        success: function (data) {
            // Create the data table.
            var gcData = new google.visualization.DataTable();
            gcData.addColumn('number', 'WeekNo');
            gcData.addColumn('number', 'Count');
    
            $.each(data, function(i, row) {
                gcData.addRow([
                    row.WeekNo,
                    row.Count
                ]);
            });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多