| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697 |
- using Microsoft.VisualBasic.CompilerServices;
- using MuchInfo.Chart.Data;
- using MuchInfo.Chart.Data.Interfaces;
- using MuchInfo.Chart.WPF.Helpers;
- using MuchInfo.Chart.WPF.Primitives.Drawing;
- using MuchInfo.Chart.WPF.Primitives.Interfaces;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- namespace MuchInfo.Chart.WPF.Primitives
- {
- public class ScaleLayer : IDeletable
- {
- #region Fields
- /// <summary>
- /// AllPlots
- /// </summary>
- private List<DisplayFormula> _allFormula;
- private Chart _chart;
- private List<IChartHighlight> _chartHighlights;
- private ChartPane _chartPane;
- private DrawingToolContainer _drawingToolContainer;
- private List<IDrawingTool> _drawingTools;
- private Guid _guid;
- private SharedDrawingToolContainer _sharedDrawingToolContainer;
- private string _symbol;
- private ValueScaleDisplay _rightValueScaleDisplay;
- private ValueScaleDisplay _leftValueScaleDisplay;
- /// <summary>
- /// mScaler
- /// </summary>
- private IValueScaler _rightValueScaler;
- private IValueScaler _leftValueScaler;
- #endregion Fields
- #region Constructors
- public ScaleLayer()
- {
- _allFormula = new List<DisplayFormula>();
- RightScaler = new ValueScalerArithmetic(false);
- LeftScaler = new ValueScalerArithmetic(false);
- _rightValueScaleDisplay = new ValueScaleDisplay(RightScaler);
- _leftValueScaleDisplay = new ValueScaleDisplay(LeftScaler);
- _guid = Guid.NewGuid();
- _drawingToolContainer = new DrawingToolContainer(this);
- _sharedDrawingToolContainer = new SharedDrawingToolContainer(this);
- _chartHighlights = null;
- _rightValueScaleDisplay.ScaleLayer = this;
- _leftValueScaleDisplay.ScaleLayer = this;
- AllDisplayFormula=new List<DisplayFormula>();
- SharedDrawingManager.AllDrawingsChanged -= new SharedDrawingManager.AllDrawingsChangedEventHandler(OnAllDrawingsChanged);
- SharedDrawingManager.DrawingChanged -= new SharedDrawingManager.DrawingChangedEventHandler(OnDrawingChanged);
- SharedDrawingManager.DrawingsChangedForSym -= new SharedDrawingManager.DrawingsChangedForSymEventHandler(OnDrawingsChangedForSym);
- SharedDrawingManager.AllDrawingsChanged += new SharedDrawingManager.AllDrawingsChangedEventHandler(OnAllDrawingsChanged);
- SharedDrawingManager.DrawingChanged += new SharedDrawingManager.DrawingChangedEventHandler(OnDrawingChanged);
- SharedDrawingManager.DrawingsChangedForSym += new SharedDrawingManager.DrawingsChangedForSymEventHandler(OnDrawingsChangedForSym);
- }
- #endregion Constructors
- #region Properties
- #region Public Properties
- /// <summary>
- /// 层中显示的所有公式
- /// </summary>
- public List<DisplayFormula> AllDisplayFormula
- {
- get
- {
- return _allFormula;
- }
- private set
- {
- _allFormula = value;
- }
- }
- private List<DisplayFormula> _comparisonFormulas = new List<DisplayFormula>();
- /// <summary>
- /// 层中显示的所有公式
- /// </summary>
- public List<DisplayFormula> ComparisonFormulas
- {
- get
- {
- return _comparisonFormulas;
- }
- private set
- {
- _comparisonFormulas = value;
- }
- }
- /// <summary>
- /// 是否为主图,区别分Y轴坐标是否显示百分比
- /// </summary>
- internal bool IsMainPlot
- {
- get
- {
- bool result = false;
- foreach (var formula in AllDisplayFormula)
- {
- if (formula.FormulaModel != null && formula.FormulaModel.IsMain)
- {
- result = formula.FormulaModel.IsMain;
- break;
- }
- }
- return result;
- }
- }
- private List<PlotBase> _allPlotbases = new List<PlotBase>();
- /// <summary>
- /// 获取层中所有的图表
- /// </summary>
- public List<PlotBase> GetAllPlots()
- {
- return _allPlotbases;
-
- }
- /// <summary>
- /// 获取层中所有的图表
- /// </summary>
- public List<PlotBase> AllPlots
- {
- get
- {
- return _allPlotbases;
- }
-
- }
- public IValueScaler RightScaler
- {
- get
- {
- return _rightValueScaler;
- }
- set
- {
- var obj = new ScalePropertiesChangedEventHandler(ScaleLayer_5544);
- bool flag = _rightValueScaler != null;
- if (flag)
- {
- _rightValueScaler.ScalePropertiesChanged -= obj;
- }
- _rightValueScaler = value;
- flag = (_rightValueScaler != null);
- if (flag)
- {
- _rightValueScaler.ScalePropertiesChanged += obj;
- }
- }
- }
- public IValueScaler LeftScaler
- {
- get
- {
- return _leftValueScaler;
- }
- set
- {
- var obj = new ScalePropertiesChangedEventHandler(ScaleLayer_5544);
- bool flag = _leftValueScaler != null;
- if (flag)
- {
- _leftValueScaler.ScalePropertiesChanged -= obj;
- }
- _leftValueScaler = value;
- flag = (_leftValueScaler != null);
- if (flag)
- {
- _leftValueScaler.ScalePropertiesChanged += obj;
- }
- }
- }
- public Guid ScaleGUID
- {
- get
- {
- return _guid;
- }
- }
- #endregion Public Properties
- #endregion Properties
- #region Methods
- #region Public Methods
- public IDrawingToolSpec AddDrawing(string symbol, IDrawingTool aDrawing, TimeSpan aTimeFrame)
- {
- // _drawingTools = null;
- //return ScaleLayer_1916.AddDrawing(symbol, aDrawing);
- return _sharedDrawingToolContainer.AddNewDrawing(symbol, aDrawing, aTimeFrame, _guid, HasPricePlot());
- //bool flag = false; //BFCSettings.RunMode() == BFCSettings.eRunMode.eBFCWhiteLabel;
- //IDrawingToolSpec result;
- //if (flag)
- //{
- // result = ScaleLayer_1916.AddDrawing(aSym, aDrawing);
- //}
- //else
- //{
- // result = ScaleLayer_1917.AddNewDrawing(aSym, aDrawing, aTimeFrame, ScaleLayer_1915, ScaleLayer_5527());
- //}
- //return result;
- }
- public IDrawingToolSpec AddDrawing(string symbol, IDrawingTool aDrawing)
- {
- //var Rlt = new IDrawingToolSpec();
- _drawingTools = null;
- //if (_chart != null)
- //{
- return _sharedDrawingToolContainer.AddNewDrawing(symbol, aDrawing, _chart.CurrentTimeFrame, _guid, HasPricePlot());
- //}
-
- //IDrawingToolSpec result;
- //if (flag)
- //{
- // result = ScaleLayer_1916.AddDrawing(aSym, aDrawing);
- //}
- //else
- //{
- // result = ScaleLayer_1917.AddNewDrawing(symbol, aDrawing, _chart.CurrentTimeFrame, ScaleLayer_1915, ScaleLayer_5527());
- //}
- //return Rlt;
- }
- /// <summary>
- /// 清除所有线与公式
- /// </summary>
- public void ClearFormulas()
- {
- foreach (var displayFormula in AllDisplayFormula)
- {
- if (displayFormula.FormulaModel != null &&
- !displayFormula.FormulaModel.IsMain && displayFormula.PlotBases!=null)
- {
- foreach (var plotBase in displayFormula.PlotBases)
- {
- this.AllPlots.Remove(plotBase);
- }
- }
- }
- //清除所有公式除主图外
- AllDisplayFormula.RemoveAll(z => !z.FormulaModel.IsMain);
- }
- public DrawingToolContainer AllXMLDrawings()
- {
- return _drawingToolContainer;
- }
- public PlotBase FirstPlot()
- {
- return null;
- }
- public void OnDelete()
- {
- RightScaler = null;
- LeftScaler = null;
- var list = new List<PlotBase>();
- foreach (var formula in AllDisplayFormula)
- {
- list.AddRange(formula.PlotBases);
- }
- try
- {
- List<PlotBase>.Enumerator enumerator = list.GetEnumerator();
- while (enumerator.MoveNext())
- {
- PlotBase current = enumerator.Current;
- current.RemovedFromChart();
- }
- }
- finally
- {
- //List<PlotBase>.Enumerator enumerator;
- //enumerator.Dispose();
- }
- SharedDrawingManager.AllDrawingsChanged -= new SharedDrawingManager.AllDrawingsChangedEventHandler(OnAllDrawingsChanged);
- SharedDrawingManager.DrawingChanged -= new SharedDrawingManager.DrawingChangedEventHandler(OnDrawingChanged);
- SharedDrawingManager.DrawingsChangedForSym -= new SharedDrawingManager.DrawingsChangedForSymEventHandler(OnDrawingsChangedForSym);
- }
- #endregion Public Methods
- #region Internal Methods
- internal List<FrameworkElement> BuildScaleLayer(Chart owner, ChartPane aPane, IDateScaler dateScale, Rect aRect)
- {
- _chart = owner;
- _chartPane = aPane;
- //if (_chartPane.IsPriceHistory)
- //{
- if (IsMainPlot || aPane.IsTimeSpaning) //分时图显示百分比
- {
- // _leftValueScaleDisplay.CurrentValueScaler.ShowScaleAsPercent = true;
- //_rightValueScaleDisplay.CurrentValueScaler.ShowScaleAsPercent = true;
- }
- // }
- var list = new List<FrameworkElement>();
- bool flag = _rightValueScaleDisplay != null;
- //只针对左边显示数据,右边显示百分比。
- List<ValueScaleDisplay.ValueScaleDisplay_Y> ylist = null;
- if (flag)
- {
- RightScaler.IsTimeSpaning = aPane.IsTimeSpaning;
- if (aPane.IsTimeSpaning)
- {
- RightScaler.TimeSpaningLast = owner.CurrentGoods.PreClose; //分时图收盘价
- }
- RightScaler.CalculateScale(AllPlots, dateScale, aPane.IsTimeSpaning);
- // list.AddRange(_leftValueScaleDisplay.GetValueScaleDisplay(owner, dateScale, aRect, null, true, aPane.IsTimeSpaning));
- list.AddRange(_rightValueScaleDisplay.GetValueScaleDisplay(owner, dateScale, aRect, null, true, aPane.IsTimeSpaning));
- //左边为数值,取左边数据为右边做参照
- // ylist = _leftValueScaleDisplay.GetYList((float)aRect.Height, LeftScaler, aPane.IsTimeSpaning);
- ylist = _rightValueScaleDisplay.GetYList((float)aRect.Height, RightScaler, aPane.IsTimeSpaning);
- }
- flag = _leftValueScaleDisplay != null;
- if (flag)
- {
- // RightScaler.IsTimeSpaning = aPane.IsTimeSpaning;
- LeftScaler.IsTimeSpaning = aPane.IsTimeSpaning;
- if (aPane.IsTimeSpaning)
- {
- // RightScaler.TimeSpaningLast = owner.CurrentGoods.PreClose; //分时图收盘价
- LeftScaler.TimeSpaningLast = owner.CurrentGoods.PreClose; //分时图收盘价
- }
- // RightScaler.CalculateScale(AllPlots, dateScale, aPane.IsTimeSpaning);
- LeftScaler.CalculateScale(AllPlots, dateScale, aPane.IsTimeSpaning);
- // list.AddRange(_rightValueScaleDisplay.GetValueScaleDisplay(owner, dateScale, aRect, ylist, false, aPane.IsTimeSpaning));
- list.AddRange(_leftValueScaleDisplay.GetValueScaleDisplay(owner, dateScale, aRect, ylist, false, aPane.IsTimeSpaning));
- }
- var plots = AllPlots;
- if (plots != null)
- {
- foreach (var plot in plots)
- {
- var comparison = plot as ComparisonPlot;
- if (comparison == null)
- {
- list.AddRange(plot.GetElements(owner, this, dateScale, RightScaler, aRect));
- }
- else
- {
- list.AddRange(comparison.GetElements(owner, this, dateScale, RightScaler, aRect));
- }
- }
- }
- flag = (_leftValueScaleDisplay != null);
- if (flag)
- {
- _leftValueScaleDisplay.BuildYList(aPane, aRect);
- }
- flag = (_rightValueScaleDisplay != null);
- if (flag)
- {
- _rightValueScaleDisplay.BuildYList(aPane, aRect);
- }
- flag = true;
- if (_symbol != null && owner.CurrentSymbol != null)
- {
- if (string.CompareOrdinal(_symbol, owner.CurrentSymbol) == 0)
- {
- flag = false;
- }
- }
- if (flag)
- {
- _drawingTools = null;
- _symbol = owner.CurrentSymbol;
- }
- flag = (_drawingTools == null);
- if (flag)
- {
- _drawingTools = _drawingToolContainer.DrawingForSymbol(owner.CurrentSymbol);
- flag = true;//(_chart == null || !ScaleLayer_1913.IgnoreSharedDrawings);
- if (flag)
- {
- _drawingTools.AddRange(_sharedDrawingToolContainer.DrawingForSymbol(owner.CurrentSymbol, _guid, HasPricePlot(), _chart));
- }
- }
- if (_drawingTools != null)
- {
- foreach (var drawingTool in _drawingTools)
- {
- if (drawingTool != null)
- {
- list.AddRange(drawingTool.GetPlots(owner, this, aRect, true));
- }
- }
- }
- if (_chartHighlights != null)
- {
- foreach (var current in _chartHighlights)
- {
- if (current != null)
- {
- list.Add(current.GetDrawing(dateScale, RightScaler, aRect));
- }
- }
- }
- return list;
- }
- internal void ClearDrawingToolContainer()
- {
- _drawingToolContainer.Clear();
- }
- internal List<FrameworkElement> GetAllPlotElements()
- {
- var list = new List<FrameworkElement>();
- if (_allFormula == null) return list;
- foreach (var formula in _allFormula)
- {
- if (!formula.IsComparison)
- list.AddRange(formula.AddLegendButton());
- }
- return list;
- }
- internal ChartPane GetChartPane()
- {
- return _chartPane;
- }
- internal string GetFirstPlotDescription()
- {
- bool flag = AllDisplayFormula.Count > 0;
- string result;
- if (flag)
- {
- result = AllDisplayFormula[0].Description;
- }
- else
- {
- result = string.Empty;
- }
- return result;
- }
- internal ValueScaleDisplay GetRightValueScaleDisplay()
- {
- return _rightValueScaleDisplay;
- }
- internal ValueScaleDisplay GetLeftValueScaleDisplay()
- {
- return _leftValueScaleDisplay;
- }
- /// <summary>
- /// 返回mScaler
- /// </summary>
- /// <returns></returns>
- internal IValueScaler GetRightValueScaler()
- {
- return RightScaler;
- }
- internal IValueScaler GetLeftValueScaler()
- {
- return LeftScaler;
- }
- internal bool HasPricePlot()
- {
- var displayFormulas = AllDisplayFormula;
- if (displayFormulas != null && displayFormulas.Any())
- {
- foreach (var formula in displayFormulas)
- {
- if (formula.FormulaModel != null && formula.FormulaModel.IsMain) return true;
- }
- }
- return false;
- }
- internal void RefreshChart(bool alsoInvalidate)
- {
- try
- {
- _drawingTools = null;
- if (_chart != null && alsoInvalidate)
- {
- _chart.Refresh(false);
- }
- }
- catch (Exception expr_4E)
- {
- ProjectData.SetProjectError(expr_4E);
- ProjectData.ClearProjectError();
- }
- }
- internal void RemoveAllPlots()
- {
- var list = new List<PlotBase>();
- list.AddRange(AllPlots);
- foreach (var current in list)
- {
- current.RemovedFromChart();
- }
- }
- internal void RemoveFromChartPane(PlotBase aPlot)
- {
- RemovePlot(aPlot);
- if (AllPlots.Count == 0)
- {
- _chartPane.RemoveScaleLayer(this);
- }
- }
- internal void RemovePlot(PlotBase aPlot)
- {
- AllPlots.Remove(aPlot);
- aPlot.RemovedFromChart();
- }
- internal bool ScaleGUIDNotEquals()
- {
- if (_drawingTools == null) return false;
- foreach (var current in _drawingTools)
- {
- var toolSpec = current.OwnerSpec as SharedDrawingToolSpec;
- if (toolSpec != null)
- {
- var drawingInfo = toolSpec.GetDrawingInfo();
- if (drawingInfo != null)
- {
- if (!drawingInfo.ScaleGUID.HasValue)
- {
- if (!ScaleGUID.Equals(drawingInfo.OriginalScaleGUID()))
- {
- return true;
- }
- }
- }
- }
- }
- return false;
- }
- internal void ScaleLayer_5514(List<IChartHighlight> items)
- {
- _chartHighlights = items;
- if ( _chart != null)
- {
- _chart.Refresh();
- }
- }
- /// <summary>
- /// GetmScaler
- /// </summary>
- /// <returns></returns>
- internal IValueScaler GetmScaler()
- {
- return this._leftValueScaler;
- }
- internal List<IDrawingTool> GetScaleLayer()
- {
- List<IDrawingTool> list = _drawingTools;
- if (list == null)
- {
- list = new List<IDrawingTool>();
- }
- return list;
- }
- internal void ScaleLayer_5516(string symbol, IDrawingTool aDrawing)
- {
- _drawingToolContainer.AddDrawing(symbol, aDrawing);
- }
- /// <summary>
- /// 更新所有plot的数据集
- /// </summary>
- /// <param name="dataset">The dataset.</param>
- /// <param name="refreshFlag">if set to <c>true</c> [refresh flag].</param>
- internal void UpdatePlotsDataSource(ILineDataSet dataset, bool refreshFlag)
- {
- //清除plots-性能问题
- AllPlots.Clear();
- if (_allFormula == null || !_allFormula.Any()) return;
- foreach (var formula in _allFormula)
- {
- if (formula.IsComparison) continue;
- formula.SetSource(dataset, refreshFlag);
- }
- foreach (var formula in ComparisonFormulas)
- {
- formula.CreatePlot();
- }
- }
- #endregion Internal Methods
- #region Private Methods
- private void OnAllDrawingsChanged()
- {
- RefreshChart(true);
- }
- private void OnDrawingChanged(DrawingToolInfo info)
- {
- OnDrawingsChangedForSym(info.Symbol);
- }
- private void OnDrawingsChangedForSym(string symbol)
- {
- if (string.IsNullOrWhiteSpace(symbol))
- {
- RefreshChart(true);
- }
- else
- {
- if (_chart != null && string.CompareOrdinal(_chart.CurrentSymbol, symbol) == 0)
- {
- RefreshChart(true);
- }
- }
- }
- /// <summary>
- /// mScaler更改后执行
- /// </summary>
- private void ScaleLayer_5544()
- {
- if (_chart != null)
- {
- _chart.Refresh(true);
- }
- }
- #endregion Private Methods
- #endregion Methods
- }
- }
|