ProductionStatementViewModel.cs 15 KB

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