简体   繁体   English

将数据从控制器加载到图表Jquery / MVC / C#

[英]Loading data from controller to chart Jquery/ MVC/ C#

I have the following controller code to return chart data to jquery. 我有以下控制器代码将图表数据返回给jquery。 UPDATE: I have modified the code as suggested, but still getting error. 更新:我已经按照建议修改了代码,但是仍然出现错误。

 public JsonResult GetLeaveDataForPieChart(int user_id)
    {

        List<EmployeeLeaveHeader> elh1 = new List<EmployeeLeaveHeader>();
        List<ChartEvent> ch = new List<ChartEvent>();
        elh1 = itlbg1.EmployeeLeaveHeaders.Where(f => f.Employee_ID == user_id).ToList();

    foreach (var item in elh1)
    {
        ChartEvent ce = new ChartEvent();
        ce.value = (item.leaveAvailable * 100).ToString();
        ce.color = item.CompanyLeaves.LeaveTypes.color;
        ce.highlight = "#ffffff";
        ce.label = item.CompanyLeaves.LeaveTypes.typeDescription + " Leave Available";
        ch.Add(ce);

        ChartEvent ce1 = new ChartEvent();
        ce1.value = (item.leaveTaken * 100).ToString();
        ce1.color = item.CompanyLeaves.LeaveTypes.color_light;
        ce1.highlight = "#ffffff";
        ce1.label = item.CompanyLeaves.LeaveTypes.typeDescription + " Leave Taken";
        ch.Add(ce1);
    }



    return Json(ch, JsonRequestBehavior.AllowGet);

}

I need to retrieve data in jquery so that it is in the required format to be passed to pie data. 我需要在jquery中检索数据,以便将其以必需的格式传递给饼图数据。

(document).ready(function () {

    user_id = $("#user_id_1").text();
    $.ajax({
        url: '/Leave/GetLeaveDataForPieChart/',
        cache: false,
        data: { user_id: user_id },
        type: "GET",
        datatype: "json",
        success: function (data) {
            alert(data);
            var pieChartCanvas = $("#pieChart").get(0).getContext("2d");
            var pieChart = new Chart(pieChartCanvas);
            var PieData = $.each(data, function (idx, obj) {
                var leave = new Object();

                    leave.value = obj.value;
                    leave.color = obj.color;
                    leave.highlight = obj.highlight;
                    leave.label = obj.label;
                    alert(leave);
                    return leave;
                });

                    var pieOptions = {
    //pie options..

};

                pieChart.Doughnut(PieData, pieOptions);
            }
        });

Can anyone explain how to convert the json data to javascript object to be passed to the pie chart? 谁能解释如何将json数据转换为要传递到饼图的javascript对象?

Here is how you parse JSON string into object. 这是将JSON字符串解析为对象的方法。

 var jsonObject = JSON.parse(jsonString);

here is how you use jquery to fetch the data and use it for your chart 这是您使用jquery来获取数据并将其用于图表的方式

 $.ajax({
        url: '/<Controller>/GetLeaveDataForPieChart/',
        cache: false,
        data: {  user_id: <UserID> },
        type: "GET",
        success: function (data, textStatus, xmlHttpRequest) {
            data = JSON.parse(data);
            //....
            //....
            //Chart
            //....
            //....
            //....
        }
    });   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM