using OxyPlot.Axes; using OxyPlot.Legends; using OxyPlot.Series; using OxyPlot; using Prism.Commands; using Prism.Ioc; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Prism.Mvvm; using Repository; using Repository.Entiies; using System.Data; using System.Windows.Forms; using TickStyle = OxyPlot.Axes.TickStyle; namespace LogoForceTestApp.Modules.MainModule.ViewModels { public class StatementPageViewModel : BindableBase { IRepository repository; #region 属性 private Int64 _TotalCount; /// /// 总产能 /// public Int64 TotalCount { get { return _TotalCount; } set { SetProperty(ref _TotalCount, value); } } private Int64 _MonthCount; /// /// 月产能 /// public Int64 MonthCount { get { return _MonthCount; } set { SetProperty(ref _MonthCount, value); } } private Int64 _WeekCount; /// /// 周产能 /// public Int64 WeekCount { get { return _WeekCount; } set { SetProperty(ref _WeekCount, value); } } private Int64 _DayCount; /// /// 日产能 /// public Int64 DayCount { get { return _DayCount; } set { SetProperty(ref _DayCount, value); } } private PlotModel _YieldChartModel; /// /// 良率图表Model的mvvm属性,可通知UI更新 /// public PlotModel YieldChartModel { get { return _YieldChartModel; } set { SetProperty(ref _YieldChartModel, value); } } private PlotModel _MonthChartModel; /// /// 月产能 /// public PlotModel MonthChartModel { get { return _MonthChartModel; } set { SetProperty(ref _MonthChartModel, value); } } private PlotModel _WeekChartModel; /// /// 周产能 /// public PlotModel WeekChartModel { get { return _WeekChartModel; } set { SetProperty(ref _WeekChartModel, value); } } private PlotModel _DayChartModel; /// /// 日产能 /// public PlotModel DayChartModel { get { return _DayChartModel; } set { SetProperty(ref _DayChartModel, value); } } #endregion #region 命令 public DelegateCommand StatementCommand { get; set; } #endregion public StatementPageViewModel(IRepository repository) { this.repository = repository; StatementCommand = new DelegateCommand(ExecuteLoadedCommand); } #region 方法 async void ExecuteLoadedCommand() { MonthCount = Count(-30); WeekCount = Count(-7); DayCount = Count(-1); } public int Count(int day) { var list = repository.GetAllQuery(c => c.CreateTime >= DateTime.Today.AddDays(day) && c.CreateTime <= DateTime.Today.AddDays(1)); return list.Count(); } /// /// 获取当前周每天的产能统计 /// public Dictionary> GetProductStatisticalWeek(string ProductName) { Dictionary> list = new Dictionary>(); try { DateTime start = DateTime.Now.AddDays(-((int)DateTime.Now.DayOfWeek - 1)); DateTime end = start.AddDays(7); int index = 1; while (true) { DateTime curent = start.AddDays(index); list.Add(index, new Dictionary()); var result = repository.GetAllQuery(c => c.CreateTime >= curent.AddDays(-1) && c.CreateTime <= curent && c.ProType == ProductName); // 检查结果是否为 null if (result != null) { list[index].Add(ProductName, Int64.Parse(result.ToString())); } else { list[index].Add(ProductName, 0); } if (curent >= end) break; index++; } return list; } catch (Exception ex) { //LogHelper.WriteLogError("获取当前周每天的产能统计时出错!", ex); return list; } } #region oxy /// /// 创建产量图表模型 /// /// private PlotModel CreateYieldChartModel(Dictionary Items) { List PieSlices = new List(); PieSlices = new List(); foreach (var item in Items) { PieSlices.Add(new PieSlice(item.Key, (double)item.Value)); } var model = new PlotModel { Title = "产能统计" }; var series = new PieSeries { StrokeThickness = 1.0, InsideLabelPosition = 0.5, AngleSpan = 360, StartAngle = 0 }; foreach (var slice in PieSlices) { series.Slices.Add(slice); } model.Series.Add(series); return model; } /// /// 创建月统计模型 /// /// /// private PlotModel CreateMonth(Dictionary> Items) { var model = new PlotModel { Title = "当前月产能统计" }; // 添加图例说明 model.Legends.Add(new Legend { LegendPlacement = LegendPlacement.Outside, LegendPosition = LegendPosition.RightMiddle, LegendOrientation = LegendOrientation.Vertical, LegendBorderThickness = 0, LegendTextColor = OxyColors.LightGray }); ; // 定义第一个Y轴y1,显示数量 var ay1 = new LinearAxis() { Key = "y1", Title = "数量", TitlePosition = 1, Minimum = 0, Position = AxisPosition.Left, IsZoomEnabled = false, IsPanEnabled = false, TickStyle = TickStyle.None, MinorTickSize = 0, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.Parse("#F3F3F3"), }; // 定义X轴为日期轴 var minValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(0)); var maxValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(Items.Keys.Count - 1)); var ax = new DateTimeAxis() { Minimum = minValue, Maximum = maxValue, StringFormat = "dd日", MajorStep = 1, Position = AxisPosition.Bottom, Angle = 45, IsZoomEnabled = false, IsPanEnabled = false, TickStyle = TickStyle.None, MinorTickSize = 0, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.Parse("#F3F3F3"), }; model.Axes.Add(ay1); model.Axes.Add(ax); foreach (var category in Items.First().Value.Keys) { var barSeries = new LinearBarSeries { Title = category }; barSeries.YAxisKey = "y1"; barSeries.BarWidth = 10; // 点击时弹出的label内容 barSeries.TrackerFormatString = "{0}\r\n{2:dd}日: {4:0}"; foreach (var item in Items) { barSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category]))); } model.Series.Add(barSeries); } var Series = new LineSeries { Title = "总产量" }; Series.YAxisKey = "y1"; Series.TrackerFormatString = "{0}\r\n{2:dd}日: {4:0}"; foreach (var item in Items) { Int64 count = 0; foreach (var item1 in item.Value) { count += item1.Value; } Series.Points.Add(new DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count)); } model.Series.Add(Series); return model; } /// /// 创建周统计模型 /// /// /// /// /// 创建周统计模型 /// /// /// private PlotModel CreateWeek(Dictionary> Items) { var model = new PlotModel { Title = "当前周产能统计" }; // 添加图例说明 model.Legends.Add(new Legend { LegendPlacement = LegendPlacement.Outside, LegendPosition = LegendPosition.RightMiddle, LegendOrientation = LegendOrientation.Vertical, LegendBorderThickness = 0, LegendTextColor = OxyColors.LightGray }); // 定义第一个Y轴y1,显示数量 var ay1 = new LinearAxis() { Key = "y1", Title = "数量", Minimum = 0, Position = AxisPosition.Left, IsZoomEnabled = false, IsPanEnabled = false, TickStyle = TickStyle.None, MinorTickSize = 0, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.Parse("#F3F3F3"), }; // 定义X轴为日期轴 var ax = new LinearAxis() { Minimum = 0, Maximum = 7.5, MajorStep = 1, Position = AxisPosition.Bottom, StringFormat = "周 0", Angle = 0, IsZoomEnabled = false, IsPanEnabled = false, TickStyle = TickStyle.None, MinorTickSize = 0, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.Parse("#F3F3F3"), }; model.Axes.Add(ay1); model.Axes.Add(ax); foreach (var category in Items.First().Value.Keys) { var barSeries = new LinearBarSeries { Title = category }; barSeries.YAxisKey = "y1"; barSeries.BarWidth = 10; // 点击时弹出的label内容 barSeries.TrackerFormatString = "{0}\r\n周{2:0}: {4:0}"; foreach (var item in Items) { barSeries.Points.Add(new DataPoint(LinearAxis.ToDouble(item.Key), (double)(item.Value[category]))); } model.Series.Add(barSeries); } var Series = new LineSeries { Title = "总产量" }; Series.YAxisKey = "y1"; Series.TrackerFormatString = "{0}\r\n{2:dd}日: {4:0}"; foreach (var item in Items) { Int64 count = 0; foreach (var item1 in item.Value) { count += item1.Value; } Series.Points.Add(new DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count)); } model.Series.Add(Series); return model; } /// /// 创建日统计模型 /// /// /// private PlotModel CreateDay(Dictionary> Items) { var model = new PlotModel { Title = "当前日产能统计" }; // 添加图例说明 model.Legends.Add(new Legend { LegendPlacement = LegendPlacement.Outside, LegendPosition = LegendPosition.RightMiddle, LegendOrientation = LegendOrientation.Vertical, LegendBorderThickness = 0, LegendTextColor = OxyColors.LightGray }); // 定义第一个Y轴y1,显示数量 var ay1 = new LinearAxis() { Key = "y1", Title = "数量", Minimum = 0, Position = AxisPosition.Left, IsZoomEnabled = false, IsPanEnabled = false, TickStyle = TickStyle.None, MinorTickSize = 0, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.Parse("#F3F3F3"), }; // 定义X轴为小时 var ax = new LinearAxis() { Minimum = 0, Maximum = 24.5, MajorStep = 1, StringFormat = "0 H", Position = AxisPosition.Bottom, Angle = 0, IsZoomEnabled = false, IsPanEnabled = false, TickStyle = TickStyle.None, MinorTickSize = 0, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.Parse("#F3F3F3"), }; model.Axes.Add(ay1); model.Axes.Add(ax); foreach (var category in Items.First().Value.Keys) { var barSeries = new LinearBarSeries { Title = category }; barSeries.YAxisKey = "y1"; barSeries.BarWidth = 10; // 点击时弹出的label内容 barSeries.TrackerFormatString = "{0}\r\n{2:0}Hours: {4:0}"; foreach (var item in Items) { barSeries.Points.Add(new DataPoint(LinearAxis.ToDouble(item.Key), (double)(item.Value[category]))); } model.Series.Add(barSeries); } var Series = new LineSeries { Title = "总产量" }; Series.YAxisKey = "y1"; Series.TrackerFormatString = "{0}\r\n{2:dd}小时: {4:0}"; foreach (var item in Items) { Int64 count = 0; foreach (var item1 in item.Value) { count += item1.Value; } Series.Points.Add(new DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count)); } model.Series.Add(Series); return model; } #endregion #endregion } }