如何使用 mvc 图表助手计算工作总小时数并格式化日期以仅显示 mvc 中的月份
How to calculate the sum of hours worked and format the dates to only show the months in mvc using mvc chart helpers
这是我目前正在运行的代码 我想知道使用 mvc 图表时这两件事是否可行?以及如何去做
我的控制器
public ActionResult CharterColumn()
{
ArrayList xValue = new ArrayList();
ArrayList yValue = new ArrayList();
var results = (from c in db.Timesheets select c);
results.ToList().ForEach(rs => xValue.Add(rs.Date));
//I want to format this to show the only the months on the x axis
results.ToList().ForEach(rs => yValue.Add(rs.Hours));
//And I want to calculate the sum of the hours for each month worked by a specific employee
new Chart(width: 800, height: 400, theme: ChartTheme.Yellow)
.AddTitle("Test")
.AddSeries("Default", chartType: "Column", xValue: xValue, yValues: yValue)
.Write("bmp");
return null;
}
和我的观点
<div>
<img src= "@Url.Action("CharterColumn")" alt="Chart"/>
</div>
您可以像这样按月和年对 TimeSheet
记录进行分组:
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
// gets the records grouped based on Year and Month.
// Creates an object of IEnumerable<IGrouping<>> type
var groupedByMonth = result
.OrderByDescending(x => x.Date)
.GroupBy(x => new { x.Date.Year, x.Date.Month }).ToList();
// gets the months names in a list
List<string> monthNames = groupedByMonth
.Select(a => dtfi.GetAbbreviatedMonthName(a.Key.Month))
.ToList();
// gets the total hours per month
List<int> hoursPerMonth = groupedByMonth
.Select(a => a.Sum(p => p.Hours))
.ToList();
ArrayList xValue = new ArrayList(monthNames);
ArrayList yValue = new ArrayList(hoursPerMonth);
我正在使用 DateTimeFormatInfo 来获取月份的名称。你也可以在没有这个的情况下实现这一点:
List<string> monthNames = groupedByMonth
.Select(a => a.FirstOrDefault().Date.ToString("MMMM"))
.ToList();
这是我目前正在运行的代码 我想知道使用 mvc 图表时这两件事是否可行?以及如何去做
我的控制器
public ActionResult CharterColumn()
{
ArrayList xValue = new ArrayList();
ArrayList yValue = new ArrayList();
var results = (from c in db.Timesheets select c);
results.ToList().ForEach(rs => xValue.Add(rs.Date));
//I want to format this to show the only the months on the x axis
results.ToList().ForEach(rs => yValue.Add(rs.Hours));
//And I want to calculate the sum of the hours for each month worked by a specific employee
new Chart(width: 800, height: 400, theme: ChartTheme.Yellow)
.AddTitle("Test")
.AddSeries("Default", chartType: "Column", xValue: xValue, yValues: yValue)
.Write("bmp");
return null;
}
和我的观点
<div>
<img src= "@Url.Action("CharterColumn")" alt="Chart"/>
</div>
您可以像这样按月和年对 TimeSheet
记录进行分组:
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
// gets the records grouped based on Year and Month.
// Creates an object of IEnumerable<IGrouping<>> type
var groupedByMonth = result
.OrderByDescending(x => x.Date)
.GroupBy(x => new { x.Date.Year, x.Date.Month }).ToList();
// gets the months names in a list
List<string> monthNames = groupedByMonth
.Select(a => dtfi.GetAbbreviatedMonthName(a.Key.Month))
.ToList();
// gets the total hours per month
List<int> hoursPerMonth = groupedByMonth
.Select(a => a.Sum(p => p.Hours))
.ToList();
ArrayList xValue = new ArrayList(monthNames);
ArrayList yValue = new ArrayList(hoursPerMonth);
我正在使用 DateTimeFormatInfo 来获取月份的名称。你也可以在没有这个的情况下实现这一点:
List<string> monthNames = groupedByMonth
.Select(a => a.FirstOrDefault().Date.ToString("MMMM"))
.ToList();