using RealTimeGraphEx.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; namespace RealTimeGraphEx.DataSeries { public class DataYSeries : DependencyObject { #region Cross Thread Fields internal Brush stroke; internal Brush fill; internal String name; internal bool isVisible; internal bool useFillandStroke; internal double currentValue; #endregion public Brush Stroke { get { return (Brush)GetValue(StrokeProperty); } set { SetValue(StrokeProperty, value); } } public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register("Stroke", typeof(Brush), typeof(DataYSeries), new PropertyMetadata(null, new PropertyChangedCallback(SetCrossThreadFields))); public Brush Fill { get { return (Brush)GetValue(FillProperty); } set { SetValue(FillProperty, value); } } public static readonly DependencyProperty FillProperty = DependencyProperty.Register("Fill", typeof(Brush), typeof(DataYSeries), new PropertyMetadata(null, new PropertyChangedCallback(SetCrossThreadFields))); public String Name { get { return (String)GetValue(NameProperty); } set { SetValue(NameProperty, value); } } public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(String), typeof(DataYSeries), new PropertyMetadata(null, new PropertyChangedCallback(SetCrossThreadFields))); public double CurrentValue { get { return (double)GetValue(CurrentValueProperty); } set { SetValue(CurrentValueProperty, value); } } public static readonly DependencyProperty CurrentValueProperty = DependencyProperty.Register("CurrentValue", typeof(double), typeof(DataYSeries), new PropertyMetadata(0.0,new PropertyChangedCallback(OnCurrentValueChanged))); private static void OnCurrentValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = d as DataYSeries; control.currentValue = control.CurrentValue; } public bool IsVisible { get { return (bool)GetValue(IsVisibleProperty); } set { SetValue(IsVisibleProperty, value); } } public static readonly DependencyProperty IsVisibleProperty = DependencyProperty.Register("IsVisible", typeof(bool), typeof(DataYSeries), new PropertyMetadata(true,new PropertyChangedCallback(SetCrossThreadFields))); public bool UseFillAndStroke { get { return (bool)GetValue(UseFillAndStrokeProperty); } set { SetValue(UseFillAndStrokeProperty, value); } } public static readonly DependencyProperty UseFillAndStrokeProperty = DependencyProperty.Register("UseFillAndStroke", typeof(bool), typeof(DataYSeries), new PropertyMetadata(false)); private static void SetCrossThreadFields(DependencyObject d, DependencyPropertyChangedEventArgs e) { (d as DataYSeries).OnSetCrossThreadFields(); } internal ConcurrentpointsList Points { get; private set; } public DataYSeries() { Points = new ConcurrentpointsList(); } internal void ClearPoints() { Points.Clear(); SetCurrentValue(0); } internal void PushData(List values) { if (values.Count == 0) return; SetCurrentValue(values[values.Count - 1]); Points.AddRange(values); } internal void PushData(double[] values) { if (values.Length == 0) return; SetCurrentValue(values[values.Length - 1]); Points.AddRange(values); } internal void PushData(double value) { SetCurrentValue(value); Points.Add(value); } protected virtual void OnSetCrossThreadFields() { this.Dispatcher.Invoke(() => { name = Name; stroke = Stroke; if (stroke != null) stroke.Freeze(); fill = Fill; if (fill != null) fill.Freeze(); isVisible = IsVisible; useFillandStroke = UseFillAndStroke; }, System.Windows.Threading.DispatcherPriority.Send); } protected virtual void SetCurrentValue(double value) { this.Dispatcher.Invoke(() => { CurrentValue = value; }, System.Windows.Threading.DispatcherPriority.Send); } internal Color? GetStrokeColor() { SolidColorBrush strokeBrush = stroke as SolidColorBrush; if (strokeBrush != null) { return strokeBrush.Color; } else { return null; } } internal Color? GetFillColor() { SolidColorBrush fillBrush = fill as SolidColorBrush; if (fillBrush != null) { return fillBrush.Color; } else { return null; } } public List GetPoints() { return Points.GetPoints(); } } }