DisplayFormula.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. using MuchInfo.Chart.Data.EnumTypes;
  2. using MuchInfo.Chart.Data.Interfaces;
  3. using MuchInfo.Chart.Data.Models;
  4. using MuchInfo.Chart.DataAdapter;
  5. using MuchInfo.Chart.Infrastructure.Data;
  6. using MuchInfo.Chart.Infrastructure.Helpers;
  7. using MuchInfo.Chart.Infrastructure.Utilities;
  8. using MuchInfo.Chart.WPF.Controls.Indicator;
  9. using MuchInfo.Chart.WPF.Controls.Utilities;
  10. using MuchInfo.Chart.WPF.Helpers;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. namespace MuchInfo.Chart.WPF.Primitives
  19. {
  20. public class DisplayFormula
  21. {
  22. private const string c_TimeSharing = "分时图";
  23. private const string c_TimeSharing_Price = "价格";
  24. private const string c_TimeSharing_AvgPrice = "均价";
  25. private const string c_TimeSharing_Volume = "成交量";
  26. private const string c_TimeSharing_Increase = "涨幅";
  27. private const string c_TimeSharing_IncreaseValue = "涨跌";
  28. private FormulaModel _formulaModel;
  29. private IDataService _dataService;//编辑器公式接口
  30. private ILineDataSet _lineSource;
  31. private const string c_defualtDownColor = "#FF55FFFF";
  32. private const string c_defualtUpColor = "#FFA43435";
  33. private Chart _chart;
  34. protected ScaleLayer mScale;
  35. private bool _isDark = true;
  36. private static Random _random = new Random(18000); // 没设置颜色时使用随机颜色,
  37. private Color[,] BrushArrary;
  38. /// <summary>
  39. /// 标题显示框
  40. /// </summary>
  41. private IndicatorLegend legendBtn;
  42. public Chart Chart
  43. {
  44. get { return _chart; }
  45. set { _chart = value; }
  46. }
  47. /// <summary>
  48. /// 计算公式
  49. /// </summary>
  50. public FormulaModel FormulaModel
  51. {
  52. get { return _formulaModel; }
  53. set { _formulaModel = value; }
  54. }
  55. /// <summary>
  56. /// 未计算的数据
  57. /// </summary>
  58. public ILineDataSet LineSource
  59. {
  60. get { return _lineSource; }
  61. set { _lineSource = value; }
  62. }
  63. /// <summary>
  64. /// 返回公式计算服务
  65. /// </summary>
  66. protected IDataService DataService
  67. {
  68. get { return this._dataService; }
  69. }
  70. private List<PlotBase> _plotBases;
  71. /// <summary>
  72. /// 公式包含的所有线
  73. /// </summary>
  74. public List<PlotBase> PlotBases
  75. {
  76. get
  77. {
  78. return _plotBases;
  79. }
  80. set { _plotBases = value; }
  81. }
  82. public string Description
  83. {
  84. get
  85. {
  86. if (this.FormulaModel == null) return string.Empty;
  87. return this.FormulaModel.FormulaName;
  88. }
  89. }
  90. /// <summary>
  91. /// 公式所显示的层
  92. /// </summary>
  93. public ScaleLayer ScaleLayer
  94. {
  95. get { return mScale; }
  96. set { mScale = value; }
  97. }
  98. /// <summary>
  99. /// 是否是叠加商品
  100. /// </summary>
  101. public bool IsComparison { get; set; }
  102. /// <summary>
  103. /// 是否显示分时图
  104. /// </summary>
  105. public bool IsTimeShare { get; set; }
  106. /// <summary>
  107. /// 叠加商品
  108. /// </summary>
  109. public GoodsInfo ComparionGoodsInfo { get; set; }
  110. #region 分时图开收市时间
  111. /// <summary>
  112. /// 开市时间
  113. /// </summary>
  114. public DateTime OpenTime { get; set; }
  115. /// <summary>
  116. /// 收市时间
  117. /// </summary>
  118. public DateTime CloseTime { get; set; }
  119. #endregion
  120. /// <summary>
  121. /// 标题显示框
  122. /// </summary>
  123. protected virtual IndicatorLegend mLegendBtn
  124. {
  125. get
  126. {
  127. return this.legendBtn;
  128. }
  129. set
  130. {
  131. var obj = new IndicatorLegend.ClickEventHandler(this.OnIndicatorLegendClick);
  132. bool flag = this.legendBtn != null;
  133. if (flag)
  134. {
  135. this.legendBtn.Click -= obj;
  136. }
  137. this.legendBtn = value;
  138. flag = (this.legendBtn != null);
  139. if (flag)
  140. {
  141. this.legendBtn.Click += obj;
  142. }
  143. }
  144. }
  145. protected DisplayFormula(FormulaModel formulaModel, Chart chart)
  146. {
  147. this.FormulaModel = formulaModel;
  148. this.mLegendBtn = new IndicatorLegend();
  149. _plotBases = new List<PlotBase>();
  150. this._chart = chart;
  151. _dataService = new DataService();
  152. }
  153. public DisplayFormula(FormulaModel formulaModel, ILineDataSet dataSet, Chart chart)
  154. : this(formulaModel, chart)
  155. {
  156. if (dataSet == null || dataSet.DataPoints == null)
  157. {
  158. // Calculation(new List<IBarDataPoint>()); //默认不进行计算显示公式有多少个图表
  159. Calculation(new List<IBarDataPoint>(), chart.CycleType, chart.ChartTimeSpan);
  160. }
  161. else
  162. {
  163. // SetSource(dataSet);
  164. }
  165. _lineSource = dataSet;
  166. // CreatePlot();
  167. }
  168. public DisplayFormula(FormulaModel formulaModel, ILineDataSet dataSet, Chart chart, GoodsInfo goodsInfo)
  169. : this(formulaModel, dataSet, chart)
  170. {
  171. this.IsComparison = true;
  172. ComparionGoodsInfo = goodsInfo;
  173. Calculation(this.LineSource.DataPoints.OfType<IBarDataPoint>().ToList(), chart.CycleType, chart.ChartTimeSpan);
  174. if (this.FormulaModel.IsMain)
  175. {
  176. foreach (var indicatorModel in FormulaModel.IndicatorList) //主图
  177. {
  178. indicatorModel.CalculateDataPoints = CalculatetionMainBarSet(indicatorModel).DataPoints;
  179. }
  180. }
  181. }
  182. /// <summary>
  183. /// 构建分时图
  184. /// </summary>
  185. /// <param name="dataSet">一天内的分钟线历史数据</param>
  186. /// <param name="chart">图表</param>
  187. /// <param name="goodsInfo">商品</param>
  188. public DisplayFormula(ILineDataSet dataSet, Chart chart, GoodsInfo goodsInfo)
  189. {
  190. this.FormulaModel = new FormulaModel(c_TimeSharing + goodsInfo.DisplayName);
  191. this.mLegendBtn = new IndicatorLegend();
  192. _plotBases = new List<PlotBase>();
  193. this._chart = chart;
  194. this.LineSource = dataSet;
  195. InitTimeShareInfo(this.FormulaModel, dataSet);
  196. }
  197. /// <summary>
  198. /// 初始化分时图数据
  199. /// </summary>
  200. /// <param name="formulaModel">公式,分时图中用于显示标题</param>
  201. private void InitTimeShareInfo(FormulaModel formulaModel, ILineDataSet dataSet)
  202. {
  203. if (dataSet==null)
  204. {
  205. return;
  206. }
  207. formulaModel.IndicatorList = new List<IndicatorModel>();
  208. //分时图中的价格图
  209. var indicator = new IndicatorModel(formulaModel)
  210. {
  211. LineName = c_TimeSharing_Price,
  212. LinePlotType = LinePlotType.Line,
  213. UpBrush = Colors.White,
  214. LineWidth = 1.2,
  215. CalculateDataPoints = new List<ILineDataPoint>(dataSet.DataPoints),
  216. };
  217. formulaModel.IndicatorList.Add(indicator);
  218. //分时图中的均线图
  219. var avgIndicator = new IndicatorModel(formulaModel)
  220. {
  221. LineName = c_TimeSharing_AvgPrice,
  222. LinePlotType = LinePlotType.Line,
  223. UpBrush = Colors.Yellow,
  224. LineWidth = 1.2,
  225. };
  226. avgIndicator.CalculateDataPoints = GetVolDataPoints(dataSet); //计算均值
  227. formulaModel.IndicatorList.Add(avgIndicator);
  228. //设置成交量的数据
  229. var volDataSet = new ChartDataSet();
  230. foreach (var dataPoint in dataSet.DataPoints)
  231. {
  232. var bar = dataPoint as IBarDataPoint;
  233. if (bar.Close >= bar.Open)
  234. {
  235. volDataSet.AddColorDataPoint(bar.Date, bar.Volume, Colors.Yellow, bar.Increase);
  236. }
  237. else
  238. {
  239. volDataSet.AddColorDataPoint(bar.Date, bar.Volume, Colors.Yellow, bar.Increase);
  240. }
  241. }
  242. //分时图中的成交量
  243. var volIndicator = new IndicatorModel(formulaModel)
  244. {
  245. LineName = c_TimeSharing_Volume,
  246. LinePlotType = LinePlotType.Bar,
  247. UpBrush = Colors.Yellow,
  248. VolBar = true,
  249. // DownBrush = new SolidColorBrush(Colors.LightGreen),
  250. LineWidth = 1.2,
  251. CalculateDataPoints = volDataSet.DataPoints,
  252. };
  253. formulaModel.IndicatorList.Add(volIndicator);
  254. }
  255. /// <summary>
  256. /// 更新分时图数据集
  257. /// </summary>
  258. /// <param name="dataSet">当前分时图分钟历史数据</param>
  259. public void UpdateTimeSharingData(ILineDataSet dataSet)
  260. {
  261. this.LineSource = dataSet;
  262. if (FormulaModel == null || FormulaModel.IndicatorList == null || !FormulaModel.IndicatorList.Any()) return;
  263. var priceIndcator = FormulaModel.IndicatorList.FirstOrDefault((indicator) => indicator.LineName == c_TimeSharing_Price);
  264. var avgIndcator = FormulaModel.IndicatorList.FirstOrDefault((indicator) => indicator.LineName == c_TimeSharing_AvgPrice);
  265. var volIndcator = FormulaModel.IndicatorList.FirstOrDefault((indicator) => indicator.LineName == c_TimeSharing_Volume);
  266. if (priceIndcator != null && avgIndcator != null && volIndcator != null)
  267. {
  268. priceIndcator.CalculateDataPoints = new List<ILineDataPoint>(dataSet.DataPoints);
  269. avgIndcator.CalculateDataPoints = GetVolDataPoints(dataSet); //计算均值
  270. //设置成交量的数据
  271. var volDataSet = new ChartDataSet();
  272. foreach (var dataPoint in dataSet.DataPoints)
  273. {
  274. var bar = dataPoint as IBarDataPoint;
  275. if (bar.Close >= bar.Open)
  276. {
  277. volDataSet.AddColorDataPoint(bar.Date, bar.Volume, Colors.Yellow, bar.Increase);
  278. }
  279. else
  280. {
  281. volDataSet.AddColorDataPoint(bar.Date, bar.Volume, Colors.Yellow, bar.Increase);
  282. }
  283. }
  284. volIndcator.CalculateDataPoints = volDataSet.DataPoints;
  285. }
  286. }
  287. /// <summary>
  288. /// 有实时行情时更新数据
  289. /// </summary>
  290. /// <param name="ibarDataPoint">行情数据点</param>
  291. public void UpdatePointSharing(IBarDataPoint ibarDataPoint)
  292. {
  293. if (this.FormulaModel == null) return;
  294. if (FormulaModel.IndicatorList == null || !FormulaModel.IndicatorList.Any()) return;
  295. var priceLine = FormulaModel.IndicatorList.FirstOrDefault((indicator) => indicator.LineName == c_TimeSharing_Price);
  296. var volBar = FormulaModel.IndicatorList.FirstOrDefault((indicator) => indicator.LineName == c_TimeSharing_Volume);
  297. var avgLine = FormulaModel.IndicatorList.FirstOrDefault((indicator) => indicator.LineName == c_TimeSharing_AvgPrice);
  298. if (priceLine != null && volBar != null && avgLine != null)
  299. {
  300. if (priceLine.CalculateDataPoints != null && priceLine.CalculateDataPoints.Any())
  301. {
  302. var volData = priceLine.CalculateDataPoints[priceLine.CalculateDataPoints.Count - 1];
  303. var time = ibarDataPoint.Date.AddSeconds(-ibarDataPoint.Date.Second);
  304. var timeSpan = time - volData.Date.AddSeconds(-volData.Date.Second);
  305. int count = (int)timeSpan.TotalMinutes;
  306. if (count == 0)
  307. {
  308. var curIndex = avgLine.CalculateDataPoints.Count - 1;
  309. priceLine.CalculateDataPoints[curIndex] = ibarDataPoint;
  310. var colorBar =
  311. volBar.CalculateDataPoints[curIndex] as OHLCDataPoint;
  312. if (colorBar != null)
  313. {
  314. colorBar.Value += ibarDataPoint.Volume;
  315. }
  316. var avgPoint = curIndex == 0
  317. ? ibarDataPoint
  318. : CaculationAvgPoint(avgLine.CalculateDataPoints[curIndex - 1], ibarDataPoint,
  319. curIndex + 1);
  320. avgLine.CalculateDataPoints[curIndex] = avgPoint;
  321. }
  322. else
  323. {
  324. if (count >= 2) //使用定时器时更新可能不会出现这情况
  325. {
  326. var repairPoint = priceLine.CalculateDataPoints[priceLine.CalculateDataPoints.Count - 1];
  327. var repairBarPoint = repairPoint as IBarDataPoint;
  328. if (repairBarPoint != null)
  329. {
  330. repairBarPoint.Volume = 0;
  331. }
  332. for (int i = 0; i < count - 1; i++)
  333. {
  334. priceLine.CalculateDataPoints.Add(repairBarPoint);
  335. volBar.CalculateDataPoints.Add(
  336. new ColoredDataPoint(ibarDataPoint.Date, 0, 0, 0, 0, Colors.Yellow, 0));
  337. var curIndex = avgLine.CalculateDataPoints.Count - 1;
  338. var avgPoint = curIndex == 0
  339. ? ibarDataPoint
  340. : CaculationAvgPoint(avgLine.CalculateDataPoints[curIndex - 1], ibarDataPoint,
  341. curIndex + 1);
  342. avgLine.CalculateDataPoints[curIndex] = avgPoint;
  343. }
  344. }
  345. priceLine.CalculateDataPoints.Add(ibarDataPoint);
  346. volBar.CalculateDataPoints.Add(
  347. new ColoredDataPoint(ibarDataPoint.Date, ibarDataPoint.Volume, ibarDataPoint.Volume, 0,
  348. ibarDataPoint.Volume, Colors.Yellow, 0));
  349. var avgIndex = avgLine.CalculateDataPoints.Count - 1;
  350. var avgCurPoint = CaculationAvgPoint(avgLine.CalculateDataPoints[avgIndex - 1], ibarDataPoint,
  351. avgIndex + 1);
  352. avgLine.CalculateDataPoints.Add(avgCurPoint);
  353. }
  354. }
  355. }
  356. }
  357. /// <summary>
  358. /// 更新Tik图表数据
  359. /// </summary>
  360. /// <param name="ibarDataPoint">数据点</param>
  361. public void UdpdateTickPoint(IBarDataPoint ibarDataPoint)
  362. {
  363. if (this.FormulaModel == null) return;
  364. if (FormulaModel.IndicatorList == null || !FormulaModel.IndicatorList.Any()) return;
  365. var dataPoints = FormulaModel.IndicatorList[0];
  366. dataPoints.CalculateDataPoints.Add(ibarDataPoint);
  367. }
  368. /// <summary>
  369. /// 更新叠加分时图商品
  370. /// </summary>
  371. /// <param name="ibarDataPoint">行情数据</param>
  372. public void UpdateComparsionPoint(IBarDataPoint ibarDataPoint)
  373. {
  374. if (this.FormulaModel == null) return;
  375. if (FormulaModel.IndicatorList == null || !FormulaModel.IndicatorList.Any()) return;
  376. var priceLine = FormulaModel.IndicatorList[0];
  377. if (priceLine.CalculateDataPoints != null && priceLine.CalculateDataPoints.Any())
  378. {
  379. var volData = priceLine.CalculateDataPoints[priceLine.CalculateDataPoints.Count - 1];
  380. var time = ibarDataPoint.Date.AddSeconds(-ibarDataPoint.Date.Second);
  381. var timeSpan = time - volData.Date.AddSeconds(-volData.Date.Second);
  382. int count = (int)timeSpan.TotalMinutes;
  383. if (count == 0)
  384. {
  385. var curIndex = priceLine.CalculateDataPoints.Count - 1;
  386. priceLine.CalculateDataPoints[curIndex] = ibarDataPoint;
  387. }
  388. else
  389. {
  390. if (count >= 2) //使用定时器时更新可能不会出现这情况
  391. {
  392. var repairPoint = priceLine.CalculateDataPoints[priceLine.CalculateDataPoints.Count - 1];
  393. var repairBarPoint = repairPoint as IBarDataPoint;
  394. if (repairBarPoint != null)
  395. {
  396. repairBarPoint.Volume = 0;
  397. }
  398. for (int i = 0; i < count - 1; i++)
  399. {
  400. priceLine.CalculateDataPoints.Add(repairBarPoint);
  401. }
  402. }
  403. priceLine.CalculateDataPoints.Add(ibarDataPoint);
  404. }
  405. }
  406. }
  407. /// <summary>
  408. /// 计算平均值
  409. /// </summary>
  410. /// <param name="frontPoint">前一个平均值</param>
  411. /// <param name="currentPoint">当前行情值</param>
  412. /// <param name="index">数据索引</param>
  413. /// <returns></returns>
  414. private ILineDataPoint CaculationAvgPoint(ILineDataPoint frontPoint, IBarDataPoint currentPoint, int index)
  415. {
  416. var value = ((index - 1) * frontPoint.Value + currentPoint.Close) / index;
  417. return new BarDataPoint(currentPoint.Date, value);
  418. }
  419. /// <summary>
  420. ///计算均值
  421. /// </summary>
  422. /// <param name="dataSet">数据源</param>
  423. /// <returns>均值数据</returns>
  424. private List<ILineDataPoint> GetVolDataPoints(ILineDataSet dataSet)
  425. {
  426. var avgDataSet = new List<ILineDataPoint>();
  427. if (dataSet == null || dataSet.DataPoints == null || !dataSet.DataPoints.Any()) return avgDataSet;
  428. var ohlcPoint = dataSet.DataPoints[0] as IOHLCDataPoint;
  429. if (ohlcPoint == null) return avgDataSet;
  430. avgDataSet.Add(new BarDataPoint(ohlcPoint.Date, ohlcPoint.Close));
  431. for (int i = 1; i < dataSet.DataPoints.Count; i++)
  432. {
  433. var date = dataSet.DataPoints[i].Date;
  434. ohlcPoint = dataSet.DataPoints[i] as IOHLCDataPoint;
  435. if (ohlcPoint == null)
  436. {
  437. avgDataSet.Add(new BarDataPoint(date, avgDataSet[i - 1].Value));
  438. }
  439. else
  440. {
  441. var value = (i * avgDataSet[i - 1].Value + ohlcPoint.Close) / (i + 1);
  442. avgDataSet.Add(new BarDataPoint(date, value));
  443. }
  444. }
  445. return avgDataSet;
  446. }
  447. ///// <summary>
  448. ///// 调用公式计算数据
  449. ///// </summary>
  450. ///// <param name="dataPoint">数据源</param>
  451. //public void Calculation(List<IBarDataPoint> dataPoint)
  452. //{
  453. // if (this.FormulaModel == null) return;
  454. // //计算指标数据
  455. // _dataService.Calculate(dataPoint, this.FormulaModel, this.Chart.CycleType, this.Chart.ChartTimeSpan);
  456. //}
  457. /// <summary>
  458. /// 调用公式计算数据
  459. /// </summary>
  460. /// <param name="dataPoint">数据源</param>
  461. public void Calculation(List<IBarDataPoint> dataPoint, CycleType cycle, TimeSpan timeSpan)
  462. {
  463. if (this.FormulaModel == null) return;
  464. if (_dataService == null) return;
  465. //计算指标数据
  466. _dataService.Calculate(dataPoint, this.FormulaModel, cycle, timeSpan);
  467. if (this.FormulaModel.IndicatorList != null && this.FormulaModel.IndicatorList.Any())
  468. {
  469. if (BrushArrary == null)
  470. {
  471. BrushArrary = new Color[FormulaModel.IndicatorList.Count, 3];
  472. }
  473. int i = 0;
  474. foreach (var indicatorModel in this.FormulaModel.IndicatorList) //指标没有配置颜色的,设置随机颜色
  475. {
  476. UpBrush(indicatorModel, i);
  477. DownBrush(indicatorModel, i);
  478. FillAreaBrush(indicatorModel, i);
  479. i++;
  480. }
  481. }
  482. }
  483. /// <summary>
  484. /// 调用公式计算指标
  485. /// </summary>
  486. public void Calculation()
  487. {
  488. if (_lineSource != null)
  489. {
  490. this.Calculation(this._lineSource.DataPoints.OfType<IBarDataPoint>().ToList(), this.Chart.CycleType, this.Chart.ChartTimeSpan);
  491. }
  492. }
  493. #region 设置图表颜色
  494. /// <summary>
  495. /// 获取上升颜色
  496. /// </summary>
  497. /// <param name="indicator"></param>
  498. /// <returns></returns>
  499. protected virtual Color UpBrush(IndicatorModel indicator, int i)
  500. {
  501. if (!ColorHelper.IsEmptyColor(indicator.UpBrush)) return indicator.UpBrush;
  502. if ((indicator.ParentFormula.IsMain || indicator.ParentFormula.IsVolume) && this._isDark)
  503. {
  504. return (Color)ColorConverter.ConvertFromString(c_defualtUpColor);
  505. }
  506. else if (indicator.ParentFormula.IsMain || indicator.ParentFormula.IsVolume)
  507. {
  508. return Colors.DarkRed;
  509. }
  510. else if (indicator.VolBar && ColorHelper.IsEmptyColor(indicator.UpBrush))
  511. {
  512. indicator.UpBrush = this._isDark ? (Color)ColorConverter.ConvertFromString(c_defualtUpColor) : Colors.DarkRed;
  513. }
  514. else if (indicator.PlotNegValsDiffColor && ColorHelper.IsEmptyColor(indicator.UpBrush))
  515. {
  516. indicator.UpBrush = this._isDark ? (Color)ColorConverter.ConvertFromString(c_defualtUpColor) : Colors.DarkRed;
  517. }
  518. else
  519. {
  520. if (BrushArrary[i, 0] != default(Color))
  521. {
  522. indicator.UpBrush = BrushArrary[i, 0];
  523. }
  524. else
  525. {
  526. int count = ColorHelper.AllColors().Count;
  527. int index = _random.Next(0, count - 1);
  528. indicator.UpBrush = ColorHelper.AllColors()[index].aColor;
  529. BrushArrary[i, 0] = ColorHelper.AllColors()[index].aColor;
  530. }
  531. }
  532. return indicator.UpBrush;
  533. }
  534. /// <summary>
  535. ///获取下降颜色
  536. /// </summary>
  537. /// <param name="indicator"></param>
  538. /// <returns></returns>
  539. private Color DownBrush(IndicatorModel indicator, int i)
  540. {
  541. if (!ColorHelper.IsEmptyColor(indicator.DownBrush)) return indicator.DownBrush;
  542. if ((indicator.ParentFormula.IsMain || indicator.ParentFormula.IsVolume) && this._isDark)
  543. {
  544. indicator.DownBrush = (Color)ColorConverter.ConvertFromString(c_defualtDownColor);
  545. }
  546. else if (indicator.ParentFormula.IsMain || indicator.ParentFormula.IsVolume)
  547. {
  548. indicator.DownBrush = Colors.DarkGreen;
  549. }
  550. else if (indicator.VolBar && ColorHelper.IsEmptyColor(indicator.DownBrush))
  551. {
  552. indicator.DownBrush = this._isDark ? (Color)ColorConverter.ConvertFromString(c_defualtDownColor) : Colors.DarkGreen;
  553. }
  554. else if (indicator.PlotNegValsDiffColor && ColorHelper.IsEmptyColor(indicator.DownBrush))
  555. {
  556. indicator.DownBrush = this._isDark ? (Color)ColorConverter.ConvertFromString(c_defualtDownColor) : Colors.DarkGreen;
  557. }
  558. else
  559. {
  560. if (BrushArrary[i, 1] != default(Color))
  561. {
  562. indicator.DownBrush = BrushArrary[i, 1];
  563. }
  564. else
  565. {
  566. int count = ColorHelper.AllColors().Count;
  567. int index = _random.Next(0, count - 1);
  568. indicator.DownBrush = ColorHelper.AllColors()[index].aColor;
  569. BrushArrary[i, 1] = ColorHelper.AllColors()[index].aColor;
  570. }
  571. }
  572. return indicator.DownBrush;
  573. }
  574. /// <summary>
  575. /// 区域填充颜色
  576. /// </summary>
  577. /// <param name="indicator"></param>
  578. /// <returns></returns>
  579. private Color FillAreaBrush(IndicatorModel indicator, int i)
  580. {
  581. if (!ColorHelper.IsEmptyColor(indicator.AreaBrush))
  582. {
  583. return indicator.AreaBrush;
  584. }
  585. if (BrushArrary[i, 2] != default(Color))
  586. {
  587. indicator.AreaBrush = BrushArrary[i, 2];
  588. }
  589. else
  590. {
  591. int count = ColorHelper.AllColors().Count;
  592. int index = _random.Next(0, count - 1);
  593. indicator.AreaBrush = ColorHelper.AllColors()[index].aColor;
  594. BrushArrary[i, 2] = ColorHelper.AllColors()[index].aColor;
  595. }
  596. return indicator.AreaBrush;
  597. }
  598. #endregion
  599. private void OnIndicatorLegendClick(MouseEventArgs a0)
  600. {
  601. // this.AddRemoveIndicators((MouseButtonEventArgs)a0);
  602. if (this.Chart != null && this.Chart.CycleType != CycleType.TimeSharing)
  603. {
  604. // AddRemoveIndicators(a0); //使用从指标菜单添加指标
  605. }
  606. }
  607. /// <summary>
  608. /// 标题控件
  609. /// </summary>
  610. /// <returns></returns>
  611. public FrameworkElement LegendBtn()
  612. {
  613. return this.mLegendBtn;
  614. }
  615. /// <summary>
  616. /// 切换指标
  617. /// </summary>
  618. internal void AddIndicators()
  619. {
  620. var Formulas = ScaleLayer.AllDisplayFormula;
  621. var formulaList = new List<string>();
  622. foreach (var formula in Formulas)
  623. {
  624. formulaList.Add(formula.FormulaModel.FormulaName);
  625. }
  626. this.DataService.OpenEditor(this._lineSource.DataPoints.OfType<IBarDataPoint>().ToList(), formulaList.ToArray(),
  627. this.Chart.CycleType, this.Chart.ChartTimeSpan);
  628. }
  629. /// <summary>
  630. /// 第一种图表颜色,显示到标题中
  631. /// </summary>
  632. public Color FristPlotBrush()
  633. {
  634. if (this.FormulaModel != null && this.FormulaModel.IndicatorList != null
  635. && this.FormulaModel.IndicatorList.Any())
  636. {
  637. var solidBrush = this.FormulaModel.IndicatorList[0].UpBrush;
  638. if (solidBrush == null) return Colors.White;
  639. return solidBrush;
  640. }
  641. return Colors.White;
  642. }
  643. internal virtual List<FrameworkElement> AddLegendButton()
  644. {
  645. var list = new List<FrameworkElement>();
  646. this.mLegendBtn.IndicatorLegend_2547(this, this.Chart);
  647. list.Add(this.mLegendBtn);
  648. return list;
  649. }
  650. /// <summary>
  651. /// 暂时显示主图
  652. /// </summary>
  653. /// <returns></returns>
  654. protected ILineDataSet CalculatetionMainBarSet(IndicatorModel indicator)
  655. {
  656. var result = new ChartDataSet();
  657. var upColor = !ColorHelper.IsEmptyColor(indicator.UpBrush)
  658. ? indicator.UpBrush
  659. : Colors.Red;
  660. if (ColorHelper.IsEmptyColor(indicator.UpBrush))
  661. {
  662. indicator.UpBrush = upColor;
  663. }
  664. var downColor = !ColorHelper.IsEmptyColor(indicator.DownBrush)
  665. ? indicator.DownBrush
  666. : Colors.LawnGreen;
  667. if (ColorHelper.IsEmptyColor(indicator.DownBrush))
  668. {
  669. indicator.DownBrush = downColor;
  670. }
  671. if (LineSource == null || !LineSource.DataPoints.Any()) return result;
  672. foreach (var dataPoint in LineSource.DataPoints)
  673. {
  674. var bar = dataPoint as IBarDataPoint;
  675. if (bar.Close >= bar.Open)
  676. {
  677. result.AddColoredBarDataPoint(bar.Date, bar.Open, bar.High, bar.Low, bar.Close, bar.Volume, upColor,
  678. bar.Turnover, bar.HoldVolume, bar.Increase);
  679. }
  680. else
  681. {
  682. result.AddColoredBarDataPoint(bar.Date, bar.Open, bar.High, bar.Low, bar.Close, bar.Volume,
  683. downColor, bar.Turnover, bar.HoldVolume, bar.Increase);
  684. }
  685. }
  686. return result;
  687. }
  688. /// <summary>
  689. /// 暂时显示成交量
  690. /// </summary>
  691. /// <returns></returns>
  692. protected ILineDataSet CalculatetionVolumeBarSet(IndicatorModel indicator)
  693. {
  694. var result = new ChartDataSet();
  695. var upColor = !ColorHelper.IsEmptyColor(indicator.UpBrush)
  696. ? indicator.UpBrush
  697. : Colors.Red;
  698. if (ColorHelper.IsEmptyColor(indicator.UpBrush))
  699. {
  700. indicator.UpBrush = upColor;
  701. }
  702. var downColor = !ColorHelper.IsEmptyColor(indicator.DownBrush)
  703. ? indicator.DownBrush
  704. : Colors.LawnGreen;
  705. if (ColorHelper.IsEmptyColor(indicator.DownBrush))
  706. {
  707. indicator.DownBrush = downColor;
  708. }
  709. if (this.LineSource == null || LineSource.DataPoints == null || !LineSource.DataPoints.Any()) return result;
  710. foreach (var dataPoint in LineSource.DataPoints)
  711. {
  712. var bar = dataPoint as IBarDataPoint;
  713. if (bar.Close >= bar.Open)
  714. {
  715. result.AddColorDataPoint(bar.Date, bar.Volume, upColor, bar.Increase);
  716. }
  717. else
  718. {
  719. result.AddColorDataPoint(bar.Date, bar.Volume, downColor, bar.Increase);
  720. }
  721. }
  722. return result;
  723. }
  724. /// <summary>
  725. /// 检测画图面板中的图例,并返回它们在aDate日期下的数据面板
  726. /// </summary>
  727. /// <param name="aDate">日期</param>
  728. /// <param name="isDarkBackground"></param>
  729. /// <param name="isForex"></param>
  730. /// <returns></returns>
  731. public List<ControlPair> GetControlPairs(DateTime aDate, bool isDarkBackground, bool isForex)
  732. {
  733. var controlPairs = new List<ControlPair>();
  734. if (!this.IsComparison)
  735. {
  736. var textBlock3 = new TextBlock
  737. {
  738. Text = this._formulaModel.FormulaName,
  739. Foreground = new SolidColorBrush(FristPlotBrush()),
  740. Margin = new Thickness(0.0, 3.0, 0.0, 0.0)
  741. };
  742. controlPairs.Add(new ControlPair
  743. {
  744. ControlLeft = textBlock3
  745. });
  746. }
  747. foreach (var plot in PlotBases)
  748. {
  749. controlPairs.AddRange(plot.GetControlPairs(aDate, isDarkBackground, isForex));
  750. }
  751. return controlPairs;
  752. }
  753. /// <summary>
  754. /// Gets the control pairs.
  755. /// </summary>
  756. /// <param name="aDate">A date.</param>
  757. /// <param name="isDarkBackground">if set to <c>true</c> [is dark background].</param>
  758. /// <param name="digits">The digits.</param>
  759. /// <returns>List{ControlPair}.</returns>
  760. public List<ControlPair> GetControlPairs(DateTime aDate, bool isDarkBackground, int digits)
  761. {
  762. var controlPairs = new List<ControlPair>();
  763. if (!this.IsComparison)
  764. {
  765. var textBlock3 = new TextBlock
  766. {
  767. Text = this._formulaModel.FormulaName,
  768. Foreground = new SolidColorBrush(FristPlotBrush()),
  769. Margin = new Thickness(0.0, 3.0, 0.0, 0.0)
  770. };
  771. controlPairs.Add(new ControlPair
  772. {
  773. ControlLeft = textBlock3
  774. });
  775. }
  776. foreach (var plot in PlotBases)
  777. {
  778. if ((this.IsTimeShare && !plot.Indicator.VolBar) || this.FormulaModel.IsMain)
  779. {
  780. controlPairs.AddRange(plot.GetControlPairs(aDate, isDarkBackground, digits));
  781. }
  782. else
  783. {
  784. controlPairs.AddRange(plot.GetControlPairs(aDate, isDarkBackground, 2));
  785. }
  786. }
  787. return controlPairs;
  788. }
  789. ///// <summary>
  790. ///// 设置新数据
  791. ///// </summary>
  792. ///// <param name="aSource"></param>
  793. //public void SetSource(ILineDataSet aSource)
  794. //{
  795. // bool IsUpdate = aSource != null && aSource.Equals(this.LineSource);
  796. // this.LineSource = aSource;
  797. // if (aSource != null && aSource.DataPoints != null)
  798. // {
  799. // if (!IsUpdate)
  800. // {
  801. // Calculation(this.LineSource.DataPoints.OfType<IBarDataPoint>().ToList()); //计算
  802. // if (this.FormulaModel.IsMain)
  803. // {
  804. // foreach (var indicatorModel in FormulaModel.IndicatorList) //主图
  805. // {
  806. // indicatorModel.CalculateDataPoints = CalculatetionMainBarSet(indicatorModel).DataPoints;
  807. // }
  808. // }
  809. // else if (this.FormulaModel.IsVolume) //成交量
  810. // {
  811. // foreach (var indicatorModel in FormulaModel.IndicatorList)
  812. // {
  813. // indicatorModel.CalculateDataPoints = CalculatetionVolumeBarSet(indicatorModel).DataPoints;
  814. // indicatorModel.LinePlotType = LinePlotType.Bar;
  815. // }
  816. // }
  817. // }
  818. // if (this.ScaleLayer == null) return;
  819. // CreatePlot();
  820. // }
  821. //}
  822. /// <summary>
  823. /// 设置新数据
  824. /// </summary>
  825. /// <param name="aSource">A source.</param>
  826. /// <param name="refreshFlag">if set to <c>true</c> [refresh flag].</param>
  827. public void SetSource(ILineDataSet aSource, bool refreshFlag = false)
  828. {
  829. // if (FormulaModel.IndicatorList == null || !FormulaModel.IndicatorList.Any()) return;
  830. this.LineSource = aSource;
  831. if (aSource != null && aSource.DataPoints != null)
  832. {
  833. //if (!IsUpdate)
  834. //{
  835. var cycleType = this.Chart.CycleType;
  836. var timeSpan = Chart.ChartTimeSpan;
  837. //Task.Factory.StartNew(new Action(() =>
  838. //{
  839. Calculation(this.LineSource.DataPoints.OfType<IBarDataPoint>().ToList(), cycleType, timeSpan); //计算
  840. if (this.FormulaModel.IsMain)
  841. {
  842. foreach (var indicatorModel in FormulaModel.IndicatorList) //主图
  843. {
  844. indicatorModel.CalculateDataPoints = CalculatetionMainBarSet(indicatorModel).DataPoints;
  845. }
  846. }
  847. else if (this.FormulaModel.IsVolume) //成交量
  848. {
  849. foreach (var indicatorModel in FormulaModel.IndicatorList)
  850. {
  851. indicatorModel.CalculateDataPoints = CalculatetionVolumeBarSet(indicatorModel).DataPoints;
  852. indicatorModel.LinePlotType = LinePlotType.Bar;
  853. }
  854. }
  855. if (this.ScaleLayer == null) return;
  856. CreatePlot();
  857. //}));
  858. }
  859. }
  860. /// <summary>
  861. /// 设置成交量与主图数据。不经编辑器中计算
  862. /// </summary>
  863. private void SetMainAndVolumeDataSet(FormulaModel formulaModel)
  864. {
  865. if (formulaModel.IsMain)
  866. {
  867. foreach (var indicatorModel in formulaModel.IndicatorList) //主图
  868. {
  869. indicatorModel.CalculateDataPoints = CalculatetionMainBarSet(indicatorModel).DataPoints;
  870. }
  871. }
  872. else if (formulaModel.IsVolume) //成交量
  873. {
  874. foreach (var indicatorModel in formulaModel.IndicatorList)
  875. {
  876. indicatorModel.CalculateDataPoints = CalculatetionVolumeBarSet(indicatorModel).DataPoints;
  877. indicatorModel.LinePlotType = LinePlotType.Bar;
  878. }
  879. }
  880. }
  881. /// <summary>
  882. /// Gets the size of the chart font.
  883. /// </summary>
  884. /// <returns>System.Double.</returns>
  885. public double GetChartFontSize()
  886. {
  887. return (this.Chart == null) ? 12f : this.Chart.ChartFontSize;
  888. }
  889. private void AddRemoveIndicators(MouseEventArgs e)
  890. {
  891. var popUpMenu = new PopupMenu(this.Chart.RootPanel) { FontSize = this.GetChartFontSize() };
  892. popUpMenu.AddItem(LanguageManager.FindResource(LanguageConst.CalculationMenu_Edit),
  893. ImageHelper.GetImage("PropertiesHS.png"), new EventHandler(this.ShowEditFormula));
  894. popUpMenu.AddItem(LanguageManager.FindResource(LanguageConst.CalculationMenu_Add),
  895. ImageHelper.GetImage("add2.png"), new EventHandler(this.SelectFormula));
  896. popUpMenu.Show(e, new Point(-5.0, -5.0));
  897. e.Handled = true;
  898. }
  899. //编辑指标
  900. private void ShowEditFormula(object sender, EventArgs e)
  901. {
  902. ClosePopupMenu(sender);
  903. if (this.FormulaModel != null)
  904. {
  905. _dataService.EditFormula(this.FormulaModel.FormulaName);
  906. }
  907. }
  908. /// <summary>
  909. /// 关闭菜单面板
  910. /// </summary>
  911. /// <param name="sender">菜单事件源</param>
  912. private void ClosePopupMenu(Object sender)
  913. {
  914. var popupmenu = GetParent<PopupMenu>(sender as DependencyObject) as PopupMenu;
  915. if (popupmenu != null)
  916. {
  917. popupmenu.ClosePopup();
  918. }
  919. }
  920. /// <summary>
  921. /// 查询指定类型的父结点
  922. /// </summary>
  923. /// <typeparam name="T"></typeparam>
  924. /// <param name="dependency"></param>
  925. /// <returns></returns>
  926. private DependencyObject GetParent<T>(DependencyObject dependency)
  927. {
  928. do
  929. {
  930. if (dependency == null) return null;
  931. if (dependency.GetType() == typeof(T)) return dependency;
  932. dependency = VisualTreeHelper.GetParent(dependency);
  933. } while (true);
  934. }
  935. /// <summary>
  936. /// 选择指标
  937. /// </summary>
  938. /// <param name="sender"></param>
  939. /// <param name="e"></param>
  940. private void SelectFormula(object sender, EventArgs e)
  941. {
  942. ClosePopupMenu(sender);
  943. var chartPanel = ScaleLayer.GetChartPane();
  944. if (chartPanel == null) return;
  945. var formulaList = new List<DisplayFormula>();
  946. foreach (var scaleLayer in chartPanel.AllScaleLayers)
  947. {
  948. formulaList.AddRange(scaleLayer.AllDisplayFormula);
  949. }
  950. //不能编辑主图
  951. var mainFormula = formulaList.FirstOrDefault((displayFormula) => displayFormula.FormulaModel != null && displayFormula.FormulaModel.IsMain);
  952. formulaList.Remove(mainFormula);
  953. string[] formulas = formulaList.Select(
  954. (formula) => formula.FormulaModel != null ? formula.FormulaModel.FormulaName : "").ToArray();
  955. if (this.LineSource != null && this.LineSource.DataPoints != null && this.LineSource.DataPoints.Any())
  956. {
  957. var formulaModels = _dataService.OpenEditor(
  958. this.LineSource.DataPoints.OfType<IBarDataPoint>().ToList(), formulas,
  959. this.Chart.CycleType, this.Chart.ChartTimeSpan);
  960. if (formulaModels == null) return;
  961. this.ScaleLayer.ClearFormulas();
  962. this.ScaleLayer.GetChartPane().RemoveAllScaleLayers();
  963. var formulaDatas = new List<FormulaModel>(formulaModels.Where((formula) => formula.IsMain));
  964. //不能选择主图
  965. foreach (var formulaData in formulaDatas)
  966. {
  967. formulaModels.Remove(formulaData);
  968. }
  969. foreach (var formulaModel in formulaModels)
  970. {
  971. SetMainAndVolumeDataSet(formulaModel); //设置主图与成交量数据
  972. Chart.AddFormula(new DisplayFormula(formulaModel,
  973. Chart) { LineSource = this.LineSource, }, false,
  974. this.ScaleLayer.GetChartPane());
  975. }
  976. Chart.Refresh(true);
  977. }
  978. }
  979. /// <summary>
  980. /// 创建公式中的图形
  981. /// </summary>
  982. public void CreatePlot()
  983. {
  984. if (FormulaModel.IndicatorList == null) return;
  985. var plots = new List<PlotBase>();
  986. PlotBases.Clear();
  987. if (!IsComparison)
  988. {
  989. foreach (var indicator in FormulaModel.IndicatorList)
  990. {
  991. var linePlot = new LinePlot(indicator, this.Chart);
  992. // linePlot.InitAsNew();
  993. this.ScaleLayer.AllPlots.Add(linePlot);
  994. linePlot.ScaleLayer = this.ScaleLayer;
  995. plots.Add(linePlot);
  996. }
  997. PlotBases.AddRange(plots); //添加到公式中
  998. }
  999. else
  1000. {
  1001. foreach (var indicator in FormulaModel.IndicatorList)
  1002. {
  1003. var comparisonPlot = new ComparisonPlot(indicator, this.Chart, this.ComparionGoodsInfo);
  1004. this.ScaleLayer.AllPlots.Add(comparisonPlot);
  1005. comparisonPlot.ScaleLayer = this.ScaleLayer;
  1006. comparisonPlot.IsActive = true;
  1007. plots.Add(comparisonPlot);
  1008. }
  1009. PlotBases.AddRange(plots); //添加到公式中
  1010. }
  1011. }
  1012. /// <summary>
  1013. ///
  1014. /// </summary>
  1015. /// <returns></returns>
  1016. public static List<DisplayFormula> CreateDisplayFormula(ILineDataSet dataSet, Chart chart)
  1017. {
  1018. var dataService = new DataService();
  1019. var formulaModels = dataService.OpenEditor(
  1020. dataSet.DataPoints.OfType<IBarDataPoint>().ToList(), new string[] { },
  1021. chart.CycleType, chart.ChartTimeSpan);
  1022. var formulaDatas = new List<FormulaModel>(formulaModels.Where((formula) => formula.IsMain));
  1023. //不能选择主图
  1024. foreach (var formulaData in formulaDatas)
  1025. {
  1026. formulaModels.Remove(formulaData);
  1027. }
  1028. var list = new List<DisplayFormula>();
  1029. foreach (var formulaModel in formulaModels)
  1030. {
  1031. if (formulaModel.IsMain) continue; //不添加主图
  1032. if (formulaModel.IsVolume) //成交量数据
  1033. {
  1034. foreach (var indicator in formulaModel.IndicatorList)
  1035. {
  1036. var result = new ChartDataSet();
  1037. var upColor = !ColorHelper.IsEmptyColor(indicator.UpBrush)
  1038. ? indicator.UpBrush
  1039. : Colors.Red;
  1040. if (ColorHelper.IsEmptyColor(indicator.UpBrush))
  1041. {
  1042. indicator.UpBrush = upColor;
  1043. }
  1044. var downColor = !ColorHelper.IsEmptyColor(indicator.DownBrush)
  1045. ? indicator.DownBrush
  1046. : Colors.LawnGreen;
  1047. if (ColorHelper.IsEmptyColor(indicator.DownBrush))
  1048. {
  1049. indicator.DownBrush = downColor;
  1050. }
  1051. if (dataSet.DataPoints == null || !dataSet.DataPoints.Any()) break;
  1052. foreach (var dataPoint in dataSet.DataPoints)
  1053. {
  1054. var bar = dataPoint as IBarDataPoint;
  1055. if (bar.Close >= bar.Open)
  1056. {
  1057. result.AddColorDataPoint(bar.Date, bar.Volume, upColor, bar.Increase);
  1058. }
  1059. else
  1060. {
  1061. result.AddColorDataPoint(bar.Date, bar.Volume, downColor, bar.Increase);
  1062. }
  1063. }
  1064. indicator.CalculateDataPoints = result.DataPoints;
  1065. }
  1066. }
  1067. list.Add(new DisplayFormula(formulaModel, dataSet, chart));
  1068. }
  1069. return list;
  1070. }
  1071. /// <summary>
  1072. /// 添加叠加分时图公式
  1073. /// </summary>
  1074. /// <param name="comparisonGoodses">叠加的商品</param>
  1075. /// <param name="chart"></param>
  1076. /// <param name="chartDataSets">叠加商品数据</param>
  1077. /// <returns></returns>
  1078. public static List<DisplayFormula> TimeSharingFormulas(List<ComparisonGoods> comparisonGoodses, Chart chart, Dictionary<string, List<ILineDataPoint>> chartDataSets)
  1079. {
  1080. var comparisonFormulas = new List<DisplayFormula>();
  1081. foreach (var goodsInfo in comparisonGoodses)
  1082. {
  1083. var dataList = chartDataSets.ContainsKey(goodsInfo.GoodsCode)
  1084. ? chartDataSets[goodsInfo.GoodsCode]
  1085. : new List<ILineDataPoint>();
  1086. var displayformula = TimeSharingLineFormula(chart, goodsInfo, dataList);
  1087. comparisonFormulas.Add(displayformula);
  1088. }
  1089. return comparisonFormulas;
  1090. }
  1091. /// <summary>
  1092. /// 创建分时图图表。
  1093. /// </summary>
  1094. /// <param name="chart">图表</param>
  1095. /// <param name="goodsInfo">叠加商品</param>
  1096. /// <param name="dataSetPoint">数据集</param>
  1097. /// <returns></returns>
  1098. public static DisplayFormula TimeSharingLineFormula(Chart chart, GoodsInfo goodsInfo, List<ILineDataPoint> dataSetPoint)
  1099. {
  1100. var formulaModel = new FormulaModel("Main" + goodsInfo.DisplayName);
  1101. var displayformula = new DisplayFormula(formulaModel, chart);
  1102. displayformula.IsComparison = true;
  1103. displayformula.ComparionGoodsInfo = goodsInfo;
  1104. formulaModel.IndicatorList = new List<IndicatorModel>();
  1105. //分时图中的价格图
  1106. var indicator = new IndicatorModel(formulaModel)
  1107. {
  1108. LineName = c_TimeSharing_Price,
  1109. LinePlotType = LinePlotType.Line,
  1110. //UpBrush = Colors.White,
  1111. LineWidth = 1.2,
  1112. };
  1113. if (dataSetPoint != null)
  1114. {
  1115. indicator.CalculateDataPoints = dataSetPoint;
  1116. }
  1117. else
  1118. {
  1119. indicator.CalculateDataPoints = new List<ILineDataPoint>();
  1120. }
  1121. formulaModel.IndicatorList.Add(indicator);
  1122. return displayformula;
  1123. }
  1124. /// <summary>
  1125. /// 创建限时图公式
  1126. /// </summary>
  1127. /// <param name="chart">图表控件</param>
  1128. /// <param name="goodsInfo">商品信息</param>
  1129. /// <param name="dataSetPoint">数据点</param>
  1130. /// <returns>限时图公式</returns>
  1131. public static DisplayFormula CreateTikChartFormula(Chart chart, GoodsInfo goodsInfo, List<ILineDataPoint> dataSetPoint)
  1132. {
  1133. var formulaModel = new FormulaModel("即时图" + goodsInfo.DisplayName);
  1134. var displayformula = new DisplayFormula(formulaModel, chart);
  1135. formulaModel.IndicatorList = new List<IndicatorModel>();
  1136. var indicator = new IndicatorModel(formulaModel)
  1137. {
  1138. LineName = c_TimeSharing_Price,
  1139. LinePlotType = LinePlotType.Line,
  1140. //UpBrush = Colors.White,
  1141. LineWidth = 1.2,
  1142. };
  1143. if (dataSetPoint != null)
  1144. {
  1145. indicator.CalculateDataPoints = dataSetPoint;
  1146. }
  1147. else
  1148. {
  1149. indicator.CalculateDataPoints = new List<ILineDataPoint>();
  1150. }
  1151. formulaModel.IndicatorList.Add(indicator);
  1152. return displayformula;
  1153. }
  1154. }
  1155. }