javascript - 如何在 mvc c# 中使用 ajax 调用在条形图中绑定(bind)动态数据?

标签 javascript asp.net-mvc charts

我正在尝试在条形图中计算过去 7 天的节目。为此,我使用了条形图以及 ASP.NET MVC 和 C#。现在在此页面中,我添加了静态条形图显示,现在我希望此图表在设定日期的图表 x 轴上创建动态。

下面是我要显示的表格数据:

DateColumn | Count
09-05-2017    10
08-05-2017    05
07-05-2017    20
06-05-2017    4000
05-05-2017    30
04-05-2017    5000
03-05-2017    40

这是我使用存储过程从 SQL Server 中的表中获取的数据。

这是我的html页面代码=>

 <div class="box box-without-bottom-padding">            
        <canvas id="myChart"></canvas>
    </div>

这是我的脚本代码=>

    <script type="text/javascript">
    $(document).ready(function () {        
    $.ajax({
        url: 'Home/GetCount',
        dataType: "json",
        type: "GET",
        contentType: 'application/json; charset=utf-8',
        async: false,
        processData: false,
        cache: false,
        delay: 15,
        success: function (data) {
            for (var i in data) {
                // here come the data as object in the object 2 value 1 is datecolumn and 2 is total count.

            }
            AutoFollow();
        },
        error: function (xhr) {
            alert('error');
        }
    });
});


function AutoFollow()
{
    var ctx = document.getElementById("myChart").getContext('2d');
    var myChart = new Chart(ctx, {
        type: 'bar',
        data: {
            labels: ["M", "T", "W", "T", "F", "S", "S"], // here i want to show my date value
            datasets: [{
                label: 'AutoFollow',
                data: [12, 19, 3, 17, 28, 24, 7], // here show my total count date wise 
                backgroundColor: "rgba(153,255,51,1)"
            }, {
                label: 'Manual',
                data: [30, 29, 5, 5, 20, 3, 10],
                backgroundColor: "rgba(255,153,0,1)"
            }]
        },            
    });
}

这是我的静态图表草图=>

enter image description here

在我的静态图表上方显示这里,请问任何知道如何在图表中设置我自己的数据的人。

最佳答案

示例:

型号:

 public class MyModel
    {
        public string DateColumn { get; set; }
        public int Count { get; set; }

        public static List<MyModel> GetList()
        {
            return new List<MyModel>
            {
                new MyModel {DateColumn = "09-05-2017",Count = 10},
                new MyModel {DateColumn = "10-05-2017",Count = 30},
                new MyModel {DateColumn = "11-05-2017",Count = 50},
                new MyModel {DateColumn = "12-05-2017",Count = 10},
                new MyModel {DateColumn = "13-05-2017",Count = 100},
                new MyModel {DateColumn = "14-05-2017",Count = 20},
                new MyModel {DateColumn = "15-05-2017",Count = 10},
                new MyModel {DateColumn = "16-05-2017",Count = 150},
                new MyModel {DateColumn = "17-05-2017",Count = 15},
                new MyModel {DateColumn = "18-05-2017",Count = 11},
                new MyModel {DateColumn = "19-05-2017",Count = 5},
            };
        }
    }

Clontoller:

 public class HomeController : Controller
    {   
        public JsonResult GetCount()
        {
            var model = MyModel.GetList();//Lode data from database
            return Json(model, JsonRequestBehavior.AllowGet);
        }
    }

View :

<div class="box box-without-bottom-padding">            
        <canvas id="myChart"></canvas>
    </div>

脚本:

  <script type="text/javascript">
        $(function() {
            var data = getData();
            AutoFollow(data);
        });

        function getData() {
            var dateValue = [];
            var countValue = [];
            $.ajax({
                url: "/home/GetCount/",
                dataType: 'json',
                async: false
            }).done(function(data) {
                data.forEach(function(obj) {
                    dateValue.push(obj.DateColumn);
                    countValue.push(obj.Count);
                });
            });
            return {
                dateValue: dateValue,
                countValue: countValue
            };
        }


        function AutoFollow(data) {
            var ctx = document.getElementById("myChart").getContext('2d');
            var myChart = new Chart(ctx,
                {
                    type: 'bar',
                    data: {
                        labels: data.dateValue, // here i want to show my date value
                        datasets: [
                            {
                                label: 'AutoFollow',
                                data: data.countValue, // here show my total count date wise 
                                backgroundColor: "rgba(153,255,51,1)"
                            }, {
                                label: 'Manual',
                                data: [30, 29, 5, 5, 20, 3, 10],
                                backgroundColor: "rgba(255,153,0,1)"
                            }
                        ]
                    }
                });
        }
    </script>

