!提交代码
This commit is contained in:
199
货架标准上位机/ViewModels/DataChartViewModel.cs
Normal file
199
货架标准上位机/ViewModels/DataChartViewModel.cs
Normal file
@ -0,0 +1,199 @@
|
||||
using HandyControl.Controls;
|
||||
using LiveCharts;
|
||||
using Ping9719.WpfEx.Mvvm;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace 货架标准上位机.ViewModel
|
||||
{
|
||||
public class DataChartViewModel : BindableBase
|
||||
{
|
||||
#region 加载
|
||||
private bool isLoad;
|
||||
public bool IsLoad { get => isLoad; set { SetProperty(ref isLoad, value); } }
|
||||
#endregion
|
||||
|
||||
#region 日期
|
||||
private List<int> year = new List<int>();
|
||||
/// <summary>
|
||||
/// 年
|
||||
/// </summary>
|
||||
public List<int> Year { get => year; set { SetProperty(ref year, value); } }
|
||||
|
||||
private List<int> month = new List<int>();
|
||||
/// <summary>
|
||||
/// 月
|
||||
/// </summary>
|
||||
public List<int> Month { get => month; set { SetProperty(ref month, value); } }
|
||||
|
||||
private int yearIndex;
|
||||
/// <summary>
|
||||
/// 年索引
|
||||
/// </summary>
|
||||
public int YearIndex { get => yearIndex; set { SetProperty(ref yearIndex, value); UpdataMonth(); } }
|
||||
|
||||
private int monthIndex;
|
||||
/// <summary>
|
||||
/// 月索引
|
||||
/// </summary>
|
||||
public int MonthIndex { get => monthIndex; set { SetProperty(ref monthIndex, value); StatChart(); } }
|
||||
#endregion
|
||||
|
||||
#region 图表
|
||||
public ChartValues<int> ChartValues1 { get; set; } = new ChartValues<int>();
|
||||
public ChartValues<int> ChartValues2 { get; set; } = new ChartValues<int>();
|
||||
public ChartValues<int> ChartValues3 { get; set; } = new ChartValues<int>();
|
||||
public Func<double, string> LabelFormatterX { get; set; } = value => $"{value}号";
|
||||
public Func<double, string> LabelFormatterY { get; set; } = value => $"{value}个";
|
||||
|
||||
private IList<string> chartLabelsX;
|
||||
/// <summary>
|
||||
/// X轴轴信息
|
||||
/// </summary>
|
||||
public IList<string> ChartLabelsX { get => chartLabelsX; set { SetProperty(ref chartLabelsX, value); } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region 方法
|
||||
/// <summary>
|
||||
/// 更新年
|
||||
/// </summary>
|
||||
public async void UpdataYear()
|
||||
{
|
||||
try
|
||||
{
|
||||
IsLoad = true;
|
||||
Year = await DataDb.db.Queryable<MyTestTable>()
|
||||
.GroupBy(o => o.Time.Year)
|
||||
.OrderByDescending(o => o.Time.Year)
|
||||
.Select(o => o.Time.Year)
|
||||
.ToListAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Year = new List<int>();
|
||||
Growl.Error("刷新数据失败:" + ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
YearIndex = Year.Any() ? 0 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新月
|
||||
/// </summary>
|
||||
public async void UpdataMonth()
|
||||
{
|
||||
try
|
||||
{
|
||||
IsLoad = true;
|
||||
if (!Year.Any() || YearIndex < 0)
|
||||
return;
|
||||
|
||||
var dt1 = new DateTime(Year[YearIndex], 1, 1);
|
||||
var dt2 = dt1.AddYears(1);
|
||||
Month = await DataDb.db.Queryable<MyTestTable>()
|
||||
.Where(o => o.Time > dt1 && o.Time < dt2)
|
||||
.GroupBy(o => o.Time.Month)
|
||||
.OrderBy(o => o.Time.Month)
|
||||
.Select(o => o.Time.Month)
|
||||
.ToListAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Month = new List<int>();
|
||||
Growl.Error("刷新数据失败:" + ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
MonthIndex = Month.Any() ? Month.Count - 1 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand ButStatChartCommand { get => new DelegateCommand(ButStatChart); }
|
||||
/// <summary>
|
||||
/// 点击刷新
|
||||
/// </summary>
|
||||
public void ButStatChart()
|
||||
{
|
||||
if (IsLoad)
|
||||
{
|
||||
Growl.Info("有任务正在进行");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Year.Any() || !Month.Any() || YearIndex < 0 || MonthIndex < 0)
|
||||
{
|
||||
Growl.Info("没有选择年份或月份");
|
||||
return;
|
||||
}
|
||||
|
||||
StatChart();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 刷新统计信息
|
||||
/// </summary>
|
||||
public async void StatChart()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!Year.Any() || !Month.Any() || YearIndex < 0 || MonthIndex < 0)
|
||||
{
|
||||
ChartValues1.Clear();
|
||||
ChartValues2.Clear();
|
||||
ChartValues3.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
IsLoad = true;
|
||||
await Task.Delay(200);
|
||||
|
||||
var year = Year[YearIndex];
|
||||
var month = Month[MonthIndex];
|
||||
|
||||
var dt1 = new DateTime(year, month, 1);
|
||||
var dt2 = dt1.AddMonths(1);
|
||||
|
||||
//从数据库中查询、统计数量
|
||||
var dbdata = await DataDb.db.Queryable<MyTestTable>()
|
||||
.Where(o => o.Time > dt1 && o.Time < dt2)
|
||||
.GroupBy(o => o.Time.Day)
|
||||
.Select(o => new
|
||||
{
|
||||
day = o.Time.Day,
|
||||
count = SqlFunc.AggregateCount(o.Id),
|
||||
okCount = SqlFunc.AggregateCount(o.Status == "合格" ? "" : null),
|
||||
notCount = SqlFunc.AggregateCount(o.Status != "合格" ? "" : null),
|
||||
})
|
||||
.OrderBy(o => o.day)
|
||||
.ToListAsync();
|
||||
|
||||
ChartLabelsX = dbdata.Select(o => o.day.ToString()).ToList();
|
||||
|
||||
ChartValues1.Clear();
|
||||
ChartValues2.Clear();
|
||||
ChartValues3.Clear();
|
||||
|
||||
ChartValues1.AddRange(dbdata.Select(o => o.count));
|
||||
ChartValues2.AddRange(dbdata.Select(o => o.okCount));
|
||||
ChartValues3.AddRange(dbdata.Select(o => o.notCount));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Growl.Error("刷新数据失败:" + ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsLoad = false;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user