ProductionStatementViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. using OxyPlot;
  2. using OxyPlot.Axes;
  3. using OxyPlot.Legends;
  4. using OxyPlot.Series;
  5. using Prism.Commands;
  6. using Prism.Ioc;
  7. using Prism.Mvvm;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Windows.Documents;
  12. using TeamAAS_VP.Core;
  13. using TeamAAS_VP.Data;
  14. using TeamAAS_VP.Interfaces;
  15. using TeamAAS_VP.Models;
  16. using TeamAAS_VP.Resources.Languages;
  17. using static MaterialDesignThemes.Wpf.Theme.ToolBar;
  18. namespace TeamAAS_VP.ViewModels.Statistics
  19. {
  20. public class ProductionStatementViewModel : BindableBase
  21. {
  22. IContainerProvider _container;
  23. ISystemDatabaseService _systemDatabaseService;
  24. Management management;
  25. #region 属性
  26. private Int64 _TotalCount;
  27. /// <summary>
  28. /// 总产能
  29. /// </summary>
  30. public Int64 TotalCount
  31. {
  32. get { return _TotalCount; }
  33. set { SetProperty(ref _TotalCount,value); }
  34. }
  35. private Int64 _MonthCount;
  36. /// <summary>
  37. /// 月产能
  38. /// </summary>
  39. public Int64 MonthCount
  40. {
  41. get { return _MonthCount; }
  42. set { SetProperty(ref _MonthCount, value); }
  43. }
  44. private Int64 _WeekCount;
  45. /// <summary>
  46. /// 周产能
  47. /// </summary>
  48. public Int64 WeekCount
  49. {
  50. get { return _WeekCount; }
  51. set { SetProperty(ref _WeekCount, value); }
  52. }
  53. private Int64 _DayCount;
  54. /// <summary>
  55. /// 日产能
  56. /// </summary>
  57. public Int64 DayCount
  58. {
  59. get { return _DayCount; }
  60. set { SetProperty(ref _DayCount, value); }
  61. }
  62. private PlotModel _YieldChartModel;
  63. /// <summary>
  64. /// 良率图表Model的mvvm属性,可通知UI更新
  65. /// </summary>
  66. public PlotModel YieldChartModel
  67. {
  68. get { return _YieldChartModel; }
  69. set { SetProperty(ref _YieldChartModel, value); }
  70. }
  71. private PlotModel _MonthChartModel;
  72. /// <summary>
  73. /// 月产能
  74. /// </summary>
  75. public PlotModel MonthChartModel
  76. {
  77. get { return _MonthChartModel; }
  78. set { SetProperty(ref _MonthChartModel, value); }
  79. }
  80. private PlotModel _WeekChartModel;
  81. /// <summary>
  82. /// 周产能
  83. /// </summary>
  84. public PlotModel WeekChartModel
  85. {
  86. get { return _WeekChartModel; }
  87. set { SetProperty(ref _WeekChartModel, value); }
  88. }
  89. private PlotModel _DayChartModel;
  90. /// <summary>
  91. /// 日产能
  92. /// </summary>
  93. public PlotModel DayChartModel
  94. {
  95. get { return _DayChartModel; }
  96. set { SetProperty(ref _DayChartModel, value); }
  97. }
  98. #endregion
  99. #region 命令
  100. private DelegateCommand _LoadedCommand;
  101. public DelegateCommand LoadedCommand =>
  102. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  103. #endregion
  104. #region 事件
  105. #endregion
  106. public ProductionStatementViewModel(IContainerProvider container, ISystemDatabaseService systemDatabaseService)
  107. {
  108. _container= container;
  109. _systemDatabaseService = systemDatabaseService;
  110. }
  111. #region 方法
  112. async void ExecuteLoadedCommand()
  113. {
  114. if (management == null)
  115. management = _container.Resolve<Management>();
  116. if (management.CurrentProduct == null) return;
  117. await App.Current.Dispatcher.InvokeAsync(new Action(async () => {
  118. YieldChartModel = CreateYieldChartModel(await _systemDatabaseService.GetProductionByCategoriesAsync(management.CurrentProduct.Name));
  119. TotalCount = await _systemDatabaseService.GetOverallProductionAsync(management.CurrentProduct.Name);
  120. MonthCount = await _systemDatabaseService.GetCurrentMonthProductionAsync(management.CurrentProduct.Name);
  121. WeekCount = await _systemDatabaseService.GetCurrentWeekProductionAsync(management.CurrentProduct.Name);
  122. DayCount = await _systemDatabaseService.GetTodayProductionAsync(management.CurrentProduct.Name);
  123. MonthChartModel = CreateMonth(await _systemDatabaseService.GetMonthlyProductionByDayAndCategoryAsync(management.CurrentProduct.Name));
  124. WeekChartModel = CreateWeek(await _systemDatabaseService.GetWeeklyProductionByDayAndCategoryAsync(management.CurrentProduct.Name));
  125. DayChartModel = CreateDay(await _systemDatabaseService.GetDailyProductionByHourAndCategoryAsync(management.CurrentProduct.Name));
  126. }));
  127. }
  128. /// <summary>
  129. /// 创建产量图表模型
  130. /// </summary>
  131. /// <returns></returns>
  132. private PlotModel CreateYieldChartModel(Dictionary<string,int> Items)
  133. {
  134. List<PieSlice> PieSlices = new List<PieSlice>();
  135. PieSlices = new List<PieSlice>();
  136. foreach (var item in Items)
  137. {
  138. PieSlices.Add(new PieSlice(item.Key, (double)item.Value));
  139. }
  140. var model = new PlotModel { Title = Lang.产能统计 };
  141. var series = new PieSeries
  142. {
  143. StrokeThickness = 1.0,
  144. InsideLabelPosition = 0.5,
  145. AngleSpan = 360,
  146. StartAngle = 0
  147. };
  148. foreach (var slice in PieSlices)
  149. {
  150. series.Slices.Add(slice);
  151. }
  152. model.Series.Add(series);
  153. return model;
  154. }
  155. /// <summary>
  156. /// 创建月统计模型
  157. /// </summary>
  158. /// <param name="Items"></param>
  159. /// <returns></returns>
  160. private PlotModel CreateMonth(Dictionary<DateTime, Dictionary<string, int>> Items)
  161. {
  162. var model = new PlotModel { Title = Lang.当前月产能统计 };
  163. // 添加图例说明
  164. model.Legends.Add(new Legend
  165. {
  166. LegendPlacement = LegendPlacement.Outside,
  167. LegendPosition = LegendPosition.RightMiddle,
  168. LegendOrientation = LegendOrientation.Vertical,
  169. LegendBorderThickness = 0,
  170. LegendTextColor = OxyColors.LightGray
  171. }); ;
  172. // 定义第一个Y轴y1,显示数量
  173. var ay1 = new LinearAxis()
  174. {
  175. Key = "y1",
  176. Title= Lang.数量,
  177. TitlePosition=1,
  178. Minimum = 0,
  179. Position = AxisPosition.Left,
  180. IsZoomEnabled = false,
  181. IsPanEnabled = false,
  182. TickStyle = TickStyle.None,
  183. MinorTickSize = 0,
  184. MajorGridlineStyle = LineStyle.Solid,
  185. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  186. };
  187. // 定义X轴为日期轴
  188. var minValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(0));
  189. var maxValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(Items.Keys.Count-1));
  190. var ax = new DateTimeAxis()
  191. {
  192. Minimum = minValue,
  193. Maximum = maxValue,
  194. StringFormat = $"dd{Lang.日}",
  195. MajorStep = 1,
  196. Position = AxisPosition.Bottom,
  197. Angle = 45,
  198. IsZoomEnabled = false,
  199. IsPanEnabled = false,
  200. TickStyle = TickStyle.None,
  201. MinorTickSize = 0,
  202. MajorGridlineStyle = LineStyle.Solid,
  203. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  204. };
  205. model.Axes.Add(ay1);
  206. model.Axes.Add(ax);
  207. foreach (var category in Items.First().Value.Keys)
  208. {
  209. var barSeries = new LinearBarSeries { Title = category };
  210. barSeries.YAxisKey = "y1";
  211. barSeries.BarWidth = 10;
  212. // 点击时弹出的label内容
  213. barSeries.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  214. foreach (var item in Items)
  215. {
  216. barSeries.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
  217. }
  218. model.Series.Add(barSeries);
  219. }
  220. var Series = new LineSeries { Title = Lang.总产量 };
  221. Series.YAxisKey = "y1";
  222. Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  223. foreach (var item in Items)
  224. {
  225. Int64 count = 0;
  226. foreach (var item1 in item.Value)
  227. {
  228. count += item1.Value;
  229. }
  230. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  231. }
  232. model.Series.Add(Series);
  233. return model;
  234. }
  235. /// <summary>
  236. /// 创建周统计模型
  237. /// </summary>
  238. /// <param name="Items"></param>
  239. /// <returns></returns>
  240. private PlotModel CreateWeek(Dictionary<int, Dictionary<string, int>> Items)
  241. {
  242. var model = new PlotModel { Title = Lang.当前周产能统计 };
  243. // 添加图例说明
  244. model.Legends.Add(new Legend
  245. {
  246. LegendPlacement = LegendPlacement.Outside,
  247. LegendPosition = LegendPosition.RightMiddle,
  248. LegendOrientation = LegendOrientation.Vertical,
  249. LegendBorderThickness = 0,
  250. LegendTextColor = OxyColors.LightGray
  251. });
  252. // 定义第一个Y轴y1,显示数量
  253. var ay1 = new LinearAxis()
  254. {
  255. Key = "y1",
  256. Title = Lang.数量,
  257. Minimum = 0,
  258. Position = AxisPosition.Left,
  259. IsZoomEnabled = false,
  260. IsPanEnabled = false,
  261. TickStyle = TickStyle.None,
  262. MinorTickSize = 0,
  263. MajorGridlineStyle = LineStyle.Solid,
  264. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  265. };
  266. // 定义X轴为日期轴
  267. var ax = new LinearAxis()
  268. {
  269. Minimum = 0,
  270. Maximum = 7.5,
  271. MajorStep = 1,
  272. Position = AxisPosition.Bottom,
  273. StringFormat= $"{Lang.周} 0",
  274. Angle = 0,
  275. IsZoomEnabled = false,
  276. IsPanEnabled = false,
  277. TickStyle = TickStyle.None,
  278. MinorTickSize = 0,
  279. MajorGridlineStyle = LineStyle.Solid,
  280. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  281. };
  282. model.Axes.Add(ay1);
  283. model.Axes.Add(ax);
  284. foreach (var category in Items.First().Value.Keys)
  285. {
  286. var barSeries = new LinearBarSeries { Title = category };
  287. barSeries.YAxisKey = "y1";
  288. barSeries.BarWidth = 10;
  289. // 点击时弹出的label内容
  290. barSeries.TrackerFormatString = $"{0}\r\n{Lang.周}{2:0}: {4:0}";
  291. foreach (var item in Items)
  292. {
  293. barSeries.Points.Add(new OxyPlot.DataPoint(LinearAxis.ToDouble(item.Key), (double)(item.Value[category])));
  294. }
  295. model.Series.Add(barSeries);
  296. }
  297. var Series = new LineSeries { Title = Lang.当前周产能统计 };
  298. Series.YAxisKey = "y1";
  299. Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.日}: {4:0}";
  300. foreach (var item in Items)
  301. {
  302. Int64 count = 0;
  303. foreach (var item1 in item.Value)
  304. {
  305. count += item1.Value;
  306. }
  307. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  308. }
  309. model.Series.Add(Series);
  310. return model;
  311. }
  312. /// <summary>
  313. /// 创建日统计模型
  314. /// </summary>
  315. /// <param name="Items"></param>
  316. /// <returns></returns>
  317. private PlotModel CreateDay(Dictionary<int, Dictionary<string, int>> Items)
  318. {
  319. var model = new PlotModel { Title = Lang.当前日产能统计 };
  320. // 添加图例说明
  321. model.Legends.Add(new Legend
  322. {
  323. LegendPlacement = LegendPlacement.Outside,
  324. LegendPosition = LegendPosition.RightMiddle,
  325. LegendOrientation = LegendOrientation.Vertical,
  326. LegendBorderThickness = 0,
  327. LegendTextColor = OxyColors.LightGray
  328. });
  329. // 定义第一个Y轴y1,显示数量
  330. var ay1 = new LinearAxis()
  331. {
  332. Key = "y1",
  333. Title = $"{Lang.数量}",
  334. Minimum = 0,
  335. Position = AxisPosition.Left,
  336. IsZoomEnabled = false,
  337. IsPanEnabled = false,
  338. TickStyle = TickStyle.None,
  339. MinorTickSize = 0,
  340. MajorGridlineStyle = LineStyle.Solid,
  341. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  342. };
  343. // 定义X轴为小时
  344. var ax = new LinearAxis()
  345. {
  346. Minimum = 0,
  347. Maximum = 24.5,
  348. MajorStep = 1,
  349. StringFormat = "0 H",
  350. Position = AxisPosition.Bottom,
  351. Angle = 0,
  352. IsZoomEnabled = false,
  353. IsPanEnabled = false,
  354. TickStyle = TickStyle.None,
  355. MinorTickSize = 0,
  356. MajorGridlineStyle = LineStyle.Solid,
  357. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  358. };
  359. model.Axes.Add(ay1);
  360. model.Axes.Add(ax);
  361. foreach (var category in Items.First().Value.Keys)
  362. {
  363. var barSeries = new LinearBarSeries { Title = category };
  364. barSeries.YAxisKey = "y1";
  365. barSeries.BarWidth = 10;
  366. // 点击时弹出的label内容
  367. barSeries.TrackerFormatString = "{0}\r\n{2:0}Hours: {4:0}";
  368. foreach (var item in Items)
  369. {
  370. barSeries.Points.Add(new OxyPlot.DataPoint(LinearAxis.ToDouble(item.Key), (double)(item.Value[category])));
  371. }
  372. model.Series.Add(barSeries);
  373. }
  374. var Series = new LineSeries { Title = Lang.总产量 };
  375. Series.YAxisKey = "y1";
  376. Series.TrackerFormatString = $"{0}\r\n{2:dd}{Lang.小时}: {4:0}";
  377. foreach (var item in Items)
  378. {
  379. Int64 count = 0;
  380. foreach (var item1 in item.Value)
  381. {
  382. count += item1.Value;
  383. }
  384. Series.Points.Add(new OxyPlot.DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  385. }
  386. model.Series.Add(Series);
  387. return model;
  388. }
  389. #endregion
  390. }
  391. }