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 where XDataPoint : GraphDataPointBase where YDataPoint : GraphDataPointBase where TDataSeries : class, IGraphDataSeries { public enum RendererType { Scrolling, Erase, } private List> _steps; private IGraphController _controller; private int _data_series_counter = 100; public GraphControllerBuilder() { _steps = new List>(); } public GraphControllerBuilder SetRenderer(RendererType renderer, TimeSpan refreshRate) { return AddStep(0, () => { if (renderer == RendererType.Scrolling) { _controller.ConnectOutput(new GraphScrollingRenderer() { RefreshRate = refreshRate }); } else { _controller.ConnectOutput(new GraphEraseRenderer() { RefreshRate = refreshRate }); } }); } public GraphControllerBuilder 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 SetPainter() where T : IGraphPainter { return AddStep(2, () => { _controller.Output.ThrowIfNull("No renderer was set."); _controller.Output.ConnectOutput(Activator.CreateInstance()); }); } public GraphControllerBuilder AddSeries(TDataSeries series) { return AddStep(_data_series_counter++, () => { _controller.AddDataSeries(series); }); } protected GraphControllerBuilder AddStep(int index, Action action) { _steps.Add(new KeyValuePair(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 Build() { _controller = new GraphController(); CommitSteps(); return _controller; } } }