Class DataSourceUtilities
- Namespace
- ScottPlot
- Assembly
- ScottPlot.dll
public static class DataSourceUtilities
- Inheritance
-
DataSourceUtilities
- Inherited Members
Methods
BinarySearch<T, TList>(TList, int, int, T, IComparer<T>?)
Searches a range of elements in a one-dimensional sorted array for a value, using the specified IComparer<T> generic interface.
public static int BinarySearch<T, TList>(this TList sortedList, int index, int length, T value, IComparer<T>? comparer) where TList : IEnumerable<T>
Parameters
sortedListTListindexintThe starting index of the range to search.
lengthintThe length of the range to search.
valueTThe object to search for.
comparerIComparer<T>The IComparer<T> implementation to use when comparing elements.-or- null to use the IComparable<T> implementation of each element.
Returns
- int
The index of the specified
valuein the specifiedarray, ifvalueis found; otherwise, a negative number. Ifvalueis not found andvalueis less than one or more elements inarray, the negative number returned is the bitwise complement of the index of the first element that is larger thanvalue. Ifvalueis not found andvalueis greater than all elements inarray, the negative number returned is the bitwise complement of (the index of the last element plus 1). If this method is called with a non-sortedarray, the return value can be incorrect and a negative number could be returned, even ifvalueis present inarray.
Type Parameters
TThe type of the elements of the array.
TList
Exceptions
- ArgumentNullException
arrayis null.- ArgumentOutOfRangeException
indexis less than the lower bound ofarray.-or-lengthis less than zero.- ArgumentException
indexandlengthdo not specify a valid range inarray.-or-compareris null, andvalueis of a type that is not compatible with the elements ofarray.- InvalidOperationException
compareris null, andTdoes not implement the IComparable<T> generic interface.
GetClosestIndex(Coordinates[], Coordinates, IndexRange)
Gets the closest index to a specified value from the sortedList
public static int GetClosestIndex(Coordinates[] sortedList, Coordinates value, IndexRange indexRange)
Parameters
sortedListCoordinates[]The collection to search. Collection must be in ascending order.
valueCoordinatesThe value to search for in the collection
indexRangeIndexRangeProvides details about the range of indexes to search
Returns
- int
The index of the item that is closest to the
value
GetClosestIndex(List<Coordinates>, Coordinates, IndexRange)
Gets the closest index to a specified value from the sortedList
public static int GetClosestIndex(List<Coordinates> sortedList, Coordinates value, IndexRange indexRange)
Parameters
sortedListList<Coordinates>The collection to search. Collection must be in ascending order.
valueCoordinatesThe value to search for in the collection
indexRangeIndexRangeProvides details about the range of indexes to search
Returns
- int
The index of the item that is closest to the
value
GetClosestIndex(List<double>, double, IndexRange)
Gets the closest index to a specified value from the sortedList
public static int GetClosestIndex(List<double> sortedList, double value, IndexRange indexRange)
Parameters
sortedListList<double>The collection to search. Collection must be in ascending order.
valuedoubleThe value to search for in the collection
indexRangeIndexRangeProvides details about the range of indexes to search
Returns
- int
The index of the item that is closest to the
value
GetClosestIndex(double[], double, IndexRange)
Gets the closest index to a specified value from the sortedList
public static int GetClosestIndex(double[] sortedList, double value, IndexRange indexRange)
Parameters
sortedListdouble[]The collection to search. Collection must be in ascending order.
valuedoubleThe value to search for in the collection
indexRangeIndexRangeProvides details about the range of indexes to search
Returns
- int
The index of the item that is closest to the
value
GetClosestIndex<TValue, TList>(TList, TValue, IndexRange, IComparer<TValue>?)
Gets the closest index to a specified value from the sortedList
public static int GetClosestIndex<TValue, TList>(this TList sortedList, TValue value, IndexRange indexRange, IComparer<TValue>? comparer) where TList : IEnumerable<TValue>
Parameters
sortedListTListThe collection to search. Collection must be in ascending order.
valueTValueThe value to search for in the collection
indexRangeIndexRangeProvides details about the range of indexes to search
comparerIComparer<TValue>A comparer used to determine equality - recommend default comparer or Default
Returns
- int
The index of the item that is closest to the
value
Type Parameters
TValueThe type of values in the collection
TListMust be an object that implements IList or IReadOnlyList. List{T} | T[] are preferred.
GetNearest(IDataSource, Coordinates, RenderDetails, float, IXAxis?, IYAxis?)
Get the nearest datapoint on the plot from the dataSource, based on the mouseLocation and the renderInfo
public static DataPoint GetNearest(IDataSource dataSource, Coordinates mouseLocation, RenderDetails renderInfo, float maxDistance, IXAxis? xAxis = null, IYAxis? yAxis = null)
Parameters
dataSourceIDataSourceThe data source
mouseLocationCoordinatesthe mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)
renderInfoRenderDetailsmaxDistancefloatThe maximum distance to search
xAxisIXAxisThe X-Axis of assigned to the datasource. If not specified, uses the bottom axis.
yAxisIYAxisThe X-Axis of assigned to the datasource. If not specified, uses the left axis.
Returns
- DataPoint
If match found : returns a datapoint that represents the closest (X,Y) coordinate on the plot, and the index that can be used to get the values from the DataSource.
If no match found : returns None
Remarks
This is the original way to locate the nearest DataPoint from the collection, and is safe for unsorted collections (such as Scatter)
Exceptions
GetNearestFast(IDataSource, Coordinates, RenderDetails, float, IXAxis?, IYAxis?)
Get the nearest datapoint on the plot from the dataSource, based on the mouseLocation and the renderInfo
public static DataPoint GetNearestFast(IDataSource dataSource, Coordinates mouseLocation, RenderDetails renderInfo, float maxDistance, IXAxis? xAxis = null, IYAxis? yAxis = null)
Parameters
dataSourceIDataSourceThe data source
mouseLocationCoordinatesthe mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)
renderInfoRenderDetailsmaxDistancefloatThe maximum distance to search
xAxisIXAxisThe X-Axis of assigned to the datasource. If not specified, uses the bottom axis.
yAxisIYAxisThe X-Axis of assigned to the datasource. If not specified, uses the left axis.
Returns
- DataPoint
If match found : returns a datapoint that represents the closest (X,Y) coordinate on the plot, and the index that can be used to get the values from the DataSource.
If no match found : returns None
Remarks
This is a faster way to locate the nearest DataPoint from the collection, but it requires the collection to be sorted in ascending order. ( Signal | SignalXY )
Exceptions
GetNearestSmart(IDataSource, Coordinates, RenderDetails, float, IXAxis?, IYAxis?)
Get the nearest datapoint on the plot from the dataSource, based on the mouseLocation and the renderInfo
public static DataPoint GetNearestSmart(IDataSource dataSource, Coordinates mouseLocation, RenderDetails renderInfo, float maxDistance, IXAxis? xAxis = null, IYAxis? yAxis = null)
Parameters
dataSourceIDataSourceThe data source
mouseLocationCoordinatesthe mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)
renderInfoRenderDetailsmaxDistancefloatThe maximum distance to search
xAxisIXAxisThe X-Axis of assigned to the datasource. If not specified, uses the bottom axis.
yAxisIYAxisThe X-Axis of assigned to the datasource. If not specified, uses the left axis.
Returns
- DataPoint
If match found : returns a datapoint that represents the closest (X,Y) coordinate on the plot, and the index that can be used to get the values from the DataSource.
If no match found : returns None
Remarks
Checks IsSorted() and decides to call the Fast or Original method accordingly
Exceptions
GetNearestX(IDataSource, Coordinates, RenderDetails, float, IXAxis?)
Get the nearest datapoint from the dataSource, based on the mouseLocation X location and the renderInfo
public static DataPoint GetNearestX(IDataSource dataSource, Coordinates mouseLocation, RenderDetails renderInfo, float maxDistance, IXAxis? xAxis = null)
Parameters
dataSourceIDataSourceThe data source
mouseLocationCoordinatesthe mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)
renderInfoRenderDetailsmaxDistancefloatThe maximum distance to search
xAxisIXAxisThe X-Axis of assigned to the datasource. If not specified, uses the bottom axis.
Returns
- DataPoint
If match found : returns a datapoint that represents the closest (X,Y) coordinate on the plot, and the index that can be used to get the values from the DataSource.
If no match found : returns None
Remarks
This is the original way to locate the nearest DataPoint from the collection, and is safe for unsorted collections (such as Scatter)
Exceptions
GetNearestXFast(IDataSource, Coordinates, RenderDetails, float, IXAxis?)
Get the nearest datapoint from the dataSource, based on the mouseLocation X location and the renderInfo
public static DataPoint GetNearestXFast(IDataSource dataSource, Coordinates mouseLocation, RenderDetails renderInfo, float maxDistance, IXAxis? xAxis = null)
Parameters
dataSourceIDataSourceThe data source
mouseLocationCoordinatesthe mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)
renderInfoRenderDetailsmaxDistancefloatThe maximum distance to search
xAxisIXAxisThe X-Axis of assigned to the datasource. If not specified, uses the bottom axis.
Returns
- DataPoint
If match found : returns a datapoint that represents the closest (X,Y) coordinate on the plot, and the index that can be used to get the values from the DataSource.
If no match found : returns None
Remarks
This is a faster way to locate the nearest DataPoint from the collection, but it requires the collection to be sorted in ascending order. ( Signal | SignalXY )
Exceptions
GetNearestXSmart(IDataSource, Coordinates, RenderDetails, float, IXAxis?)
Get the nearest datapoint from the dataSource, based on the mouseLocation X location and the renderInfo
public static DataPoint GetNearestXSmart(IDataSource dataSource, Coordinates mouseLocation, RenderDetails renderInfo, float maxDistance, IXAxis? xAxis = null)
Parameters
dataSourceIDataSourceThe data source
mouseLocationCoordinatesthe mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)
renderInfoRenderDetailsmaxDistancefloatThe maximum distance to search
xAxisIXAxisThe X-Axis of assigned to the datasource. If not specified, uses the bottom axis.
Returns
- DataPoint
If match found : returns a datapoint that represents the closest (X,Y) coordinate on the plot, and the index that can be used to get the values from the DataSource.
If no match found : returns None
Remarks
Checks IsSorted() and decides to call the Fast or Original method accordingly
Exceptions
GetRenderIndexCount(IDataSource)
Calculates the maximum count of indexes available to render in an IDataSource's collection
public static int GetRenderIndexCount(this IDataSource dataSource)
Parameters
dataSourceIDataSourceThe datasource
Returns
- int
The number of indexes that should be rendered
Remarks
Example :
MaxIndex = Xs.Length - 1
MinIndex = 0,
returns Xs.Length
GetRenderIndexRange(IDataSource)
Creates a new IndexRange
public static IndexRange GetRenderIndexRange(this IDataSource dataSource)
Parameters
dataSourceIDataSourceThe DataSource
Returns
- IndexRange
an IndexRange representing a the minimum and maximum indexes to render
IsAscending<T>(IEnumerable<T>, IComparer<T>?)
Check if a collection is in ascending order
public static bool IsAscending<T>(this IEnumerable<T> values, IComparer<T>? comparer)
Parameters
valuesIEnumerable<T>The values
comparerIComparer<T>The comparer to use. If not specified, uses Comparer{T}.Default (which is preferred)
Returns
- bool
True if the collection is ascending (and can therefore be used with BinarySearch and GetClosest). Otherwise false.
Type Parameters
Tthe type to evaluate
ScaleCoordinate(Coordinates, double, double, double, double)
Scale a coordinate by applying the offsets and the scaling factors.
public static Coordinates ScaleCoordinate(Coordinates coordinate, double xScalingFactor, double xOffset, double yScalingFactor, double yOffset)
Parameters
coordinateCoordinatesxScalingFactordoubleThe plottable's X Scaling factor
xOffsetdoubleThe plottable's X Offset
yScalingFactordoubleThe plottable's Y Scaling factor
yOffsetdoubleThe plottable's Y OFfset
Returns
- Coordinates
A new Coordinates value
ScaleXY(double, double, double)
Scale the value by applying the scalingFactor and the offset
public static double ScaleXY(double value, double scalingFactor, double offset)
Parameters
valuedoublethe value to scale
scalingFactordoublethe scaling factor to apply.
offsetdoublethe offset to apply
Returns
- double
value *
scalingFactor+offset
ScaleXY<T>(IReadOnlyList<T>, int, double, double)
Scale a value from the collection by the scalingFactor and offset
public static double ScaleXY<T>(IReadOnlyList<T> collection, int index, double scalingFactor, double offset)
Parameters
collectionIReadOnlyList<T>the collection to select a value from
indexintthe index of the value within the collection
scalingFactordoublethe scaling factor to apply.
offsetdoublethe offset to apply
Returns
- double
value *
scalingFactor+offset
Type Parameters
T
ScaleXY<T>(T, double, double)
Scale the value by applying the scalingFactor and the offset
public static double ScaleXY<T>(T value, double scalingFactor, double offset)
Parameters
valueTthe value to scale
scalingFactordoublethe scaling factor to apply.
offsetdoublethe offset to apply
Returns
- double
value *
scalingFactor+offset
Type Parameters
TThe type of value - will be converted to a double via GenericToDouble<T>(ref T)
ScaleXY<T>(T[], int, double, double)
Scale a value from the collection by the scalingFactor and offset
public static double ScaleXY<T>(T[] collection, int index, double scalingFactor, double offset)
Parameters
collectionT[]the collection to select a value from
indexintthe index of the value within the collection
scalingFactordoublethe scaling factor to apply.
offsetdoublethe offset to apply
Returns
- double
value *
scalingFactor+offset
Type Parameters
T
UnScaleCoordinate(Coordinates, RenderDetails, double, double, double, double, IXAxis?, IYAxis?)
Unscale a coordinate
public static Coordinates UnScaleCoordinate(Coordinates pixelCoordinate, RenderDetails renderInfo, double xScalingFactor = 1, double xOffset = 0, double yScalingFactor = 1, double yOffset = 0, IXAxis? xaxis = null, IYAxis? yaxis = null)
Parameters
pixelCoordinateCoordinatesthe coordinate to unscale
renderInfoRenderDetailsRenderDetails used to unscale the coordinate by the pixels/axis
xScalingFactordoubleThe plottable's X Scaling factor
xOffsetdoubleThe plottable's X Offset
yScalingFactordoubleThe plottable's Y Scaling factor
yOffsetdoubleThe plottable's Y OFfset
xaxisIXAxisThe X-Axis to used to get the pixel scaling from the
renderInfo. If not found uses the default value from the renderInfo.yaxisIYAxisThe Y-Axis to used to get the pixel scaling from the
renderInfo. If not found uses the default value from the renderInfo.
Returns
- Coordinates
A new Coordinates value
UnScaleCoordinate(Coordinates, double, double, double, double)
Unscale a coordinate
public static Coordinates UnScaleCoordinate(Coordinates coordinate, double xScalingFactor, double xOffset, double yScalingFactor, double yOffset)
Parameters
coordinateCoordinatesxScalingFactordoubleThe plottable's X Scaling factor
xOffsetdoubleThe plottable's X Offset
yScalingFactordoubleThe plottable's Y Scaling factor
yOffsetdoubleThe plottable's Y OFfset
Returns
- Coordinates
A new Coordinates value
UnscaleXY(double, double, double)
public static double UnscaleXY(double value, double scalingFactor, double offset)