StatementPageViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using OxyPlot.Axes;
  2. using OxyPlot.Legends;
  3. using OxyPlot.Series;
  4. using OxyPlot;
  5. using Prism.Commands;
  6. using Prism.Ioc;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Prism.Mvvm;
  13. using Repository;
  14. using Repository.Entiies;
  15. using System.Data;
  16. using System.Windows.Forms;
  17. using TickStyle = OxyPlot.Axes.TickStyle;
  18. namespace LogoForceTestApp.Modules.MainModule.ViewModels
  19. {
  20. public class StatementPageViewModel : BindableBase
  21. {
  22. IRepository repository;
  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. public DelegateCommand StatementCommand { get; set; }
  99. #endregion
  100. public StatementPageViewModel(IRepository repository)
  101. {
  102. this.repository = repository;
  103. StatementCommand = new DelegateCommand(ExecuteLoadedCommand);
  104. }
  105. #region 方法
  106. async void ExecuteLoadedCommand()
  107. {
  108. MonthCount = Count(-30);
  109. WeekCount = Count(-7);
  110. DayCount = Count(-1);
  111. }
  112. public int Count(int day)
  113. {
  114. var list = repository.GetAllQuery<Information>(c => c.CreateTime >= DateTime.Today.AddDays(day) && c.CreateTime <= DateTime.Today.AddDays(1));
  115. return list.Count();
  116. }
  117. /// <summary>
  118. /// 获取当前周每天的产能统计
  119. /// </summary>
  120. public Dictionary<int, Dictionary<string, Int64>> GetProductStatisticalWeek(string ProductName)
  121. {
  122. Dictionary<int, Dictionary<string, Int64>> list = new Dictionary<int, Dictionary<string, long>>();
  123. try
  124. {
  125. DateTime start = DateTime.Now.AddDays(-((int)DateTime.Now.DayOfWeek - 1));
  126. DateTime end = start.AddDays(7);
  127. int index = 1;
  128. while (true)
  129. {
  130. DateTime curent = start.AddDays(index);
  131. list.Add(index, new Dictionary<string, long>());
  132. var result = repository.GetAllQuery<Information>(c => c.CreateTime >= curent.AddDays(-1) && c.CreateTime <= curent && c.ProType == ProductName);
  133. // 检查结果是否为 null
  134. if (result != null)
  135. {
  136. list[index].Add(ProductName, Int64.Parse(result.ToString()));
  137. }
  138. else
  139. {
  140. list[index].Add(ProductName, 0);
  141. }
  142. if (curent >= end)
  143. break;
  144. index++;
  145. }
  146. return list;
  147. }
  148. catch (Exception ex)
  149. {
  150. //LogHelper.WriteLogError("获取当前周每天的产能统计时出错!", ex);
  151. return list;
  152. }
  153. }
  154. #region oxy
  155. /// <summary>
  156. /// 创建产量图表模型
  157. /// </summary>
  158. /// <returns></returns>
  159. private PlotModel CreateYieldChartModel(Dictionary<string, Int64> Items)
  160. {
  161. List<PieSlice> PieSlices = new List<PieSlice>();
  162. PieSlices = new List<PieSlice>();
  163. foreach (var item in Items)
  164. {
  165. PieSlices.Add(new PieSlice(item.Key, (double)item.Value));
  166. }
  167. var model = new PlotModel { Title = "产能统计" };
  168. var series = new PieSeries
  169. {
  170. StrokeThickness = 1.0,
  171. InsideLabelPosition = 0.5,
  172. AngleSpan = 360,
  173. StartAngle = 0
  174. };
  175. foreach (var slice in PieSlices)
  176. {
  177. series.Slices.Add(slice);
  178. }
  179. model.Series.Add(series);
  180. return model;
  181. }
  182. /// <summary>
  183. /// 创建月统计模型
  184. /// </summary>
  185. /// <param name="Items"></param>
  186. /// <returns></returns>
  187. private PlotModel CreateMonth(Dictionary<DateTime, Dictionary<string, Int64>> Items)
  188. {
  189. var model = new PlotModel { Title = "当前月产能统计" };
  190. // 添加图例说明
  191. model.Legends.Add(new Legend
  192. {
  193. LegendPlacement = LegendPlacement.Outside,
  194. LegendPosition = LegendPosition.RightMiddle,
  195. LegendOrientation = LegendOrientation.Vertical,
  196. LegendBorderThickness = 0,
  197. LegendTextColor = OxyColors.LightGray
  198. }); ;
  199. // 定义第一个Y轴y1,显示数量
  200. var ay1 = new LinearAxis()
  201. {
  202. Key = "y1",
  203. Title = "数量",
  204. TitlePosition = 1,
  205. Minimum = 0,
  206. Position = AxisPosition.Left,
  207. IsZoomEnabled = false,
  208. IsPanEnabled = false,
  209. TickStyle = TickStyle.None,
  210. MinorTickSize = 0,
  211. MajorGridlineStyle = LineStyle.Solid,
  212. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  213. };
  214. // 定义X轴为日期轴
  215. var minValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(0));
  216. var maxValue = DateTimeAxis.ToDouble(Items.Keys.ElementAt(Items.Keys.Count - 1));
  217. var ax = new DateTimeAxis()
  218. {
  219. Minimum = minValue,
  220. Maximum = maxValue,
  221. StringFormat = "dd日",
  222. MajorStep = 1,
  223. Position = AxisPosition.Bottom,
  224. Angle = 45,
  225. IsZoomEnabled = false,
  226. IsPanEnabled = false,
  227. TickStyle = TickStyle.None,
  228. MinorTickSize = 0,
  229. MajorGridlineStyle = LineStyle.Solid,
  230. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  231. };
  232. model.Axes.Add(ay1);
  233. model.Axes.Add(ax);
  234. foreach (var category in Items.First().Value.Keys)
  235. {
  236. var barSeries = new LinearBarSeries { Title = category };
  237. barSeries.YAxisKey = "y1";
  238. barSeries.BarWidth = 10;
  239. // 点击时弹出的label内容
  240. barSeries.TrackerFormatString = "{0}\r\n{2:dd}日: {4:0}";
  241. foreach (var item in Items)
  242. {
  243. barSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(item.Key), (double)(item.Value[category])));
  244. }
  245. model.Series.Add(barSeries);
  246. }
  247. var Series = new LineSeries { Title = "总产量" };
  248. Series.YAxisKey = "y1";
  249. Series.TrackerFormatString = "{0}\r\n{2:dd}日: {4:0}";
  250. foreach (var item in Items)
  251. {
  252. Int64 count = 0;
  253. foreach (var item1 in item.Value)
  254. {
  255. count += item1.Value;
  256. }
  257. Series.Points.Add(new DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  258. }
  259. model.Series.Add(Series);
  260. return model;
  261. }
  262. /// <summary>
  263. /// 创建周统计模型
  264. /// </summary>
  265. /// <param name="Items"></param>
  266. /// <returns></returns>
  267. /// <summary>
  268. /// 创建周统计模型
  269. /// </summary>
  270. /// <param name="Items"></param>
  271. /// <returns></returns>
  272. private PlotModel CreateWeek(Dictionary<int, Dictionary<string, Int64>> Items)
  273. {
  274. var model = new PlotModel { Title = "当前周产能统计" };
  275. // 添加图例说明
  276. model.Legends.Add(new Legend
  277. {
  278. LegendPlacement = LegendPlacement.Outside,
  279. LegendPosition = LegendPosition.RightMiddle,
  280. LegendOrientation = LegendOrientation.Vertical,
  281. LegendBorderThickness = 0,
  282. LegendTextColor = OxyColors.LightGray
  283. });
  284. // 定义第一个Y轴y1,显示数量
  285. var ay1 = new LinearAxis()
  286. {
  287. Key = "y1",
  288. Title = "数量",
  289. Minimum = 0,
  290. Position = AxisPosition.Left,
  291. IsZoomEnabled = false,
  292. IsPanEnabled = false,
  293. TickStyle = TickStyle.None,
  294. MinorTickSize = 0,
  295. MajorGridlineStyle = LineStyle.Solid,
  296. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  297. };
  298. // 定义X轴为日期轴
  299. var ax = new LinearAxis()
  300. {
  301. Minimum = 0,
  302. Maximum = 7.5,
  303. MajorStep = 1,
  304. Position = AxisPosition.Bottom,
  305. StringFormat = "周 0",
  306. Angle = 0,
  307. IsZoomEnabled = false,
  308. IsPanEnabled = false,
  309. TickStyle = TickStyle.None,
  310. MinorTickSize = 0,
  311. MajorGridlineStyle = LineStyle.Solid,
  312. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  313. };
  314. model.Axes.Add(ay1);
  315. model.Axes.Add(ax);
  316. foreach (var category in Items.First().Value.Keys)
  317. {
  318. var barSeries = new LinearBarSeries { Title = category };
  319. barSeries.YAxisKey = "y1";
  320. barSeries.BarWidth = 10;
  321. // 点击时弹出的label内容
  322. barSeries.TrackerFormatString = "{0}\r\n周{2:0}: {4:0}";
  323. foreach (var item in Items)
  324. {
  325. barSeries.Points.Add(new DataPoint(LinearAxis.ToDouble(item.Key), (double)(item.Value[category])));
  326. }
  327. model.Series.Add(barSeries);
  328. }
  329. var Series = new LineSeries { Title = "总产量" };
  330. Series.YAxisKey = "y1";
  331. Series.TrackerFormatString = "{0}\r\n{2:dd}日: {4:0}";
  332. foreach (var item in Items)
  333. {
  334. Int64 count = 0;
  335. foreach (var item1 in item.Value)
  336. {
  337. count += item1.Value;
  338. }
  339. Series.Points.Add(new DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  340. }
  341. model.Series.Add(Series);
  342. return model;
  343. }
  344. /// <summary>
  345. /// 创建日统计模型
  346. /// </summary>
  347. /// <param name="Items"></param>
  348. /// <returns></returns>
  349. private PlotModel CreateDay(Dictionary<int, Dictionary<string, Int64>> Items)
  350. {
  351. var model = new PlotModel { Title = "当前日产能统计" };
  352. // 添加图例说明
  353. model.Legends.Add(new Legend
  354. {
  355. LegendPlacement = LegendPlacement.Outside,
  356. LegendPosition = LegendPosition.RightMiddle,
  357. LegendOrientation = LegendOrientation.Vertical,
  358. LegendBorderThickness = 0,
  359. LegendTextColor = OxyColors.LightGray
  360. });
  361. // 定义第一个Y轴y1,显示数量
  362. var ay1 = new LinearAxis()
  363. {
  364. Key = "y1",
  365. Title = "数量",
  366. Minimum = 0,
  367. Position = AxisPosition.Left,
  368. IsZoomEnabled = false,
  369. IsPanEnabled = false,
  370. TickStyle = TickStyle.None,
  371. MinorTickSize = 0,
  372. MajorGridlineStyle = LineStyle.Solid,
  373. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  374. };
  375. // 定义X轴为小时
  376. var ax = new LinearAxis()
  377. {
  378. Minimum = 0,
  379. Maximum = 24.5,
  380. MajorStep = 1,
  381. StringFormat = "0 H",
  382. Position = AxisPosition.Bottom,
  383. Angle = 0,
  384. IsZoomEnabled = false,
  385. IsPanEnabled = false,
  386. TickStyle = TickStyle.None,
  387. MinorTickSize = 0,
  388. MajorGridlineStyle = LineStyle.Solid,
  389. MajorGridlineColor = OxyColor.Parse("#F3F3F3"),
  390. };
  391. model.Axes.Add(ay1);
  392. model.Axes.Add(ax);
  393. foreach (var category in Items.First().Value.Keys)
  394. {
  395. var barSeries = new LinearBarSeries { Title = category };
  396. barSeries.YAxisKey = "y1";
  397. barSeries.BarWidth = 10;
  398. // 点击时弹出的label内容
  399. barSeries.TrackerFormatString = "{0}\r\n{2:0}Hours: {4:0}";
  400. foreach (var item in Items)
  401. {
  402. barSeries.Points.Add(new DataPoint(LinearAxis.ToDouble(item.Key), (double)(item.Value[category])));
  403. }
  404. model.Series.Add(barSeries);
  405. }
  406. var Series = new LineSeries { Title = "总产量" };
  407. Series.YAxisKey = "y1";
  408. Series.TrackerFormatString = "{0}\r\n{2:dd}小时: {4:0}";
  409. foreach (var item in Items)
  410. {
  411. Int64 count = 0;
  412. foreach (var item1 in item.Value)
  413. {
  414. count += item1.Value;
  415. }
  416. Series.Points.Add(new DataPoint(DateTimeAxis.ToDouble(item.Key), (double)count));
  417. }
  418. model.Series.Add(Series);
  419. return model;
  420. }
  421. #endregion
  422. #endregion
  423. }
  424. }