samo kung

samo kung

  • NA
  • 7
  • 2.7k

Google Chart With ASP.NET Core can't get it to work

Feb 19 2018 3:45 AM
  1. public JsonResult PopulationChart()  
  2. {  
  3.     using (SqlConnection con = new SqlConnection("Data Source=.....;Initial Catalog=....;User ID=....;Password=.....;"))  
  4.     {  
  5.         //await con.OpenAsync();  
  6.         con.Open();  
  7.         SqlTransaction transaction;  
  8.         transaction = con.BeginTransaction();  
  9.   
  10.         using (SqlCommand cmd = new SqlCommand("GenLocationData", con, transaction))  
  11.         {  
  12.             try  
  13.             {  
  14.                 cmd.CommandType = CommandType.StoredProcedure;  
  15.                 //cmd.Parameters.AddWithValue("@UN", username);  
  16.                 //cmd.Parameters.AddWithValue("@PW", password);  
  17.   
  18.                 //await cmd.ExecuteNonQueryAsync();  
  19.                 cmd.ExecuteNonQuery();  
  20.                 transaction.Commit();  
  21.   
  22.   
  23.   
  24.                 var populationList = _smscontext.LocationDatas.OrderBy(xx => xx.Location).ToList();  
  25.                   
  26.                 return Json(populationList);  
  27.                 //return RedirectToAction("Index", "UserHome");  
  28.             }  
  29.             catch (Exception ex)  
  30.             {  
  31.                 transaction.Rollback();  
  32.                 ModelState.AddModelError("", ex.Message);  
  33.                 var populationList = _smscontext.LocationDatas.OrderBy(xx => xx.Location).ToList();  
  34.                 return Json(populationList);  
  35.             }  
  36.         }  
  37.     }  
  38.   
  39. }  
  1. <div id="chart_div"></div>  
  2. <script type="text/javascript">  
  3. //google.charts.load('current', {  
  4. // packages: ['corechart']  
  5. //});  
  6. google.charts.load('current', { packages: ['corechart''bar'] });  
  7. google.charts.setOnLoadCallback(LoadData);  
  8. function LoadData()  
  9. {  
  10. $.ajax({  
  11. url: '@Url.Action("PopulationChart","SMS")',  
  12. dataType: "json",  
  13. type: "GET",  
  14. error: function (xhr, status, error)  
  15. {  
  16. var err = eval("(" + xhr.responseText + ")");  
  17. toastr.error(err.message);  
  18. },  
  19. success: function (data)  
  20. {  
  21. PopulationChart(data);  
  22. return false;  
  23. }  
  24. });  
  25. return false;  
  26. }  
  27.   
  28. function PopulationChart(data) {  
  29. var tdata = new google.visualization.DataTable(data);  
  30. tdata.addColumn('string''Location');  
  31. tdata.addColumn('number''Count');  
  32. for (var i = 0; i < data.length; i++) {  
  33. //tdata.data.addRow([data[i].Location, data[i].Count]);  
  34. var xxx = data[i].Location;  
  35. tdata.addRow([data[i].Location, 1]);  
  36. }  
  37. var options = {  
  38. 'title''How Much Pizza I Ate Last Night',  
  39. //'width': 500,  
  40. //'height': 300  
  41. //is3D: true  
  42. };  
  43. var chart = new google.visualization.BarChart(document.getElementById('chart_div'));  
  44. chart.draw(tdata, options);  
  45. return false;  
  46. }  
  47. </script>  
My problem is google charts can't seem to display the data[i].Location contents. Any help would be very appreciated.

Answers (1)