收到Google图表错误“轴0的数据列不能为字符串类型”

时间:2018-09-20 11:46:06

标签: sql asp.net-mvc google-visualization google-chartwrapper

我在此上看到了多个论坛,但我知道该错误,但是已经检查了我的代码,但无法获取我要去哪里的错误,请协助我 我查询的数据库表是: 分配表: Allocation 里程碑表: milestone table aspnetusers表: aspusers table

然后我在asp.net mvc中的代码 控制器方法:

  [HttpPost]
    public JsonResult StudentColumnChart()

    {
        var userID = User.Identity.GetUserId();
        string supervisorid = userID;
        string query = $"SELECT AspNetUsers.Name AS Name, SUM(Milestone.Progress)/ COUNT(Milestone.Progress) * 100 AS Progress FROM Milestone INNER JOIN AspNetUsers ON Milestone.StudentNumber = AspNetUsers.Id INNER JOIN Allocation ON Milestone.StudentNumber = Allocation.StudentNumber Where Allocation.StaffNumber =  '{{supervisorid}}'  GROUP BY AspnetUsers.Name "; 

        //string query=$"SELECT AspNetUsers.Name AS NAME, SUM(Milestone.Progress)/ COUNT(Milestone.Progress) * 100 AS TotalProgress, Allocation.StaffNumber AS STAFF, Allocation.StudentNumber AS STUDENT " +



        //string query = "SELECT AspNetUsers.Name, SUM(Milestone.Progress)/ COUNT(Milestone.Progress) * 100 AS TotalProgress, Allocation.StaffNumber, Allocation.StudentNumber" +
        //     " From Milestone " +
        //     "INNER JOIN AspNetUsers ON Milestone.StudentNumber=AspNetUsers.Id " + 

        //     "  WHERE Allocation.StaffNumber=id AND Milestone.StudentNumber=Allocation.StudentNumber GROUP BY AspNetUsers.Name";
        // //string query = "SELECT AspNetUsers.Name, SUM(Milestone.Progress)/ COUNT(Milestone.Progress) * 100 AS TotalProgress, Allocation.StaffNumber, Allocation.StudentNumber" +
        //    " From Milestone INNER JOIN AspNetUsers ON Milestone.StudentNumber=AspNetUsers.Id " + 

        //    "  WHERE Allocation.StaffNumber=id AND  GROUP BY AspNetUsers.Name";
        string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        List<object> chartData = new List<object>();
        chartData.Add(new object[]
                        {
                           "Name","Progress"
                        });

        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        chartData.Add(new object[]
                        {
                         sdr["Name"], sdr["Progress"]
                        });
                    }
                }

                con.Close();
            }
        }

        return Json(chartData);
    }

视图:

<script type="text/javascript" src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<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() {
        $.ajax({
            type: "Post",
            url: "/CHART/StudentColumnChart",

            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                var data = google.visualization.arrayToDataTable(r);

                //ColumnChart
                var options = {
                    hAxis: {
                        title: "TotalProgress",
                        id: "progress",
                        label: "progress",
                        type: "number"
                    },
                    vAxis: {
                        title: "Name",
                        id: "Name",
                        label: "Name",
                        type: "string"
                    },
                    title: 'Students Progress %',
                    width: 600,
                    height: 400,
                    bar: { groupWidth: "50%" },
                    legend: { position: "none" },
                    isStacked: true,
                    format: "#.#'%'"
                };
                var chart = new google.visualization.ColumnChart($("#chart")[0]);

                //var chart = new google.visualization.PieChart($("#chart")[0]);

                chart.draw(data, options);
            },
            failure: function (r) {
                alert(r.d);
            },
            error: function (r) {
                alert(r.d);
            }

        });
    }
</script>

<div id="chart" style="width: 900px; height: 500px;"></div>

但是我一直收到此错误: error

请帮助!!!!

0 个答案:

没有答案