在mvc中显示图表中的年龄组

时间:2015-07-03 04:05:37

标签: c# model-view-controller charts bar-chart

我试图计算数据库中的年龄组并将其显示在图表上。

我在下面的代码中显示了条形图上的药物类别和数量。

public JsonResult DrugStatus()
    {
        List<Drug> drg = new List<Drug>();
        DataContext da = new DataContext();
        drg = da.Drugs.ToList();

        var chartData = new object[drg.Count + 1];
        chartData[0] = new object[]{
            "Drug Group",
            "Drug Quantity"
        };

        int count = 0;
        foreach (var i in drg)
        {
            count++;
            chartData[count] = new object[] { i.DrugCategory, i.Quantity };
        }

        return new JsonResult { Data = chartData, JsonRequestBehavior = JsonRequestBehavior.AllowGet};
    }

我想对我的数据库中不同年龄段的客户做同样的事情,但我在逻辑上遇到了问题。

以下是我所做的

public JsonResult AgeGroup()
    {
        List<Patient> patient = new List<Patient>();
        DataContext da = new DataContext();

        patient = da.Patients.ToList();

        var select = (from a in da.Patients
                      group a by a.Age into g
                      select new
                      {
                          Age = g.Key,
                          Age1 = g.Where(a => Convert.ToInt32(a.Age) >= 13 && Convert.ToInt32(a.Age) <= 18).Count()
                      });
        var chartData = new object[patient.Count + 1];
        chartData[0] = new object[]{
            "Age Group"
        };

        string age1, age2, age3, age4, age5;
        foreach (Patient p in da.Patients)
        {
            int Age = Convert.ToInt32(p.Age);

            if ((Age >= 13) && (Age <= 18))
            {
                age1 = "13 - 18";
            }

            if ((Age >= 19) && (Age <= 30))
            {
                age2 = "19 - 30";
            }

            if ((Age >= 31) && (Age <= 45))
            {
                age3 = "31 - 45";
            }

            if ((Age >= 46) && (Age <= 60))
            {
                age4 = "46 - 60";
            }

            if (Age >= 61)
            {
                age5 = "61 Above";
            }
        }
    }

现在我想将它添加到列表中,这样我就可以像在DrugStatus中那样循环它()

foreach (var i in drg)
    {
        count++;
        chartData[count] = new object[] { i.DrugCategory, i.Quantity };
    }

但首先我想将值添加到列表中,我该怎么做这个逻辑...任何帮助???

1 个答案:

答案 0 :(得分:0)

好的,经过后来的思考和集中,我发现我可以做逻辑并添加到通用列表&#34; Class&#34;我通过创建一个类来存储值,如下所示。

public class AgeClass
    {
        public string AgeG { get; set; }
        public int count { get; set; }

        public AgeClass()
        {
            AgeG = "";
            count = 0;
        }

        public AgeClass(string Age, int C)
        {
            AgeG = Age;
            count = C;
        }
    }

之后我继续使用JsonResult Logic,编写逻辑以获得特定年龄组并计算属于该特定年龄组的人数,如下所示......

public JsonResult AgeGroup()
    {
        List<AgeClass> agc = new List<AgeClass>();

        DataContext da = new DataContext();

        string age1 = "", age2 = "", age3 = "", age4 = "", age5 = "";
        int Count1 = 0, Count2 = 0, Count3 = 0, Count4 = 0, Count5 = 0;

        foreach (Patient p in da.Patients)
        {
            int Age = Convert.ToInt32(p.Age);

            if ((Age >= 13) && (Age <= 18))
            {
                age1 = "13 - 18";
                Count1++;
            }

            if ((Age >= 19) && (Age <= 30))
            {
                age2 = "19 - 30";
                Count2++;
            }

            if ((Age >= 31) && (Age <= 45))
            {
                age3 = "31 - 45";
                Count3++;
            }

            if ((Age >= 46) && (Age <= 60))
            {
                age4 = "46 - 60";
                Count4++;
            }

            if (Age >= 61)
            {
                age5 = "61 Above";
                Count5++;
            }
        }

完成所有操作后,我继续将结果添加到我之前创建的AgeClass中。

 AgeClass obj1 = new AgeClass(age1, Count1);
        AgeClass obj2 = new AgeClass(age2, Count2);
        AgeClass obj3 = new AgeClass(age3, Count3);
        AgeClass obj4 = new AgeClass(age4, Count4);
        AgeClass obj5 = new AgeClass(age5, Count5);

        agc.Add(obj1);
        agc.Add(obj2);
        agc.Add(obj3);
        agc.Add(obj4);
        agc.Add(obj5);

然后创建了chartData并将值插入其中并循环遍历已保存我的年龄组数据的AgeClass。

var chartData = new object[agc.Count + 1];
        chartData[0] = new object[]{
            "Age Group",
            "Age Count"
        };

        int count = 0;
        foreach (var i in agc)
        {
            count++;
            chartData[count] = new object[] { i.AgeG, i.count };
        }

        return new JsonResult { Data = chartData, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }

并在我的条形图视图中显示它并且工作正常,我希望这有助于某人。

和Carly Rae Jepsen的道具&#34; Emotions&#34;我重复的专辑,还有巧克力城&#34; The Intrutructible Choc Boi Nation&#34;专辑音乐有助于程序员认为哈哈!

提出此解决方案的任何其他方式将非常感谢...谢谢