MainViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. using GalaSoft.MvvmLight;
  2. using GalaSoft.MvvmLight.Command;
  3. using GalaSoft.MvvmLight.Messaging;
  4. using MuchInfo.Chart.App.Converters;
  5. using MuchInfo.Chart.App.Services;
  6. using MuchInfo.Chart.Data.EnumTypes;
  7. using MuchInfo.Chart.Data.Models;
  8. using MuchInfo.Chart.Infrastructure.Data;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Linq;
  13. using System.Windows;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. namespace MuchInfo.Chart.App.ViewModels
  17. {
  18. /// <summary>
  19. /// This class contains properties that a View can data bind to.
  20. /// <para>
  21. /// See http://www.galasoft.ch/mvvm
  22. /// </para>
  23. /// </summary>
  24. public class MainViewModel : ViewModelBase
  25. {
  26. #region Fields
  27. private readonly IDataService _dataService;
  28. /// <summary>
  29. /// AcceleratedWheelFactor
  30. /// </summary>
  31. private int _acceleratedWheelFactor;
  32. /// <summary>
  33. /// AcceleratedWheelModifierKeys
  34. /// </summary>
  35. private ModifierKeys _acceleratedWheelModifierKeys;
  36. /// <summary>
  37. /// ChartBackground
  38. /// </summary>
  39. private SolidColorBrush _chartBackground;
  40. /// <summary>
  41. /// ChartFontSize
  42. /// </summary>
  43. private double _chartFontSize;
  44. /// <summary>
  45. /// ChartLanguage
  46. /// </summary>
  47. private ChartLanguageType _chartLanguage;
  48. /// <summary>
  49. /// CycleType
  50. /// </summary>
  51. private CycleType _cycleType;
  52. ///// <summary>
  53. ///// DataSet
  54. ///// </summary>
  55. //private ChartDataSet _dataSet;
  56. /// <summary>
  57. /// DrawingToolPosition
  58. /// </summary>
  59. private DrawingToolPositionType _drawingToolPosition;
  60. /// <summary>
  61. /// CurrentDrawingToolType
  62. /// </summary>
  63. private DrawingToolType _drawingToolType;
  64. /// <summary>
  65. /// FillComparisonCandle
  66. /// </summary>
  67. private KeyValuePair<string, bool> _fillComparisonCandle;
  68. private GoodsInfo _goodsInfo;
  69. /// <summary>
  70. /// GridOpacity
  71. /// </summary>
  72. private double _gridOpacity;
  73. private string _indicatorType;
  74. /// <summary>
  75. /// PlotType
  76. /// </summary>
  77. private LinePlotType _linePlotType;
  78. /// <summary>
  79. /// MaxRecordCount
  80. /// </summary>
  81. private int _maxRecordCount;
  82. /// <summary>
  83. /// MinRecordCount
  84. /// </summary>
  85. private int _minRecordCount;
  86. /// <summary>
  87. /// RecordCount
  88. /// </summary>
  89. private int _recordCount;
  90. /// <summary>
  91. /// ShowDrawingTool
  92. /// </summary>
  93. private Visibility _showDrawingTool;
  94. /// <summary>
  95. /// ShowHeader
  96. /// </summary>
  97. private Visibility _showHeader;
  98. /// <summary>
  99. /// SymbolForeground
  100. /// </summary>
  101. private Brush _symbolForeground;
  102. #endregion Fields
  103. #region Constructors
  104. /// <summary>
  105. /// Initializes a new instance of the MainViewModel class.
  106. /// </summary>
  107. public MainViewModel(IDataService dataService)
  108. {
  109. _dataService = dataService;
  110. _chartBackground = new SolidColorBrush(Colors.Black);
  111. _chartFontSize = 12;
  112. _chartLanguage = ChartLanguageType.SimplifiedChinese;
  113. _gridOpacity = 0.15;
  114. _showHeader = Visibility.Visible;
  115. _showDrawingTool = Visibility.Collapsed;
  116. _drawingToolPosition = DrawingToolPositionType.Left;
  117. _drawingToolType = DrawingToolType.Cross;
  118. _maxRecordCount = 5000;
  119. _minRecordCount = 50;
  120. _recordCount = 100;
  121. _maxLoadRecordCount = 2000;
  122. _linePlotType = LinePlotType.Candlestick;
  123. _cycleType = CycleType.Minute;
  124. _acceleratedWheelModifierKeys = System.Windows.Input.ModifierKeys.Control;
  125. _acceleratedWheelFactor = 10;
  126. var openTimes = new List<OpenCloseTime>()
  127. {
  128. new OpenCloseTime()
  129. {
  130. OpenTime =DateTime.Today.AddHours(-14),
  131. CloseTime = DateTime.Today.AddHours(18),
  132. },
  133. // new OpenCloseTime()
  134. //{
  135. // OpenTime =DateTime.Today.AddHours(13),
  136. // CloseTime = DateTime.Today.AddHours(15),
  137. //},
  138. // new OpenCloseTime()
  139. //{
  140. // OpenTime =DateTime.Today.AddHours(17),
  141. // CloseTime = DateTime.Today.AddHours(20),
  142. //},
  143. };
  144. GoodsInfos = new ObservableCollection<GoodsInfo>()
  145. {
  146. new GoodsInfo(100,"XAU1", GoodsType.PreciousMetal, 37, openTimes,1),
  147. new GoodsInfo(100,"XAU10", GoodsType.PreciousMetal, 37, openTimes,1),
  148. new GoodsInfo(100,"AU100", GoodsType.PreciousMetal, 37, openTimes,1),
  149. new GoodsInfo(1,"U1000", GoodsType.PreciousMetal, 37, openTimes,1),
  150. new GoodsInfo(1,"U100", GoodsType.PreciousMetal, 37, openTimes,1),
  151. new GoodsInfo(91,"AUTD", GoodsType.Futures, 37, openTimes,2),
  152. new GoodsInfo(100,"XAUUSD", GoodsType.PreciousMetal, 37, openTimes,2),
  153. new GoodsInfo(100,"XAGUSD", GoodsType.PreciousMetal, 37, openTimes,2)
  154. };
  155. _fillComparisonCandle = BoolDictionary.FirstOrDefault();
  156. Messenger.Default.Register<string>(this, "SetCurrentGoods", (s) =>
  157. {
  158. GoodsInfo = GoodsInfos.FirstOrDefault();
  159. });
  160. }
  161. #endregion Constructors
  162. #region Properties
  163. #region Public Properties
  164. /// <summary>
  165. /// Sets and gets the AcceleratedWheelFactor property.
  166. /// Changes to that property's value raise the PropertyChanged event.
  167. /// </summary>
  168. public int AcceleratedWheelFactor
  169. {
  170. get
  171. {
  172. return _acceleratedWheelFactor;
  173. }
  174. set
  175. {
  176. Set(() => AcceleratedWheelFactor, ref _acceleratedWheelFactor, value);
  177. }
  178. }
  179. private BarDataPoint _TikBarDataPoint;
  180. public BarDataPoint TikBarDataPoint
  181. {
  182. get { return _TikBarDataPoint; }
  183. set { Set(() => TikBarDataPoint, ref _TikBarDataPoint, value); }
  184. }
  185. /// <summary>
  186. /// Sets and gets the AcceleratedWheelModifierKeys property.
  187. /// Changes to that property's value raise the PropertyChanged event.
  188. /// </summary>
  189. public ModifierKeys AcceleratedWheelModifierKeys
  190. {
  191. get
  192. {
  193. return _acceleratedWheelModifierKeys;
  194. }
  195. set
  196. {
  197. Set(() => AcceleratedWheelModifierKeys, ref _acceleratedWheelModifierKeys, value);
  198. }
  199. }
  200. public RelayCommand<MuchInfo.Chart.WPF.Chart> AddIndicatorCommand
  201. {
  202. get
  203. {
  204. return new RelayCommand<WPF.Chart>((chart) => chart.AddIndicator());
  205. }
  206. }
  207. public Dictionary<string, bool> BoolDictionary
  208. {
  209. get
  210. {
  211. return new Dictionary<string, bool>
  212. {
  213. {"否", false},
  214. {"是", true}
  215. };
  216. }
  217. }
  218. public RelayCommand<WPF.Chart> CancelComparisonGoodsCommand
  219. {
  220. get
  221. {
  222. return
  223. new RelayCommand<WPF.Chart>((c) => c.ClearComparison());
  224. }
  225. }
  226. /// <summary>
  227. /// MaxLoadRecordCount
  228. /// </summary>
  229. private int _maxLoadRecordCount;
  230. /// <summary>
  231. /// Sets and gets the MaxLoadRecordCount property.
  232. /// Changes to that property's value raise the PropertyChanged event.
  233. /// </summary>
  234. public int MaxLoadRecordCount
  235. {
  236. get
  237. {
  238. return _maxLoadRecordCount;
  239. }
  240. set
  241. {
  242. Set(() => MaxLoadRecordCount, ref _maxLoadRecordCount, value);
  243. }
  244. }
  245. /// <summary>
  246. /// Sets and gets the ChartBackground property.
  247. /// Changes to that property's value raise the PropertyChanged event.
  248. /// </summary>
  249. public SolidColorBrush ChartBackground
  250. {
  251. get
  252. {
  253. return _chartBackground;
  254. }
  255. set
  256. {
  257. Set(() => ChartBackground, ref _chartBackground, value);
  258. }
  259. }
  260. /// <summary>
  261. /// Sets and gets the ChartFontSize property.
  262. /// Changes to that property's value raise the PropertyChanged event.
  263. /// </summary>
  264. public double ChartFontSize
  265. {
  266. get
  267. {
  268. return _chartFontSize;
  269. }
  270. set
  271. {
  272. Set(() => ChartFontSize, ref _chartFontSize, value);
  273. }
  274. }
  275. /// <summary>
  276. /// Sets and gets the ChartLanguage property.
  277. /// Changes to that property's value raise the PropertyChanged event.
  278. /// </summary>
  279. public ChartLanguageType ChartLanguage
  280. {
  281. get
  282. {
  283. return _chartLanguage;
  284. }
  285. set
  286. {
  287. Set(() => ChartLanguage, ref _chartLanguage, value);
  288. }
  289. }
  290. /// <summary>
  291. /// Gets the comparison goods command.
  292. /// </summary>
  293. /// <value>The comparison goods command.</value>
  294. public RelayCommand<ComparisonGoodsCommandArgs> ComparisonGoodsCommand
  295. {
  296. get
  297. {
  298. return new RelayCommand<ComparisonGoodsCommandArgs>(
  299. (c) =>
  300. {
  301. var list = c.ComparisonListBox.SelectedItems.OfType<ComparisonGoods>().ToList();
  302. c.Chart.SetComparisonGoodsList(list);
  303. });
  304. }
  305. }
  306. /// <summary>
  307. /// Gets the comparison goods list.
  308. /// </summary>
  309. /// <value>The comparison goods list.</value>
  310. public List<ComparisonGoods> ComparisonGoodsList
  311. {
  312. get
  313. {
  314. return new List<ComparisonGoods>()
  315. {
  316. new ComparisonGoods(1,"U1000", GoodsType.PreciousMetal, Colors.Green, 0, null,1),
  317. new ComparisonGoods(1,"U100", GoodsType.PreciousMetal, Colors.Red, 0, null,1),
  318. new ComparisonGoods(91,"AUTD", GoodsType.PreciousMetal, Colors.Silver, 0, null,1),
  319. new ComparisonGoods(100,"XAUUSD", GoodsType.PreciousMetal, Colors.Gold, 0, null,1),
  320. new ComparisonGoods(100,"XAGUSD", GoodsType.PreciousMetal, Colors.LightPink, 0, null,1)
  321. };
  322. }
  323. }
  324. private List<CycleType> _CycleItems;
  325. /// <summary>
  326. /// 图表集合
  327. /// </summary>
  328. public List<CycleType> CycleItems
  329. {
  330. get
  331. {
  332. if (_CycleItems == null || _CycleItems.Count() == 0)
  333. {
  334. var items = new List<CycleType>()
  335. {
  336. CycleType.TimeSharing,
  337. CycleType.Minute,
  338. CycleType.Minute5 ,
  339. CycleType.Minute15,
  340. CycleType.Minute30,
  341. CycleType.Hour,
  342. CycleType.Day,
  343. CycleType.Month,
  344. CycleType.Year,
  345. };
  346. Set(() => CycleItems, ref _CycleItems, items);
  347. }
  348. return _CycleItems;
  349. }
  350. set { Set(() => CycleItems, ref _CycleItems, value); }
  351. }
  352. /// <summary>
  353. /// Sets and gets the CycleType property.
  354. /// Changes to that property's value raise the PropertyChanged event.
  355. /// </summary>
  356. public CycleType CycleType
  357. {
  358. get
  359. {
  360. return _cycleType;
  361. }
  362. set
  363. {
  364. Set(() => CycleType, ref _cycleType, value);
  365. }
  366. }
  367. public List<CycleType> CycleTypes
  368. {
  369. get
  370. {
  371. if (_dataService != null) return _dataService.GetCycleTypes();
  372. return null;
  373. }
  374. }
  375. ///// <summary>
  376. ///// Sets and gets the DataSet property.
  377. ///// Changes to that property's value raise the PropertyChanged event.
  378. ///// </summary>
  379. //public ChartDataSet DataSet
  380. //{
  381. // get
  382. // {
  383. // return _dataSet;
  384. // }
  385. // set
  386. // {
  387. // //对象必需实现INotificationChanged才能通知改变
  388. // _dataSet = value;
  389. // //Set(() => DataSet, ref _dataSet, value);
  390. // }
  391. //}
  392. /// <summary>
  393. /// Sets and gets the DrawingToolPosition property.
  394. /// Changes to that property's value raise the PropertyChanged event.
  395. /// </summary>
  396. public DrawingToolPositionType DrawingToolPosition
  397. {
  398. get
  399. {
  400. return _drawingToolPosition;
  401. }
  402. set
  403. {
  404. Set(() => DrawingToolPosition, ref _drawingToolPosition, value);
  405. //显示画线工具
  406. ShowDrawingTool = Visibility.Visible;
  407. }
  408. }
  409. public List<DrawingToolPositionType> DrawingToolPositions
  410. {
  411. get
  412. {
  413. if (_dataService != null) return _dataService.GetDrawingToolPositions();
  414. return null;
  415. }
  416. }
  417. /// <summary>
  418. /// Sets and gets the CurrentDrawingToolType property.
  419. /// Changes to that property's value raise the PropertyChanged event.
  420. /// </summary>
  421. public DrawingToolType DrawingToolType
  422. {
  423. get
  424. {
  425. return _drawingToolType;
  426. }
  427. set
  428. {
  429. Set(() => DrawingToolType, ref _drawingToolType, value);
  430. }
  431. }
  432. public List<DrawingToolType> DrawingToolTypes
  433. {
  434. get
  435. {
  436. if (_dataService != null) return _dataService.GetDrawingToolTypes();
  437. return null;
  438. }
  439. }
  440. /// <summary>
  441. /// Sets and gets the FillComparisonCandle property.
  442. /// Changes to that property's value raise the PropertyChanged event.
  443. /// </summary>
  444. /// <value>The fill comparison candle.</value>
  445. public KeyValuePair<string, bool> FillComparisonCandle
  446. {
  447. get
  448. {
  449. return _fillComparisonCandle;
  450. }
  451. set
  452. {
  453. Set(() => FillComparisonCandle, ref _fillComparisonCandle, value);
  454. }
  455. }
  456. /// <summary>
  457. /// Sets and gets the MyProperty property.
  458. /// Changes to that property's value raise the PropertyChanged event.
  459. /// </summary>
  460. public GoodsInfo GoodsInfo
  461. {
  462. get
  463. {
  464. return _goodsInfo;
  465. }
  466. set
  467. {
  468. Set(() => GoodsInfo, ref _goodsInfo, value);
  469. }
  470. }
  471. public static ObservableCollection<GoodsInfo> GoodsInfos
  472. {
  473. get;
  474. private set;
  475. }
  476. /// <summary>
  477. /// Sets and gets the GridOpacity property.
  478. /// Changes to that property's value raise the PropertyChanged event.
  479. /// </summary>
  480. public double GridOpacity
  481. {
  482. get
  483. {
  484. return _gridOpacity;
  485. }
  486. set
  487. {
  488. Set(() => GridOpacity, ref _gridOpacity, value);
  489. }
  490. }
  491. /// <summary>
  492. /// Sets and gets the MyProperty property.
  493. /// Changes to that property's value raise the PropertyChanged event.
  494. /// </summary>
  495. public string IndicatorType
  496. {
  497. get
  498. {
  499. return _indicatorType;
  500. }
  501. set
  502. {
  503. Set(() => IndicatorType, ref _indicatorType, value);
  504. }
  505. }
  506. public List<string> IndicatorTypes
  507. {
  508. get
  509. {
  510. if (_dataService != null) return _dataService.GetIndicatorTypes();
  511. return null;
  512. }
  513. }
  514. public Dictionary<ChartLanguageType, string> Languages
  515. {
  516. get
  517. {
  518. if (_dataService != null) return _dataService.GetLanguages();
  519. return null;
  520. }
  521. }
  522. /// <summary>
  523. /// Sets and gets the PlotType property.
  524. /// Changes to that property's value raise the PropertyChanged event.
  525. /// </summary>
  526. public LinePlotType LinePlotType
  527. {
  528. get
  529. {
  530. return _linePlotType;
  531. }
  532. set
  533. {
  534. Set(() => LinePlotType, ref _linePlotType, value);
  535. }
  536. }
  537. public List<LinePlotType> LinePlotTypes
  538. {
  539. get
  540. {
  541. if (_dataService != null) return _dataService.GetLinePlotTypes();
  542. return null;
  543. }
  544. }
  545. /// <summary>
  546. /// Sets and gets the MaxRecordCount property.
  547. /// Changes to that property's value raise the PropertyChanged event.
  548. /// </summary>
  549. public int MaxRecordCount
  550. {
  551. get
  552. {
  553. return _maxRecordCount;
  554. }
  555. set
  556. {
  557. Set(() => MaxRecordCount, ref _maxRecordCount, value);
  558. }
  559. }
  560. /// <summary>
  561. /// Sets and gets the MinRecordCount property.
  562. /// Changes to that property's value raise the PropertyChanged event.
  563. /// </summary>
  564. public int MinRecordCount
  565. {
  566. get
  567. {
  568. return _minRecordCount;
  569. }
  570. set
  571. {
  572. Set(() => MinRecordCount, ref _minRecordCount, value);
  573. }
  574. }
  575. public List<ModifierKeys> ModifierKeys
  576. {
  577. get
  578. {
  579. if (_dataService != null) return _dataService.GetModifierKeys();
  580. return null;
  581. }
  582. }
  583. /// <summary>
  584. /// Sets and gets the RecordCount property.
  585. /// Changes to that property's value raise the PropertyChanged event.
  586. /// </summary>
  587. public int RecordCount
  588. {
  589. get
  590. {
  591. return _recordCount;
  592. }
  593. set
  594. {
  595. Set(() => RecordCount, ref _recordCount, value);
  596. }
  597. }
  598. /// <summary>
  599. /// Sets and gets the ShowDrawingTool property.
  600. /// Changes to that property's value raise the PropertyChanged event.
  601. /// </summary>
  602. public Visibility ShowDrawingTool
  603. {
  604. get
  605. {
  606. return _showDrawingTool;
  607. }
  608. set
  609. {
  610. Set(() => ShowDrawingTool, ref _showDrawingTool, value);
  611. }
  612. }
  613. /// <summary>
  614. /// Sets and gets the ShowHeader property.
  615. /// Changes to that property's value raise the PropertyChanged event.
  616. /// </summary>
  617. public Visibility ShowHeader
  618. {
  619. get
  620. {
  621. return _showHeader;
  622. }
  623. set
  624. {
  625. Set(() => ShowHeader, ref _showHeader, value);
  626. }
  627. }
  628. /// <summary>
  629. /// Sets and gets the SymbolForeground property.
  630. /// Changes to that property's value raise the PropertyChanged event.
  631. /// </summary>
  632. public Brush SymbolForeground
  633. {
  634. get
  635. {
  636. return _symbolForeground;
  637. }
  638. set
  639. {
  640. Set(() => SymbolForeground, ref _symbolForeground, value);
  641. }
  642. }
  643. public List<Visibility> Visibilities
  644. {
  645. get
  646. {
  647. if (_dataService != null) return _dataService.GetVisibilities();
  648. return null;
  649. }
  650. }
  651. public RelayCommand<WPF.Chart> ExportToExcel
  652. {
  653. get
  654. {
  655. return new RelayCommand<WPF.Chart>((chart) => chart.ExportToExcel());
  656. }
  657. }
  658. #endregion Public Properties
  659. #endregion Properties
  660. }
  661. }