ScaleLayer.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. using Microsoft.VisualBasic.CompilerServices;
  2. using MuchInfo.Chart.Data;
  3. using MuchInfo.Chart.Data.Interfaces;
  4. using MuchInfo.Chart.WPF.Helpers;
  5. using MuchInfo.Chart.WPF.Primitives.Drawing;
  6. using MuchInfo.Chart.WPF.Primitives.Interfaces;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Windows;
  11. namespace MuchInfo.Chart.WPF.Primitives
  12. {
  13. public class ScaleLayer : IDeletable
  14. {
  15. #region Fields
  16. /// <summary>
  17. /// AllPlots
  18. /// </summary>
  19. private List<DisplayFormula> _allFormula;
  20. private Chart _chart;
  21. private List<IChartHighlight> _chartHighlights;
  22. private ChartPane _chartPane;
  23. private DrawingToolContainer _drawingToolContainer;
  24. private List<IDrawingTool> _drawingTools;
  25. private Guid _guid;
  26. private SharedDrawingToolContainer _sharedDrawingToolContainer;
  27. private string _symbol;
  28. private ValueScaleDisplay _rightValueScaleDisplay;
  29. private ValueScaleDisplay _leftValueScaleDisplay;
  30. /// <summary>
  31. /// mScaler
  32. /// </summary>
  33. private IValueScaler _rightValueScaler;
  34. private IValueScaler _leftValueScaler;
  35. #endregion Fields
  36. #region Constructors
  37. public ScaleLayer()
  38. {
  39. _allFormula = new List<DisplayFormula>();
  40. RightScaler = new ValueScalerArithmetic(false);
  41. LeftScaler = new ValueScalerArithmetic(false);
  42. _rightValueScaleDisplay = new ValueScaleDisplay(RightScaler);
  43. _leftValueScaleDisplay = new ValueScaleDisplay(LeftScaler);
  44. _guid = Guid.NewGuid();
  45. _drawingToolContainer = new DrawingToolContainer(this);
  46. _sharedDrawingToolContainer = new SharedDrawingToolContainer(this);
  47. _chartHighlights = null;
  48. _rightValueScaleDisplay.ScaleLayer = this;
  49. _leftValueScaleDisplay.ScaleLayer = this;
  50. AllDisplayFormula=new List<DisplayFormula>();
  51. SharedDrawingManager.AllDrawingsChanged -= new SharedDrawingManager.AllDrawingsChangedEventHandler(OnAllDrawingsChanged);
  52. SharedDrawingManager.DrawingChanged -= new SharedDrawingManager.DrawingChangedEventHandler(OnDrawingChanged);
  53. SharedDrawingManager.DrawingsChangedForSym -= new SharedDrawingManager.DrawingsChangedForSymEventHandler(OnDrawingsChangedForSym);
  54. SharedDrawingManager.AllDrawingsChanged += new SharedDrawingManager.AllDrawingsChangedEventHandler(OnAllDrawingsChanged);
  55. SharedDrawingManager.DrawingChanged += new SharedDrawingManager.DrawingChangedEventHandler(OnDrawingChanged);
  56. SharedDrawingManager.DrawingsChangedForSym += new SharedDrawingManager.DrawingsChangedForSymEventHandler(OnDrawingsChangedForSym);
  57. }
  58. #endregion Constructors
  59. #region Properties
  60. #region Public Properties
  61. /// <summary>
  62. /// 层中显示的所有公式
  63. /// </summary>
  64. public List<DisplayFormula> AllDisplayFormula
  65. {
  66. get
  67. {
  68. return _allFormula;
  69. }
  70. private set
  71. {
  72. _allFormula = value;
  73. }
  74. }
  75. private List<DisplayFormula> _comparisonFormulas = new List<DisplayFormula>();
  76. /// <summary>
  77. /// 层中显示的所有公式
  78. /// </summary>
  79. public List<DisplayFormula> ComparisonFormulas
  80. {
  81. get
  82. {
  83. return _comparisonFormulas;
  84. }
  85. private set
  86. {
  87. _comparisonFormulas = value;
  88. }
  89. }
  90. /// <summary>
  91. /// 是否为主图,区别分Y轴坐标是否显示百分比
  92. /// </summary>
  93. internal bool IsMainPlot
  94. {
  95. get
  96. {
  97. bool result = false;
  98. foreach (var formula in AllDisplayFormula)
  99. {
  100. if (formula.FormulaModel != null && formula.FormulaModel.IsMain)
  101. {
  102. result = formula.FormulaModel.IsMain;
  103. break;
  104. }
  105. }
  106. return result;
  107. }
  108. }
  109. private List<PlotBase> _allPlotbases = new List<PlotBase>();
  110. /// <summary>
  111. /// 获取层中所有的图表
  112. /// </summary>
  113. public List<PlotBase> GetAllPlots()
  114. {
  115. return _allPlotbases;
  116. }
  117. /// <summary>
  118. /// 获取层中所有的图表
  119. /// </summary>
  120. public List<PlotBase> AllPlots
  121. {
  122. get
  123. {
  124. return _allPlotbases;
  125. }
  126. }
  127. public IValueScaler RightScaler
  128. {
  129. get
  130. {
  131. return _rightValueScaler;
  132. }
  133. set
  134. {
  135. var obj = new ScalePropertiesChangedEventHandler(ScaleLayer_5544);
  136. bool flag = _rightValueScaler != null;
  137. if (flag)
  138. {
  139. _rightValueScaler.ScalePropertiesChanged -= obj;
  140. }
  141. _rightValueScaler = value;
  142. flag = (_rightValueScaler != null);
  143. if (flag)
  144. {
  145. _rightValueScaler.ScalePropertiesChanged += obj;
  146. }
  147. }
  148. }
  149. public IValueScaler LeftScaler
  150. {
  151. get
  152. {
  153. return _leftValueScaler;
  154. }
  155. set
  156. {
  157. var obj = new ScalePropertiesChangedEventHandler(ScaleLayer_5544);
  158. bool flag = _leftValueScaler != null;
  159. if (flag)
  160. {
  161. _leftValueScaler.ScalePropertiesChanged -= obj;
  162. }
  163. _leftValueScaler = value;
  164. flag = (_leftValueScaler != null);
  165. if (flag)
  166. {
  167. _leftValueScaler.ScalePropertiesChanged += obj;
  168. }
  169. }
  170. }
  171. public Guid ScaleGUID
  172. {
  173. get
  174. {
  175. return _guid;
  176. }
  177. }
  178. #endregion Public Properties
  179. #endregion Properties
  180. #region Methods
  181. #region Public Methods
  182. public IDrawingToolSpec AddDrawing(string symbol, IDrawingTool aDrawing, TimeSpan aTimeFrame)
  183. {
  184. // _drawingTools = null;
  185. //return ScaleLayer_1916.AddDrawing(symbol, aDrawing);
  186. return _sharedDrawingToolContainer.AddNewDrawing(symbol, aDrawing, aTimeFrame, _guid, HasPricePlot());
  187. //bool flag = false; //BFCSettings.RunMode() == BFCSettings.eRunMode.eBFCWhiteLabel;
  188. //IDrawingToolSpec result;
  189. //if (flag)
  190. //{
  191. // result = ScaleLayer_1916.AddDrawing(aSym, aDrawing);
  192. //}
  193. //else
  194. //{
  195. // result = ScaleLayer_1917.AddNewDrawing(aSym, aDrawing, aTimeFrame, ScaleLayer_1915, ScaleLayer_5527());
  196. //}
  197. //return result;
  198. }
  199. public IDrawingToolSpec AddDrawing(string symbol, IDrawingTool aDrawing)
  200. {
  201. //var Rlt = new IDrawingToolSpec();
  202. _drawingTools = null;
  203. //if (_chart != null)
  204. //{
  205. return _sharedDrawingToolContainer.AddNewDrawing(symbol, aDrawing, _chart.CurrentTimeFrame, _guid, HasPricePlot());
  206. //}
  207. //IDrawingToolSpec result;
  208. //if (flag)
  209. //{
  210. // result = ScaleLayer_1916.AddDrawing(aSym, aDrawing);
  211. //}
  212. //else
  213. //{
  214. // result = ScaleLayer_1917.AddNewDrawing(symbol, aDrawing, _chart.CurrentTimeFrame, ScaleLayer_1915, ScaleLayer_5527());
  215. //}
  216. //return Rlt;
  217. }
  218. /// <summary>
  219. /// 清除所有线与公式
  220. /// </summary>
  221. public void ClearFormulas()
  222. {
  223. foreach (var displayFormula in AllDisplayFormula)
  224. {
  225. if (displayFormula.FormulaModel != null &&
  226. !displayFormula.FormulaModel.IsMain && displayFormula.PlotBases!=null)
  227. {
  228. foreach (var plotBase in displayFormula.PlotBases)
  229. {
  230. this.AllPlots.Remove(plotBase);
  231. }
  232. }
  233. }
  234. //清除所有公式除主图外
  235. AllDisplayFormula.RemoveAll(z => !z.FormulaModel.IsMain);
  236. }
  237. public DrawingToolContainer AllXMLDrawings()
  238. {
  239. return _drawingToolContainer;
  240. }
  241. public PlotBase FirstPlot()
  242. {
  243. return null;
  244. }
  245. public void OnDelete()
  246. {
  247. RightScaler = null;
  248. LeftScaler = null;
  249. var list = new List<PlotBase>();
  250. foreach (var formula in AllDisplayFormula)
  251. {
  252. list.AddRange(formula.PlotBases);
  253. }
  254. try
  255. {
  256. List<PlotBase>.Enumerator enumerator = list.GetEnumerator();
  257. while (enumerator.MoveNext())
  258. {
  259. PlotBase current = enumerator.Current;
  260. current.RemovedFromChart();
  261. }
  262. }
  263. finally
  264. {
  265. //List<PlotBase>.Enumerator enumerator;
  266. //enumerator.Dispose();
  267. }
  268. SharedDrawingManager.AllDrawingsChanged -= new SharedDrawingManager.AllDrawingsChangedEventHandler(OnAllDrawingsChanged);
  269. SharedDrawingManager.DrawingChanged -= new SharedDrawingManager.DrawingChangedEventHandler(OnDrawingChanged);
  270. SharedDrawingManager.DrawingsChangedForSym -= new SharedDrawingManager.DrawingsChangedForSymEventHandler(OnDrawingsChangedForSym);
  271. }
  272. #endregion Public Methods
  273. #region Internal Methods
  274. internal List<FrameworkElement> BuildScaleLayer(Chart owner, ChartPane aPane, IDateScaler dateScale, Rect aRect)
  275. {
  276. _chart = owner;
  277. _chartPane = aPane;
  278. //if (_chartPane.IsPriceHistory)
  279. //{
  280. if (IsMainPlot || aPane.IsTimeSpaning) //分时图显示百分比
  281. {
  282. // _leftValueScaleDisplay.CurrentValueScaler.ShowScaleAsPercent = true;
  283. //_rightValueScaleDisplay.CurrentValueScaler.ShowScaleAsPercent = true;
  284. }
  285. // }
  286. var list = new List<FrameworkElement>();
  287. bool flag = _rightValueScaleDisplay != null;
  288. //只针对左边显示数据,右边显示百分比。
  289. List<ValueScaleDisplay.ValueScaleDisplay_Y> ylist = null;
  290. if (flag)
  291. {
  292. RightScaler.IsTimeSpaning = aPane.IsTimeSpaning;
  293. if (aPane.IsTimeSpaning)
  294. {
  295. RightScaler.TimeSpaningLast = owner.CurrentGoods.PreClose; //分时图收盘价
  296. }
  297. RightScaler.CalculateScale(AllPlots, dateScale, aPane.IsTimeSpaning);
  298. // list.AddRange(_leftValueScaleDisplay.GetValueScaleDisplay(owner, dateScale, aRect, null, true, aPane.IsTimeSpaning));
  299. list.AddRange(_rightValueScaleDisplay.GetValueScaleDisplay(owner, dateScale, aRect, null, true, aPane.IsTimeSpaning));
  300. //左边为数值,取左边数据为右边做参照
  301. // ylist = _leftValueScaleDisplay.GetYList((float)aRect.Height, LeftScaler, aPane.IsTimeSpaning);
  302. ylist = _rightValueScaleDisplay.GetYList((float)aRect.Height, RightScaler, aPane.IsTimeSpaning);
  303. }
  304. flag = _leftValueScaleDisplay != null;
  305. if (flag)
  306. {
  307. // RightScaler.IsTimeSpaning = aPane.IsTimeSpaning;
  308. LeftScaler.IsTimeSpaning = aPane.IsTimeSpaning;
  309. if (aPane.IsTimeSpaning)
  310. {
  311. // RightScaler.TimeSpaningLast = owner.CurrentGoods.PreClose; //分时图收盘价
  312. LeftScaler.TimeSpaningLast = owner.CurrentGoods.PreClose; //分时图收盘价
  313. }
  314. // RightScaler.CalculateScale(AllPlots, dateScale, aPane.IsTimeSpaning);
  315. LeftScaler.CalculateScale(AllPlots, dateScale, aPane.IsTimeSpaning);
  316. // list.AddRange(_rightValueScaleDisplay.GetValueScaleDisplay(owner, dateScale, aRect, ylist, false, aPane.IsTimeSpaning));
  317. list.AddRange(_leftValueScaleDisplay.GetValueScaleDisplay(owner, dateScale, aRect, ylist, false, aPane.IsTimeSpaning));
  318. }
  319. var plots = AllPlots;
  320. if (plots != null)
  321. {
  322. foreach (var plot in plots)
  323. {
  324. var comparison = plot as ComparisonPlot;
  325. if (comparison == null)
  326. {
  327. list.AddRange(plot.GetElements(owner, this, dateScale, RightScaler, aRect));
  328. }
  329. else
  330. {
  331. list.AddRange(comparison.GetElements(owner, this, dateScale, RightScaler, aRect));
  332. }
  333. }
  334. }
  335. flag = (_leftValueScaleDisplay != null);
  336. if (flag)
  337. {
  338. _leftValueScaleDisplay.BuildYList(aPane, aRect);
  339. }
  340. flag = (_rightValueScaleDisplay != null);
  341. if (flag)
  342. {
  343. _rightValueScaleDisplay.BuildYList(aPane, aRect);
  344. }
  345. flag = true;
  346. if (_symbol != null && owner.CurrentSymbol != null)
  347. {
  348. if (string.CompareOrdinal(_symbol, owner.CurrentSymbol) == 0)
  349. {
  350. flag = false;
  351. }
  352. }
  353. if (flag)
  354. {
  355. _drawingTools = null;
  356. _symbol = owner.CurrentSymbol;
  357. }
  358. flag = (_drawingTools == null);
  359. if (flag)
  360. {
  361. _drawingTools = _drawingToolContainer.DrawingForSymbol(owner.CurrentSymbol);
  362. flag = true;//(_chart == null || !ScaleLayer_1913.IgnoreSharedDrawings);
  363. if (flag)
  364. {
  365. _drawingTools.AddRange(_sharedDrawingToolContainer.DrawingForSymbol(owner.CurrentSymbol, _guid, HasPricePlot(), _chart));
  366. }
  367. }
  368. if (_drawingTools != null)
  369. {
  370. foreach (var drawingTool in _drawingTools)
  371. {
  372. if (drawingTool != null)
  373. {
  374. list.AddRange(drawingTool.GetPlots(owner, this, aRect, true));
  375. }
  376. }
  377. }
  378. if (_chartHighlights != null)
  379. {
  380. foreach (var current in _chartHighlights)
  381. {
  382. if (current != null)
  383. {
  384. list.Add(current.GetDrawing(dateScale, RightScaler, aRect));
  385. }
  386. }
  387. }
  388. return list;
  389. }
  390. internal void ClearDrawingToolContainer()
  391. {
  392. _drawingToolContainer.Clear();
  393. }
  394. internal List<FrameworkElement> GetAllPlotElements()
  395. {
  396. var list = new List<FrameworkElement>();
  397. if (_allFormula == null) return list;
  398. foreach (var formula in _allFormula)
  399. {
  400. if (!formula.IsComparison)
  401. list.AddRange(formula.AddLegendButton());
  402. }
  403. return list;
  404. }
  405. internal ChartPane GetChartPane()
  406. {
  407. return _chartPane;
  408. }
  409. internal string GetFirstPlotDescription()
  410. {
  411. bool flag = AllDisplayFormula.Count > 0;
  412. string result;
  413. if (flag)
  414. {
  415. result = AllDisplayFormula[0].Description;
  416. }
  417. else
  418. {
  419. result = string.Empty;
  420. }
  421. return result;
  422. }
  423. internal ValueScaleDisplay GetRightValueScaleDisplay()
  424. {
  425. return _rightValueScaleDisplay;
  426. }
  427. internal ValueScaleDisplay GetLeftValueScaleDisplay()
  428. {
  429. return _leftValueScaleDisplay;
  430. }
  431. /// <summary>
  432. /// 返回mScaler
  433. /// </summary>
  434. /// <returns></returns>
  435. internal IValueScaler GetRightValueScaler()
  436. {
  437. return RightScaler;
  438. }
  439. internal IValueScaler GetLeftValueScaler()
  440. {
  441. return LeftScaler;
  442. }
  443. internal bool HasPricePlot()
  444. {
  445. var displayFormulas = AllDisplayFormula;
  446. if (displayFormulas != null && displayFormulas.Any())
  447. {
  448. foreach (var formula in displayFormulas)
  449. {
  450. if (formula.FormulaModel != null && formula.FormulaModel.IsMain) return true;
  451. }
  452. }
  453. return false;
  454. }
  455. internal void RefreshChart(bool alsoInvalidate)
  456. {
  457. try
  458. {
  459. _drawingTools = null;
  460. if (_chart != null && alsoInvalidate)
  461. {
  462. _chart.Refresh(false);
  463. }
  464. }
  465. catch (Exception expr_4E)
  466. {
  467. ProjectData.SetProjectError(expr_4E);
  468. ProjectData.ClearProjectError();
  469. }
  470. }
  471. internal void RemoveAllPlots()
  472. {
  473. var list = new List<PlotBase>();
  474. list.AddRange(AllPlots);
  475. foreach (var current in list)
  476. {
  477. current.RemovedFromChart();
  478. }
  479. }
  480. internal void RemoveFromChartPane(PlotBase aPlot)
  481. {
  482. RemovePlot(aPlot);
  483. if (AllPlots.Count == 0)
  484. {
  485. _chartPane.RemoveScaleLayer(this);
  486. }
  487. }
  488. internal void RemovePlot(PlotBase aPlot)
  489. {
  490. AllPlots.Remove(aPlot);
  491. aPlot.RemovedFromChart();
  492. }
  493. internal bool ScaleGUIDNotEquals()
  494. {
  495. if (_drawingTools == null) return false;
  496. foreach (var current in _drawingTools)
  497. {
  498. var toolSpec = current.OwnerSpec as SharedDrawingToolSpec;
  499. if (toolSpec != null)
  500. {
  501. var drawingInfo = toolSpec.GetDrawingInfo();
  502. if (drawingInfo != null)
  503. {
  504. if (!drawingInfo.ScaleGUID.HasValue)
  505. {
  506. if (!ScaleGUID.Equals(drawingInfo.OriginalScaleGUID()))
  507. {
  508. return true;
  509. }
  510. }
  511. }
  512. }
  513. }
  514. return false;
  515. }
  516. internal void ScaleLayer_5514(List<IChartHighlight> items)
  517. {
  518. _chartHighlights = items;
  519. if ( _chart != null)
  520. {
  521. _chart.Refresh();
  522. }
  523. }
  524. /// <summary>
  525. /// GetmScaler
  526. /// </summary>
  527. /// <returns></returns>
  528. internal IValueScaler GetmScaler()
  529. {
  530. return this._leftValueScaler;
  531. }
  532. internal List<IDrawingTool> GetScaleLayer()
  533. {
  534. List<IDrawingTool> list = _drawingTools;
  535. if (list == null)
  536. {
  537. list = new List<IDrawingTool>();
  538. }
  539. return list;
  540. }
  541. internal void ScaleLayer_5516(string symbol, IDrawingTool aDrawing)
  542. {
  543. _drawingToolContainer.AddDrawing(symbol, aDrawing);
  544. }
  545. /// <summary>
  546. /// 更新所有plot的数据集
  547. /// </summary>
  548. /// <param name="dataset">The dataset.</param>
  549. /// <param name="refreshFlag">if set to <c>true</c> [refresh flag].</param>
  550. internal void UpdatePlotsDataSource(ILineDataSet dataset, bool refreshFlag)
  551. {
  552. //清除plots-性能问题
  553. AllPlots.Clear();
  554. if (_allFormula == null || !_allFormula.Any()) return;
  555. foreach (var formula in _allFormula)
  556. {
  557. if (formula.IsComparison) continue;
  558. formula.SetSource(dataset, refreshFlag);
  559. }
  560. foreach (var formula in ComparisonFormulas)
  561. {
  562. formula.CreatePlot();
  563. }
  564. }
  565. #endregion Internal Methods
  566. #region Private Methods
  567. private void OnAllDrawingsChanged()
  568. {
  569. RefreshChart(true);
  570. }
  571. private void OnDrawingChanged(DrawingToolInfo info)
  572. {
  573. OnDrawingsChangedForSym(info.Symbol);
  574. }
  575. private void OnDrawingsChangedForSym(string symbol)
  576. {
  577. if (string.IsNullOrWhiteSpace(symbol))
  578. {
  579. RefreshChart(true);
  580. }
  581. else
  582. {
  583. if (_chart != null && string.CompareOrdinal(_chart.CurrentSymbol, symbol) == 0)
  584. {
  585. RefreshChart(true);
  586. }
  587. }
  588. }
  589. /// <summary>
  590. /// mScaler更改后执行
  591. /// </summary>
  592. private void ScaleLayer_5544()
  593. {
  594. if (_chart != null)
  595. {
  596. _chart.Refresh(true);
  597. }
  598. }
  599. #endregion Private Methods
  600. #endregion Methods
  601. }
  602. }