显示在MVC饼图按百分比数据(Showing data by percentage on Pie C

2019-10-22 04:31发布

在我的MVC5应用程序,我有表可以说,它的名字是学生和属于一个团队每一个学生。 我想按照自己的团队按百分比向学生展示。 为此,我也为了加入学生实体实体创建一个视图模型。 所以,我该怎么让剑道UI饼图上我想要的数据? 你可以看看代码并纠正下面的错误? 和你能给适合这种方法视图样本? 提前致谢。


视图模型:

public class StudentViewModel
{
    public int StudentId { get; set; }

    public int TeamId { get; set; }

    public string TeamName { get; set; }

    public int TeamPercentage { get; set; }
}


控制器:

public ActionResult Index_Read([DataSourceRequest] DataSourceRequest request)
{
    var dataContext = repository.Student;

    var result = dataContext.ToDataSourceResult(request, m => new StudentViewModel
        {
            StudentId = m.StudentId,
            TeamId = m.TeamId,
            TeamName = m.TeamName,

            TeamPercentage = //??? How can I obtain percentage by Lambda Expression ???
        }
    );
    return Json(result, JsonRequestBehavior.AllowGet);
}

Answer 1:

你可以这样做:

视图模型

public class StudentViewModel
{
    public string category { get; set; }
    public int value { get; set; }
}

控制器 (请注意我在查询中做了一些假设)

public ActionResult Index_Read()
{
    var teamCount = repository.Team.Count();
    var result = repository.Student.GroupBy(g => g.TeamName).Select(s => new StudentViewModel { category = s.Key, value = s.Count()/teamCount });
    // Imagine result contains something like this: { category = "Team 1", value = "20" }, { category = "Team 2", value = "30" }, { category = "Team 3", value = "10" }, { category = "Team 4", value = "40" }
    return View(result);
}

视图

@model IEnumerable<StudentViewModel>

@(Html.Kendo().Chart()
    .Name("chart")
    .Title(title => title.Text("Teams").Position(ChartTitlePosition.Bottom))
    .Legend(legend => legend.Visible(true))
    .Series(series => series.Pie(Model))
)

替代

另一种方式来做到这一点会被使用的数据源绑定。

public class StudentViewModel
{
    public string TeamName{ get; set; }
    public int Percentage { get; set; }
}

@(Html.Kendo().Chart<StudentViewModel>()
    .Name("chart2")
    .DataSource(ds => ds.Read("GetTeamData", "Home"))
    .Series(series => series.Pie(m => m.Percentage, model => model.TeamName))
)


文章来源: Showing data by percentage on Pie Charts in MVC