2012-04-09 43 views
3

我想顯示在我的網站使用Google圖表API到目前爲止我不能得到它的工作,我找不到任何示例使用MVC 3 剃刀使用谷歌圖表API與MVC 3剃鬚刀和jQuery的AJAX

這裏是使用JSON獲取數據

// Load the Visualization API and the piechart package. 
    google.load('visualization', '1.0', { 'packages': ['corechart'] }); 

    // Set a callback to run when the Google Visualization API is loaded. 
    google.setOnLoadCallback(drawChart); 

    JSON.stringify = JSON.stringify || function (obj) { 
     var t = typeof (obj); 
     if (t != "object" || obj === null) { 
      // simple data type 
      if (t == "string") obj = '"' + obj + '"'; 
      return String(obj); 
     } 
     else { 
      // recurse array or object 
      var n, v, json = [], arr = (obj && obj.constructor == Array); 
      for (n in obj) { 
       v = obj[n]; t = typeof (v); 
       if (t == "string") v = '"' + v + '"'; 
       else if (t == "object" && v !== null) v = JSON.stringify(v); 
       json.push((arr ? "" : '"' + n + '":') + String(v)); 
      } 
      return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}"); 
     } 
    }; 
    // Callback that creates and populates a data table, 
    // instantiates the pie chart, passes in the data and 
    // draws it. 
    function drawChart() { 

     // Create the data table. 
     var data = new google.visualization.DataTable(); 
     data.addColumn('string', 'Topping'); 
     data.addColumn('number', 'Slices'); 
     $.post('@Url.Content("~/Home/GetMyChart1")', 
      function (items) { 
       // Successful requests get here 
       alert(JSON.stringify(items) + " - " + items.rows.length); 
       data.addRows(items.rows.length); 
       $.each(items.rows, function (i, item) { 
        alert(i); 
        data.setCell(i, 0, item.Name); 
        data.setCell(i, 1, item.ID); 
       }); 
       alert("finished"); 
       alert(data.length); 
      }); 
     // Set chart options 
     var options = { 'title': 'How Much Pizza I Ate Last Night', 
      'width': 400, 
      'height': 300 
     }; 

     // Instantiate and draw our chart, passing in some options. 
     var chart = new google.visualization.PieChart(document.getElementById('chart_div')); 
     chart.draw(data, options); 
    } 

控制器代碼

public ActionResult GetMyChart1(string CurrentClass) 
    { 
     var tests = from t in db.Tests 
        group t by new { t.StudentID, t.Student.CurrentSchoolGrade } into tl 
        select new { StudentID = tl.Key.StudentID, Class = tl.Key.CurrentSchoolGrade, Score = (tl.Sum(k => k.Score)/tl.Sum(l => l.Part.Score))* 100, Count = tl.Count() }; 
     var results = from t in tests 
         where t.Class == CurrentClass 
         select t; 
     List<DataItem> dt = new List<DataItem>(); 
     dt.Add(new DataItem(results.Count(x => x.Score <= 40), "0% - 40%")); 
     dt.Add(new DataItem(results.Count(x => x.Score <= 60 && x.Score > 40), "40% - 60%")); 
     dt.Add(new DataItem(results.Count(x => x.Score <= 80 && x.Score > 60), "60% - 80%")); 
     dt.Add(new DataItem(results.Count(x => x.Score <= 100 && x.Score > 60), "80% - 100%")); 

     chartJson cj = new chartJson(); 
     cj.rows = dt; 

     return Json(cj); 
    } 
public class chartJson 
    { 
     public List<DataItem> rows { get; set; } 
    } 
public class DataItem 
{ 
    public DataItem(int id, string name) 
    { 
     ID = id; 
     Name = name; 
    } 
    public int ID { get; set; } 
    public string Name { get; set; } 
} 

所有警報返回除了警報(data.length)正確的價值觀我的代碼即時通訊;它會返回undefined ,並且繪圖div會顯示一個寫入標籤。無數據

+0

的谷歌圖表API是用JavaScript編寫的。那麼,爲什麼你在使用ASP.Net MVC時遇到麻煩? – xandercoded 2012-04-09 13:44:29

+0

@Xander我添加了一些代碼,如果有幫助的話,你可以告訴我我做錯了什麼 – 2012-04-09 13:47:20

+0

儘可能多地給出詳細信息是明智的,以便我們能夠更好地回答你的問題。如果您描述頁面加載或加載後發生的情況,這將有所幫助。根據上面的信息,我不知道你身邊發生了什麼。 – 2012-04-09 13:51:20

回答

3

我在想,你需要的圖表繪製線移動POST回調的內部:

$.post('@Url.Content("~/Home/GetMyChart1")', function (items) { 
    // Successful requests get here     
    alert(JSON.stringify(items) + " - " + items.rows.length);     
    data.addRows(items.rows.length);     
    $.each(items.rows, function (i, item) { 
     alert(i);      
     data.setCell(i, 0, item.Name);      
     data.setCell(i, 1, item.ID);     
    });     
    alert("finished");     
    alert(data.length);    
    // Set chart options   
    var options = { 
     'title': 'How Much Pizza I Ate Last Night',    
     'width': 400,    
     'height': 300   
     // Instantiate and draw our chart, passing in some options.   
     var chart = new google.visualization.PieChart(document.getElementById('chart_div')); 
     chart.draw(data, options); 
    });   
};   
+0

我不明白它爲什麼工作 – 2012-04-10 07:11:47

+0

因爲這個帖子是異步發生的,所以程序代碼在下一行繼續執行,而帖子正在發生。 google.visualization.PieChart行可能在完成後執行。所以你希望PieChart能夠成功完成帖子,這是在anonymous post函數中。 – mgnoonan 2012-04-10 12:41:42

+0

嗯好吧我現在明白了,非常感謝你 – 2012-04-10 12:44:27

0

Google圖表API是使用JavaScript編寫的,因此它可以用於任何Web框架,包括ASP.NET MVC。所有你需要做的就是將它包含在你的觀點中。它不應該被限制或不能工作,因爲你正在使用ASP.NET MVC。

+0

據我所知,也許我沒有問正確的問題 – 2012-04-09 13:52:09

0

查看完整的代碼示例後,看起來google.setOnLoadCallback(drawChart);最有可能在data準備好之前執行drawChart方法。因此沒有圖表。

而不是製作一個Ajax .Post到服務器檢索數據,只是建立你的數據在初始請求。