Init
This commit is contained in:
129
DataDisplay/DataSource/CompositeDataSource.cs
Normal file
129
DataDisplay/DataSource/CompositeDataSource.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
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<SKPoint> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Data source that is a composer from several other data sources.</summary>
|
||||
public class CompositeDataSource : IPointDataSource {
|
||||
/// <summary>Initializes a new instance of the <see cref="CompositeDataSource"/> class</summary>
|
||||
public CompositeDataSource() { }
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
dataParts.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CompositeDataSource"/> class.
|
||||
/// </summary>
|
||||
/// <param name="dataSources">Data sources.</param>
|
||||
public CompositeDataSource(params IPointDataSource[] dataSources) {
|
||||
if (dataSources == null)
|
||||
throw new ArgumentNullException("dataSources");
|
||||
|
||||
foreach (var dataSource in dataSources) {
|
||||
AddDataPart(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
private readonly List<IPointDataSource> dataParts = new List<IPointDataSource>();
|
||||
|
||||
public IEnumerable<IPointDataSource> DataParts {
|
||||
get { return dataParts; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds data part.
|
||||
/// </summary>
|
||||
/// <param name="dataPart">The data part.</param>
|
||||
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<IPointEnumerator> 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
|
||||
}
|
||||
}
|
||||
}
|
||||
14
DataDisplay/DataSource/DataSourceExtensions.cs
Normal file
14
DataDisplay/DataSource/DataSourceExtensions.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace DataDisplay.DataSource
|
||||
{
|
||||
public static class DataSourceExtensions
|
||||
{
|
||||
public static CompositeDataSource Join(this IPointDataSource ds1, IPointDataSource ds2)
|
||||
{
|
||||
return new CompositeDataSource(ds1, ds2);
|
||||
}
|
||||
}
|
||||
}
|
||||
110
DataDisplay/DataSource/EnumerableDataSource.cs
Normal file
110
DataDisplay/DataSource/EnumerableDataSource.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace DataDisplay.DataSource
|
||||
{
|
||||
public class EnumerableDataSource<T> : EnumerableDataSourceBase<T>
|
||||
{
|
||||
public EnumerableDataSource(IEnumerable<T> data) : base(data) { }
|
||||
|
||||
public EnumerableDataSource(IEnumerable data) : base(data) { }
|
||||
|
||||
private readonly List<Mapping<T>> mappings = new List<Mapping<T>>();
|
||||
private Func<T, double> xMapping;
|
||||
private Func<T, double> yMapping;
|
||||
private Func<T, Point> xyMapping;
|
||||
|
||||
public Func<T, double> XMapping
|
||||
{
|
||||
get { return xMapping; }
|
||||
set { SetXMapping(value); }
|
||||
}
|
||||
|
||||
public Func<T, double> YMapping
|
||||
{
|
||||
get { return yMapping; }
|
||||
set { SetYMapping(value); }
|
||||
}
|
||||
|
||||
public Func<T, Point> XYMapping
|
||||
{
|
||||
get { return xyMapping; }
|
||||
set { SetXYMapping(value); }
|
||||
}
|
||||
|
||||
public void SetXMapping(Func<T, double> mapping)
|
||||
{
|
||||
if (mapping == null)
|
||||
throw new ArgumentNullException("mapping");
|
||||
|
||||
this.xMapping = mapping;
|
||||
RaiseDataChanged();
|
||||
}
|
||||
|
||||
public void SetYMapping(Func<T, double> mapping)
|
||||
{
|
||||
if (mapping == null)
|
||||
throw new ArgumentNullException("mapping");
|
||||
|
||||
this.yMapping = mapping;
|
||||
RaiseDataChanged();
|
||||
}
|
||||
|
||||
public void SetXYMapping(Func<T, Point> mapping)
|
||||
{
|
||||
if (mapping == null)
|
||||
throw new ArgumentNullException("mapping");
|
||||
|
||||
this.xyMapping = mapping;
|
||||
RaiseDataChanged();
|
||||
}
|
||||
|
||||
public void AddMapping(BindableProperty property, Func<T, object> mapping)
|
||||
{
|
||||
if (property == null)
|
||||
throw new ArgumentNullException("property");
|
||||
if (mapping == null)
|
||||
throw new ArgumentNullException("mapping");
|
||||
|
||||
mappings.Add(new Mapping<T> { Property = property, F = mapping });
|
||||
}
|
||||
|
||||
public override IPointEnumerator GetEnumerator(BindableObject context)
|
||||
{
|
||||
return new EnumerablePointEnumerator<T>(this);
|
||||
}
|
||||
|
||||
internal void FillPoint(T elem, ref Point point)
|
||||
{
|
||||
if (xyMapping != null)
|
||||
{
|
||||
point = xyMapping(elem);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (xMapping != null)
|
||||
{
|
||||
point.X = xMapping(elem);
|
||||
}
|
||||
if (yMapping != null)
|
||||
{
|
||||
point.Y = yMapping(elem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void ApplyMappings(BindableObject target, T elem)
|
||||
{
|
||||
if (target != null)
|
||||
{
|
||||
foreach (var mapping in mappings)
|
||||
{
|
||||
target.SetValue(mapping.Property, mapping.F(elem));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
57
DataDisplay/DataSource/EnumerableDataSourceBase.cs
Normal file
57
DataDisplay/DataSource/EnumerableDataSourceBase.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Diagnostics;
|
||||
using System.Windows;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace DataDisplay.DataSource
|
||||
{
|
||||
/// <summary>Base class for all sources who receive data for charting
|
||||
/// from any IEnumerable of T</summary>
|
||||
/// <typeparam name="T">Type of items in IEnumerable</typeparam>
|
||||
public abstract class EnumerableDataSourceBase<T> : IPointDataSource {
|
||||
private IEnumerable data;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the data.
|
||||
/// </summary>
|
||||
/// <value>The data.</value>
|
||||
public IEnumerable Data {
|
||||
get { return data; }
|
||||
set {
|
||||
if (value == null)
|
||||
throw new ArgumentNullException("value");
|
||||
|
||||
data = value;
|
||||
|
||||
var observableCollection = data as INotifyCollectionChanged;
|
||||
if (observableCollection != null) {
|
||||
observableCollection.CollectionChanged += observableCollection_CollectionChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected EnumerableDataSourceBase(IEnumerable<T> data) : this((IEnumerable)data) { }
|
||||
|
||||
protected EnumerableDataSourceBase(IEnumerable data) {
|
||||
if (data == null)
|
||||
throw new ArgumentNullException("data");
|
||||
Data = data;
|
||||
}
|
||||
|
||||
private void observableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
|
||||
RaiseDataChanged();
|
||||
}
|
||||
|
||||
public event EventHandler DataChanged;
|
||||
public void RaiseDataChanged() {
|
||||
if (DataChanged != null) {
|
||||
DataChanged(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract IPointEnumerator GetEnumerator(BindableObject context);
|
||||
}
|
||||
}
|
||||
37
DataDisplay/DataSource/EnumerablePointEnumerator.cs
Normal file
37
DataDisplay/DataSource/EnumerablePointEnumerator.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Windows;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace DataDisplay.DataSource
|
||||
{
|
||||
/// <summary>This enumerator enumerates given enumerable object as sequence of points</summary>
|
||||
/// <typeparam name="T">Type parameter of source IEnumerable</typeparam>
|
||||
public sealed class EnumerablePointEnumerator<T> : IPointEnumerator {
|
||||
private readonly EnumerableDataSource<T> dataSource;
|
||||
private readonly IEnumerator enumerator;
|
||||
|
||||
public EnumerablePointEnumerator(EnumerableDataSource<T> dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
enumerator = dataSource.Data.GetEnumerator();
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
try{return enumerator.MoveNext();}
|
||||
catch(InvalidOperationException){return false;}
|
||||
}
|
||||
|
||||
public void GetCurrent(ref Point p) {
|
||||
dataSource.FillPoint((T)enumerator.Current, ref p);
|
||||
}
|
||||
|
||||
public void ApplyMappings(BindableObject target) {
|
||||
dataSource.ApplyMappings(target, (T)enumerator.Current);
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
//enumerator.Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
17
DataDisplay/DataSource/IPointDataSource.cs
Normal file
17
DataDisplay/DataSource/IPointDataSource.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace DataDisplay.DataSource
|
||||
{
|
||||
/// <summary>Data source that returns sequence of 2D points.</summary>
|
||||
public interface IPointDataSource
|
||||
{
|
||||
|
||||
/// <summary>Returns object to enumerate points in source.</summary>
|
||||
IPointEnumerator GetEnumerator(BindableObject context);
|
||||
|
||||
/// <summary>This event is raised when contents of source are changed.</summary>
|
||||
event EventHandler DataChanged;
|
||||
}
|
||||
}
|
||||
22
DataDisplay/DataSource/IPointEnumerator.cs
Normal file
22
DataDisplay/DataSource/IPointEnumerator.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace DataDisplay.DataSource
|
||||
{
|
||||
public interface IPointEnumerator : IDisposable {
|
||||
/// <summary>Move to next point in sequence</summary>
|
||||
/// <returns>True if successfully moved to next point
|
||||
/// or false if end of sequence is reached</returns>
|
||||
bool MoveNext();
|
||||
|
||||
/// <summary>Stores current value(s) in given point.</summary>
|
||||
/// <param name="p">Reference to store value</param>
|
||||
/// <remarks>Depending on implementing class this method may set only X or Y
|
||||
/// fields in specified point. That's why GetCurrent is a regular method and
|
||||
/// not a property as in standard enumerators</remarks>
|
||||
void GetCurrent(ref Point p);
|
||||
|
||||
void ApplyMappings(BindableObject target);
|
||||
}
|
||||
}
|
||||
16
DataDisplay/DataSource/Mapping.cs
Normal file
16
DataDisplay/DataSource/Mapping.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace DataDisplay.DataSource
|
||||
{
|
||||
/// <summary>Mapping class holds information about mapping of TSource type
|
||||
/// to some DependencyProperty.</summary>
|
||||
/// <typeparam name="TSource">Mapping source type.</typeparam>
|
||||
internal sealed class Mapping<TSource> {
|
||||
/// <summary>Property that will be set.</summary>
|
||||
internal BindableProperty Property { get; set; }
|
||||
/// <summary>Function that computes value for property from TSource type.</summary>
|
||||
internal Func<TSource, object> F { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user