在ASP.NET MVC谷歌API图 [英] Google API chart in ASP.NET MVC

查看:145
本文介绍了在ASP.NET MVC谷歌API图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用谷歌API来创建一个simplechart

I am using google apis to create a simplechart

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

我打电话从我查看通过AJAX这种方法。下面是我的看法。

I am calling this method via ajax from my View. Below is my View

@{
    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>

不过我正在这条线上一个JavaScript错误说不是一个数组

However I am getting a javascript error on this line saying not an array

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

我也试图与JSON.Parse但没有成功。感谢您的帮助。

I have also tried with JSON.Parse but with no success. Thanks for any help.

推荐答案

我会尝试设置的contentType和处理的结果是成功的,而不是处理程序如下:

I would try setting the contentType and handling the result in a success handler instead as follows:

      $.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 are returning looks ok to me as I tried this:

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

这篇关于在ASP.NET MVC谷歌API图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