此版本是最基础版,不涉及上下月,上下年衔接。
首先确定数据格式
// 折线图
Area: {
categories: [],
series: []
}配置数据模型
public class Datalist {
public List<string> categories { get; set; }
public List<DataDetail> series { get; set; }
}
public class DataDetail
{
public string name { get; set; }
public int[] data { get; set; }
public string color { get; set; }
}程序本体
/// <summary>
/// 小程序-获取charts数据
/// </summary>
[HttpGet("GetChartsDataByType")]
[AllowAnonymous]
public async Task<dynamic> GetChartsDataByType(int type)
{
var businessId = "zagwzq530";
var data = new Datalist();
//获取当前时间
DateTime nowdate = DateTime.Now;
var date = nowdate.Date;
//通过传入的type决定获取的数据的范围,1为周,2为月,3为年
switch (type)
{
case 1://根据周查看用户增长量
//限制为7位数的整数数组
//不需要额外填充数据,默认数组就是由7个0组成
int[] a = new int[7];
// 获取周几
DayOfWeek currentDayOfWeek = nowdate.DayOfWeek;
// 将周几转成数字
int dayOfWeek = (int)nowdate.DayOfWeek;
//动态计算前几日是分别是几号,获取各自的每天的数量
for (int i = 0; i < 6; i++) // 循环一周
{
var y = -dayOfWeek + i;
if (y <= 0) {
DateTime twoDaysAgo = nowdate.AddDays(y);
var date1 = twoDaysAgo.Date;
var entity = _db.Queryable<ArApp_UserEntity>().Where(a => a.CreatorTime.Value.Date == date1).Where(a => a.DeleteMark == null && a.BusinessId == businessId).Count();
a[i] = entity;
}
}
List<string> categories = new List<string>() { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
List<DataDetail> series = new List<DataDetail>() {new DataDetail() { name = "用户数", data= a, color = "#facc14" }};
data.categories = categories;
data.series = series;
break;
case 2://根据月查看用户增长量
int[] b = new int[4];
//首先根据当前时间获取年,月
var year1 = nowdate.Year;
var mouth1 = nowdate.Month;
//获取所有今年这个月的数据
var entity1 = _db.Queryable<ArApp_UserEntity>().Where(a => a.CreatorTime.Value.Year == year1 && a.CreatorTime.Value.Month == mouth1).Where(a => a.DeleteMark == null && a.BusinessId == businessId).ToListAsync();
//循环获取所在周,因为就4个值,所以直接switch判断,
foreach (var item in entity1.Result) {
int weekOfYear = (int)item.CreatorTime.Value.DayOfWeek;
if (weekOfYear > 0 && weekOfYear < 5) {
switch (weekOfYear)
{
case 1:
b[0] += 1;
break;
case 2:
b[1] += 1;
break;
case 3:
b[2] += 1;
break;
case 4:
b[3] += 1;
break;
}
}
};
List<string> categories2 = new List<string>() { "第一周", "第二周", "第三周", "第四周"};
List<DataDetail> series2 = new List<DataDetail>() { new DataDetail() { name = "用户数", data = b, color = "#2fc25b" }};
data.categories = categories2;
data.series = series2;
break;
case 3://根据年查看用户增长量
int[] c = new int[12];
//首先根据当前时间获取年,月
var year = nowdate.Year;
var mouth = nowdate.Month;
//先判断数据的年份是否一样
//在判断月份
for (int i = 1;i <= mouth;i++ ) {
//判断数据的年份是否一样,并且月份也要一样
var entity = _db.Queryable<ArApp_UserEntity>().Where(a => a.CreatorTime.Value.Year == year && a.CreatorTime.Value.Month == i).Where(a => a.DeleteMark == null && a.BusinessId == businessId).Count();
c[i-1] = entity;
};
List<string> categories3 = new List<string>() { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" };
List<DataDetail> series3 = new List<DataDetail>() { new DataDetail() { name = "用户数", data = c, color = "#1890ff" } };
data.categories = categories3;
data.series = series3;
break;
default:
Console.WriteLine("Invalid number");
break;
};
return data;
}