============================================= ================================================ ================================================

                 --------------------------
                   **As Per Your Request**
                 --------------------------

型号:

 public class MyModel
    {
        public string DateColumn { get; set; }
        public string Lable { get; set; }
        public int Count { get; set; }

        public static List<MyModel> GetList()
        {
            return new List<MyModel>
            {
                new MyModel {DateColumn = "09-05-2017",Count = 10,Lable = "Lable1"},
                new MyModel {DateColumn = "10-05-2017",Count = 30,Lable = "Lable1"},
                new MyModel {DateColumn = "11-05-2017",Count = 50,Lable = "Lable1"},
                new MyModel {DateColumn = "12-05-2017",Count = 10,Lable = "Lable1"},
                new MyModel {DateColumn = "13-05-2017",Count = 100,Lable = "Lable1"},
                new MyModel {DateColumn = "14-05-2017",Count = 20,Lable = "Lable2"},
                new MyModel {DateColumn = "15-05-2017",Count = 10,Lable = "Lable2"},
                new MyModel {DateColumn = "16-05-2017",Count = 150,Lable = "Lable2"},
                new MyModel {DateColumn = "17-05-2017",Count = 15,Lable = "Lable2"},
                new MyModel {DateColumn = "18-05-2017",Count = 11,Lable = "Lable2"},
                new MyModel {DateColumn = "19-05-2017",Count = 5,Lable = "Lable2"},
            };
        }
    }

Action :

public JsonResult GetCount()
{
    var data = MyModel.GetList()
        .GroupBy(s => s.Lable)
        .Select(s => new
        {
            DateColumn = s.Select(d => d.DateColumn),
            data = new
            {
                Lable = s.Key,
                Count = s.Select(d => d.Count)
            }

        }); //Lode data from database
    var model = new
    {
        DateColumn = data.First().DateColumn,
        CountColumn = data.Select(s => s.data)
    };
    return Json(model, JsonRequestBehavior.AllowGet);
}

脚本:

    <script type="text/javascript">
        $(function() {
            var data = getData();
            AutoFollow(data);
        });

        function getData() {
            var dateValue ;
            var countValue;
            $.ajax({
                url: "/Chart/GetCount/",
                dataType: 'json',
                async: false
            }).done(function (data) {
                dateValue = data.DateColumn;
                countValue = data.valueColumn;
            });
            return {
                dateValue: dateValue,
                countValue: countValue
            };
        }


        function AutoFollow(data) {
            var ctx = document.getElementById("myChart").getContext('2d');
            var myChart = new Chart(ctx,
                {
                    type: 'bar',
                    data: {
                        labels: data.dateValue, // here i want to show my date value
                        datasets: [
                            {
                                label: data.countValue[0].Lable,
                                data: data.countValue[0].Count, // here show my total count date wise
                                backgroundColor: "rgba(153,255,51,1)"
                            }, {
                                label: data.countValue[1].Lable,
                                data: data.countValue[1].Count,
                                backgroundColor: "rgba(255,153,0,1)"
                            }
                        ]
                    }
                });
        }
</script>

关于javascript - 如何在 mvc c# 中使用 ajax 调用在条形图中绑定(bind)动态数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43863998/

相关文章:

javascript - 在 php 中循环 Javascript 函数

javascript - 如何在单击 Angular JS 图表时获取 x 轴和 y 轴?

java - 多选项卡问题 对于 Java Web 应用程序之一 - 混合来自两个不同记录的数据

javascript - 使用 JavaScript 禁用 onClick 功能

javascript - HTML5 地理定位器对相应浏览器的最大调用次数?

asp.net-mvc - 尝试在 asp.net mvc 中提交富文本编辑器内容并获取 "potentially dangerous Request.Form value was detected"

asp.net-mvc - 更改为发布时无法加载文件或程序集 (nopcommerce 3.5)

asp.net-mvc - 谁设置了 HttpContext.User.Identity 的 IsAuthenticated 属性

vba - 如何使用 pastepecial 将图表作为位图粘贴到 vba 中的另一张纸上

iOS-图表 : Increasing bar width in grouped sets of BarChartView