123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- 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;
- /// <summary>
- /// 总产能
- /// </summary>
- public Int64 TotalCount
- {
- get { return _TotalCount; }
- set { SetProperty(ref _TotalCount, value); }
- }
- private Int64 _MonthCount;
- /// <summary>
- /// 月产能
- /// </summary>
- public Int64 MonthCount
- {
- get { return _MonthCount; }
- set { SetProperty(ref _MonthCount, value); }
- }
- private Int64 _WeekCount;
- /// <summary>
- /// 周产能
- /// </summary>
- public Int64 WeekCount
- {
- get { return _WeekCount; }
- set { SetProperty(ref _WeekCount, value); }
- }
- private Int64 _DayCount;
- /// <summary>
- /// 日产能
- /// </summary>
- public Int64 DayCount
- {
- get { return _DayCount; }
- set { SetProperty(ref _DayCount, value); }
- }
- private PlotModel _YieldChartModel;
- /// <summary>
- /// 良率图表Model的mvvm属性,可通知UI更新
- /// </summary>
- public PlotModel YieldChartModel
- {
- get { return _YieldChartModel; }
- set { SetProperty(ref _YieldChartModel, value); }
- }
- private PlotModel _MonthChartModel;
- /// <summary>
- /// 月产能
- /// </summary>
- public PlotModel MonthChartModel
- {
- get { return _MonthChartModel; }
- set { SetProperty(ref _MonthChartModel, value); }
- }
- private PlotModel _WeekChartModel;
- /// <summary>
- /// 周产能
- /// </summary>
- public PlotModel WeekChartModel
- {
- get { return _WeekChartModel; }
- set { SetProperty(ref _WeekChartModel, value); }
- }
- private PlotModel _DayChartModel;
- /// <summary>
- /// 日产能
- /// </summary>
- 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<Information>(c => c.CreateTime >= DateTime.Today.AddDays(day) && c.CreateTime <= DateTime.Today.AddDays(1));
- return list.Count();
- }
- /// <summary>
- /// 获取当前周每天的产能统计
- /// </summary>
- public Dictionary<int, Dictionary<string, Int64>> GetProductStatisticalWeek(string ProductName)
- {
- Dictionary<int, Dictionary<string, Int64>> list = new Dictionary<int, Dictionary<string, long>>();
- 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<string, long>());
- var result = repository.GetAllQuery<Information>(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
- /// <summary>
- /// 创建产量图表模型
- /// </summary>
- /// <returns></returns>
- private PlotModel CreateYieldChartModel(Dictionary<string, Int64> Items)
- {
- List<PieSlice> PieSlices = new List<PieSlice>();
- PieSlices = new List<PieSlice>();
- 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;
- }
- /// <summary>
- /// 创建月统计模型
- /// </summary>
- /// <param name="Items"></param>
- /// <returns></returns>
- private PlotModel CreateMonth(Dictionary<DateTime, Dictionary<string, Int64>> 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;
- }
- /// <summary>
- /// 创建周统计模型
- /// </summary>
- /// <param name="Items"></param>
- /// <returns></returns>
- /// <summary>
- /// 创建周统计模型
- /// </summary>
- /// <param name="Items"></param>
- /// <returns></returns>
- private PlotModel CreateWeek(Dictionary<int, Dictionary<string, Int64>> 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;
- }
- /// <summary>
- /// 创建日统计模型
- /// </summary>
- /// <param name="Items"></param>
- /// <returns></returns>
- private PlotModel CreateDay(Dictionary<int, Dictionary<string, Int64>> 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
- }
- }
|