Google API Graph in ASP.NET MVC

I use google apis to create a simple character

    [AllowAnonymous]
    public JsonResult PieChart()
    {
      return Json("[[\"State\",\"Total\"],[\"GA\",50], [\"AL\",30]]",JsonRequestBehavior.AllowGet);
    }

I call this method through ajax from my view. Below my look

@{
    ViewBag.Title = "Visuals";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Visuals</h2>
<div id="piechart_3d" style="width: 900px; height: 500px;"></div>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {

          var jsonData = $.ajax({
                type: 'GET',
                url: '@Url.Action("PieChart","Home")',
                dataType: "json",
                async: false
           }).responseText;

        var data = google.visualization.arrayToDataTable(jsonData, false);
        var options = {
          title: 'My Daily Activities',
          is3D: true,
        };

        var chart = new google.visualization.PieChart($('#piechart_3d')[0]);
        chart.draw(data, options);
      }

</script>

However, I get a javascript error on this line saying it is not an array

var data = google.visualization.arrayToDataTable(jsonData, false);

I also tried using JSON.Parse, but without success. Thanks for any help.

+4
source share
2 answers

I would try to configure contentType and process the result in a success handler, namely:

      $.ajax({
            type: 'GET',
            url: '@Url.Action("PieChart","Home")',
            dataType: "json",
            contentType: 'application/json; charset=utf-8',
            async: false,
            success: function(result) {
                  jsonData = result;
           }
       });

The data you return looks fine, as I tried this:

http://jsfiddle.net/Qquse/547/

+2
source

, hutchonoid . :

object[][] arr = new object[][] { new object[] { "Trickle", "Count" }, new object[] { "Ga", 50 }, new object[] { "Ga", 50 } };
+1

Source: https://habr.com/ru/post/1532670/


All Articles