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
///
/// AllPlots
///
private List _allFormula;
private Chart _chart;
private List _chartHighlights;
private ChartPane _chartPane;
private DrawingToolContainer _drawingToolContainer;
private List _drawingTools;
private Guid _guid;
private SharedDrawingToolContainer _sharedDrawingToolContainer;
private string _symbol;
private ValueScaleDisplay _rightValueScaleDisplay;
private ValueScaleDisplay _leftValueScaleDisplay;
///
/// mScaler
///
private IValueScaler _rightValueScaler;
private IValueScaler _leftValueScaler;
#endregion Fields
#region Constructors
public ScaleLayer()
{
_allFormula = new List();
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();
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
///
/// 层中显示的所有公式
///
public List AllDisplayFormula
{
get
{
return _allFormula;
}
private set
{
_allFormula = value;
}
}
private List _comparisonFormulas = new List();
///
/// 层中显示的所有公式
///
public List ComparisonFormulas
{
get
{
return _comparisonFormulas;
}
private set
{
_comparisonFormulas = value;
}
}
///
/// 是否为主图,区别分Y轴坐标是否显示百分比
///
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 _allPlotbases = new List();
///
/// 获取层中所有的图表
///
public List GetAllPlots()
{
return _allPlotbases;
}
///
/// 获取层中所有的图表
///
public List 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;
}
///
/// 清除所有线与公式
///
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();
foreach (var formula in AllDisplayFormula)
{
list.AddRange(formula.PlotBases);
}
try
{
List.Enumerator enumerator = list.GetEnumerator();
while (enumerator.MoveNext())
{
PlotBase current = enumerator.Current;
current.RemovedFromChart();
}
}
finally
{
//List.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 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();
bool flag = _rightValueScaleDisplay != null;
//只针对左边显示数据,右边显示百分比。
List 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 GetAllPlotElements()
{
var list = new List();
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;
}
///
/// 返回mScaler
///
///
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();
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 items)
{
_chartHighlights = items;
if ( _chart != null)
{
_chart.Refresh();
}
}
///
/// GetmScaler
///
///
internal IValueScaler GetmScaler()
{
return this._leftValueScaler;
}
internal List GetScaleLayer()
{
List list = _drawingTools;
if (list == null)
{
list = new List();
}
return list;
}
internal void ScaleLayer_5516(string symbol, IDrawingTool aDrawing)
{
_drawingToolContainer.AddDrawing(symbol, aDrawing);
}
///
/// 更新所有plot的数据集
///
/// The dataset.
/// if set to true [refresh flag].
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);
}
}
}
///
/// mScaler更改后执行
///
private void ScaleLayer_5544()
{
if (_chart != null)
{
_chart.Refresh(true);
}
}
#endregion Private Methods
#endregion Methods
}
}