using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Collections; using Xamarin.Forms; using SkiaSharp; namespace DataDisplay.DataSource { public class DataSourceHelper { private DataSourceHelper() { } public static IEnumerable GetPoints(IPointDataSource dataSource) { if (dataSource == null) throw new ArgumentNullException("dataSource"); using (IPointEnumerator enumerator = dataSource.GetEnumerator(null)) { while (enumerator.MoveNext()) { Point xPoint=new Point(); enumerator.GetCurrent(ref xPoint); yield return new SKPoint((float)xPoint.X,(float)xPoint.Y); xPoint=new Point(); } } } } /// Data source that is a composer from several other data sources. public class CompositeDataSource : IPointDataSource { /// Initializes a new instance of the class public CompositeDataSource() { } public void Clear() { dataParts.Clear(); } /// /// Initializes a new instance of the class. /// /// Data sources. public CompositeDataSource(params IPointDataSource[] dataSources) { if (dataSources == null) throw new ArgumentNullException("dataSources"); foreach (var dataSource in dataSources) { AddDataPart(dataSource); } } private readonly List dataParts = new List(); public IEnumerable DataParts { get { return dataParts; } } /// /// Adds data part. /// /// The data part. public void AddDataPart(IPointDataSource dataPart) { if (dataPart == null) throw new ArgumentNullException("dataPart"); dataParts.Add(dataPart); dataPart.DataChanged += OnPartDataChanged; } private void OnPartDataChanged(object sender, EventArgs e) { RaiseDataChanged(); } #region IPointSource Members public event EventHandler DataChanged; protected void RaiseDataChanged() { if (DataChanged != null) { DataChanged(this, EventArgs.Empty); } } public IPointEnumerator GetEnumerator(BindableObject context) { return new CompositeEnumerator(this, context); } #endregion private sealed class CompositeEnumerator : IPointEnumerator { private readonly IEnumerable enumerators; public CompositeEnumerator(CompositeDataSource dataSource, BindableObject context) { enumerators = dataSource.dataParts.Select(part => part.GetEnumerator(context)).ToList(); } #region IChartPointEnumerator Members public bool MoveNext() { bool res = false; foreach (var enumerator in enumerators) { res |= enumerator.MoveNext(); } return res; } public void ApplyMappings(BindableObject glyph) { foreach (var enumerator in enumerators) { enumerator.ApplyMappings(glyph); } } public void GetCurrent(ref Point p) { foreach (var enumerator in enumerators) { enumerator.GetCurrent(ref p); } } public void Dispose() { foreach (var enumerator in enumerators) { enumerator.Dispose(); } } #endregion } } }