using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; using RealTimeGraphEx.Models; using RealTimeGraphEx.DataSeries; using RealTimeGraphEx.Controllers; using System.Runtime.ExceptionServices; using RealTimeGraphEx.DX2D; using System.Windows.Controls; using System.Windows.Threading; namespace RealTimeGraphEx.DirectXGraphs { public class RealTimeGraphExDirectXMultiLineScroll : RealTimeGraphExMultiBase { #region Cross Thread Fields protected Direct2DControl dxGraph; protected List _graphPolygons; protected Brush _stroke; protected Brush _fill; protected bool _fillGraph; protected double _strokeWidth; #endregion #region Constructors public RealTimeGraphExDirectXMultiLineScroll() : base() { _graphPolygons = new List(); _graphController = new GraphMultiController(); } #endregion #region Properties /// /// Gets or sets the graph strokes color. /// public Brush Stroke { get { return (Brush)GetValue(StrokeProperty); } set { SetValue(StrokeProperty, value); } } public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register("Stroke", typeof(Brush), typeof(RealTimeGraphExDirectXMultiLineScroll), new PropertyMetadata(Brushes.Red, new PropertyChangedCallback(CrossModelChanged))); /// /// Gets or sets the graph fill color. /// public Brush Fill { get { return (Brush)GetValue(FillProperty); } set { SetValue(FillProperty, value); } } public static readonly DependencyProperty FillProperty = DependencyProperty.Register("Fill", typeof(Brush), typeof(RealTimeGraphExDirectXMultiLineScroll), new PropertyMetadata(Brushes.Transparent, new PropertyChangedCallback(CrossModelChanged))); /// /// Gets or sets whether the graph will be rendered using the Fill color property. /// public bool FillGraph { get { return (bool)GetValue(FillGraphProperty); } set { SetValue(FillGraphProperty, value); } } public static readonly DependencyProperty FillGraphProperty = DependencyProperty.Register("FillGraph", typeof(bool), typeof(RealTimeGraphExDirectXMultiLineScroll), new PropertyMetadata(false, new PropertyChangedCallback(CrossModelChanged))); public double StrokeWidth { get { return (double)GetValue(StrokeWidthProperty); } set { SetValue(StrokeWidthProperty, value); } } public static readonly DependencyProperty StrokeWidthProperty = DependencyProperty.Register("StrokeWidth", typeof(double), typeof(RealTimeGraphExDirectXMultiLineScroll), new PropertyMetadata(1.0, new PropertyChangedCallback(CrossModelChanged))); #endregion #region Override Methods protected override void Initialize() { base.Initialize(); dxGraph = new DXGraphSurfaceMultiScroll() { HorizontalAlignment = System.Windows.HorizontalAlignment.Left, VerticalAlignment = System.Windows.VerticalAlignment.Stretch }; gridMain.Children.Remove(img); if (!gridMain.Children.Contains(dxGraph)) { gridMain.Children.Add(dxGraph); } dxGraph.IsHitTestVisible = false; Grid.SetColumnSpan(dxGraph, 3); } protected override void OnControllerChanged() { if (_graphController != null) { _graphController.RegisterMethods(ClearGraph, StartPushThread, SetPaused, ChangeRenderMode, null); foreach (var dataSeries in _graphController.DataSeriesCollection) { _graphPolygons.Add(new ConcurrentpointsList()); } } } protected override void OnSizeChanged(object sender, SizeChangedEventArgs e) { OnSetCrossThreadFields(); } protected override void OnSetCrossThreadFields() { base.OnSetCrossThreadFields(); this.Dispatcher.Invoke(() => { _maxPoints = MaxPoints; _scaleFactor = _width / _maxPoints; _strokeWidth = StrokeWidth; _fill = Fill; _stroke = Stroke; _fillGraph = FillGraph; }, System.Windows.Threading.DispatcherPriority.Send); } protected override void OnClearGraph() { _graphPolygons.ForEach(x => x.Clear()); _graphController.ClearPoints(); base.OnClearGraph(); } protected override double ConvertYToImageY(double value) { double valuePrecentage = ((((value - (_minimum - (_strokeWidth / 2))) * 100) / (_maximum - (_minimum - (_strokeWidth / 2)))) * ((_height) - (_strokeWidth / 2))) / 100; return valuePrecentage; } protected override double ConvertYToImageYFliped(double value) { double valuePrecentage = ConvertYToImageY(value); valuePrecentage = (_height - (_strokeWidth / 2)) - valuePrecentage; //Flip return valuePrecentage; } protected internal override void OnRenderGraph() { if (_graphController != null && _graphController.dataSeriesCollection.Count > 0 && _graphController.TotalPoints > 0 && _width > 1 && _height > 1) { var pointsCollection = _graphController.GetAndClearAllPoints(); if (!_isPaused) { Parallel.For(0, pointsCollection.Count, (i) => { for (int j = 0; j < pointsCollection[(int)i].Count; j++) { double value = pointsCollection[(int)i][j]; NormalizeValue(ref value); _graphPolygons[i].Add(value); } }); if (_graphPolygons[0].Count > _maxPoints + _graphController.dataSeriesCollection[0].Points.Count) { Parallel.ForEach(_graphPolygons, (polygon) => { polygon.RemoveFromStart(polygon.Count - (_maxPoints + _graphController.dataSeriesCollection[0].Points.Count)); }); } } } updateCounter++; if (updateCounter >= 1) { updateCounter = 0; if (!_disableRendering) { OnDrawVisuals(); } } } protected override void OnDragging(System.Windows.Controls.Primitives.DragDeltaEventArgs e) { base.OnDragging(e); } #endregion #region Virtual Methods protected virtual void OnDrawVisuals() { if (_graphPolygons[0].Count > 0) { List polygonPoints = new List(); List polygonPointsFill = new List(); List strokes = new List(); List fills = new List(); for (int i = 0; i < _graphPolygons.Count; i++) { var polygon = _graphPolygons[i]; double scale = _width / (polygon.Count - 1); if (polygon.Count > 0 && _graphController.dataSeriesCollection[i].isVisible) { Brush stroke = _graphController.dataSeriesCollection[i].stroke != null ? _graphController.dataSeriesCollection[i].stroke : _stroke; Brush fill = _graphController.dataSeriesCollection[i].fill != null ? _graphController.dataSeriesCollection[i].fill : _fill; strokes.Add(stroke); fills.Add(fill); polygonPoints.Add(polygon.ToDXPolygonPoints(_offSetX, _offSetY, scale, ConvertYToImageYFliped)); polygonPointsFill.Add(polygon.ToDXPolygonPointsFill(_offSetX, _offSetY, _mainWidth, _mainHeight, scale, ConvertYToImageYFliped)); } } (dxGraph as DXGraphSurfaceMultiScroll).SetProperties( polygonPoints, _fillGraph ? polygonPointsFill : null, strokes, _strokeWidth, _fillGraph, fills, _antialiased); dxGraph.EnableRendering(); } } public virtual List GetCurrentPointsOnGraph(int seriesIndex) { return _graphPolygons[seriesIndex].GetPoints(); } #endregion } }