aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/RealTimeGraphEx/DirectXGraphs/RealTimeGraphExDirectXMultiLineScroll.cs
blob: 7671272f00439efb17336522bb591c48e3532bf1 (generated by cgit v1.3.1 (git 2.54.0) at 2026-08-03 11:39:33 +0000
' href='#n197'>197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
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<ConcurrentpointsList> _graphPolygons;
        protected Brush _stroke;
        protected Brush _fill;
        protected bool _fillGraph;
        protected double _strokeWidth;

        #endregion

        #region Constructors

        public RealTimeGraphExDirectXMultiLineScroll()
            : base()
        {
            _graphPolygons = new List<ConcurrentpointsList>();
            _graphController = new GraphMultiController();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the graph strokes color.
        /// </summary>
        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)));

        /// <summary>
        /// Gets or sets the graph fill color.
        /// </summary>
        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)));

        /// <summary>
        /// Gets or sets whether the graph will be rendered using the Fill color property.
        /// </summary>
        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<SharpDX.Vector2[]> polygonPoints = new List<SharpDX.Vector2[]>();
                List<SharpDX.Vector2[]> polygonPointsFill = new List<SharpDX.Vector2[]>();
                List<Brush> strokes = new List<Brush>();
                List<Brush> fills = new List<Brush>();

                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<double> GetCurrentPointsOnGraph(int seriesIndex)
        {
            return _graphPolygons[seriesIndex].GetPoints();
        }

        #endregion

    }
}