aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/RealTimeGraphX/GraphControllerBuilder.cs
blob: 3183604d5d8e01f42d2bb18391b625c4a7d6c67a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using RealTimeGraphX.Controllers;
using RealTimeGraphX.Renderers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RealTimeGraphX
{
    public class GraphControllerBuilder<TDataSeries, XDataPoint, YDataPoint> where XDataPoint : GraphDataPointBase where YDataPoint : GraphDataPointBase where TDataSeries : class, IGraphDataSeries
    {
        public enum RendererType
        {
            Scrolling,
            Erase,
        }

        private List<KeyValuePair<int, Action>> _steps;
        private IGraphController<TDataSeries, XDataPoint, YDataPoint> _controller;
        private int _data_series_counter = 100;

        public GraphControllerBuilder()
        {
            _steps = new List<KeyValuePair<int, Action>>();
        }

        public GraphControllerBuilder<TDataSeries, XDataPoint, YDataPoint> SetRenderer(RendererType renderer, TimeSpan refreshRate)
        {
            return AddStep(0, () =>
            {
                if (renderer == RendererType.Scrolling)
                {
                    _controller.ConnectOutput(new GraphScrollingRenderer<TDataSeries, XDataPoint, YDataPoint>() { RefreshRate = refreshRate });
                }
                else
                {
                    _controller.ConnectOutput(new GraphEraseRenderer<TDataSeries, XDataPoint, YDataPoint>() { RefreshRate = refreshRate });
                }
            });
        }

        public GraphControllerBuilder<TDataSeries, XDataPoint, YDataPoint> SetRange(YDataPoint minimumY, YDataPoint maximumY, XDataPoint maximumX, bool autoY = false)
        {
            return AddStep(1, () =>
            {
                _controller.Range.MinimumY = minimumY;
                _controller.Range.MaximumY = maximumY;
                _controller.Range.MaximumX = maximumX;
                _controller.Range.AutoY = autoY;
            });
        }

        public GraphControllerBuilder<TDataSeries, XDataPoint, YDataPoint> SetPainter<T>() where T : IGraphPainter<TDataSeries>
        {
            return AddStep(2, () =>
            {
                _controller.Output.ThrowIfNull("No renderer was set.");
                _controller.Output.ConnectOutput(Activator.CreateInstance<T>());
            });
        }

        public GraphControllerBuilder<TDataSeries, XDataPoint, YDataPoint> AddSeries(TDataSeries series)
        {
            return AddStep(_data_series_counter++, () =>
            {
                _controller.AddDataSeries(series);
            });
        }

        protected GraphControllerBuilder<TDataSeries, XDataPoint, YDataPoint> AddStep(int index, Action action)
        {
            _steps.Add(new KeyValuePair<int, Action>(index, action));
            return this;
        }

        protected void CommitSteps()
        {
            foreach (var step in _steps.ToList().GroupBy(x => x.Key).Select(g => g.First()).OrderBy(x => x.Key))
            {
                step.Value();
            }
        }

        public IGraphController<TDataSeries, XDataPoint, YDataPoint> Build()
        {
            _controller = new GraphController<TDataSeries, XDataPoint, YDataPoint>();
            CommitSteps();
            return _controller;
        }
    }
}