//The MIT License(MIT)
//Copyright(c) 2016 Alberto Rodriguez & LiveCharts Contributors
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
using System;
using System.Collections.Generic;
using LiveCharts.Definitions.Series;
using LiveCharts.Dtos;
namespace LiveCharts.Helpers
{
///
/// LiveCharts extensions methods
///
public static class Extentions
{
///
/// Executes an Action in every item of a collection
///
/// type to iterate with
/// collection to iterate
/// action to execute
internal static void ForEach(this IEnumerable source, Action predicate)
{
foreach (var item in source) predicate(item);
}
///
/// Splits a collection of points every double.Nan
///
/// collection to split
/// collection of collections
internal static IEnumerable> SplitEachNaN(this IList toSplit)
{
var l = new List(toSplit.Count);
var acum = -1;
foreach (var point in toSplit)
{
if (double.IsNaN(point.X) || double.IsNaN(point.Y))
{
yield return l;
acum += l.Count;
l = new List(toSplit.Count - acum);
}
else
{
l.Add(point);
}
}
yield return l;
}
///
/// Return the inverse axis orientation
///
/// current orientation
/// inverted axis orientation
internal static AxisOrientation Invert(this AxisOrientation axis)
{
return axis == AxisOrientation.X
? AxisOrientation.Y
: AxisOrientation.X;
}
///
/// Converts any collection to chart values
///
/// type to convert
/// values to convert
/// a new ChartValues instance containing the passed collection
public static ChartValues AsChartValues(this IEnumerable values)
{
var l = new ChartValues();
l.AddRange(values);
return l;
}
///
/// Converts an enumeration of series to a SeriesCollection instance.
///
/// The series.
///
public static SeriesCollection AsSeriesCollection(this IEnumerable series)
{
var collection = new SeriesCollection();
collection.AddRange(series);
return collection;
}
///
/// Gets the closest chart point with a given value.
///
/// The target series.
/// The value.
/// the axis orientation
///
public static ChartPoint ClosestPointTo(this ISeriesView series, double value, AxisOrientation orientation)
{
ChartPoint t = null;
var delta = double.PositiveInfinity;
foreach (var point in series.Values.GetPoints(series))
{
var i = orientation == AxisOrientation.X ? point.X : point.Y;
var di = Math.Abs(i - value);
if (di < delta)
{
t = point;
delta = di;
}
}
return t;
}
}
}