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
sortedList
TListindex
intThe starting index of the range to search.
length
intThe length of the range to search.
value
TThe object to search for.
comparer
IComparer<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
value
in the specifiedarray
, ifvalue
is found; otherwise, a negative number. Ifvalue
is not found andvalue
is 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
. Ifvalue
is not found andvalue
is 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 ifvalue
is present inarray
.
Type Parameters
T
The type of the elements of the array.
TList
Exceptions
- ArgumentNullException
array
is null.- ArgumentOutOfRangeException
index
is less than the lower bound ofarray
.-or-length
is less than zero.- ArgumentException
index
andlength
do not specify a valid range inarray
.-or-comparer
is null, andvalue
is of a type that is not compatible with the elements ofarray
.- InvalidOperationException
comparer
is null, andT
does 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
sortedList
Coordinates[]The collection to search. Collection must be in ascending order.
value
CoordinatesThe value to search for in the collection
indexRange
IndexRangeProvides 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
sortedList
List<Coordinates>The collection to search. Collection must be in ascending order.
value
CoordinatesThe value to search for in the collection
indexRange
IndexRangeProvides 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
sortedList
List<double>The collection to search. Collection must be in ascending order.
value
doubleThe value to search for in the collection
indexRange
IndexRangeProvides 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
sortedList
double[]The collection to search. Collection must be in ascending order.
value
doubleThe value to search for in the collection
indexRange
IndexRangeProvides 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
sortedList
TListThe collection to search. Collection must be in ascending order.
value
TValueThe value to search for in the collection
indexRange
IndexRangeProvides details about the range of indexes to search
comparer
IComparer<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
TValue
The type of values in the collection
TList
Must 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
dataSource
IDataSourceThe data source
mouseLocation
Coordinatesthe mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)
renderInfo
RenderDetailsmaxDistance
floatThe maximum distance to search
xAxis
IXAxisThe X-Axis of assigned to the datasource. If not specified, uses the bottom axis.
yAxis
IYAxisThe 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
dataSource
IDataSourceThe data source
mouseLocation
Coordinatesthe mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)
renderInfo
RenderDetailsmaxDistance
floatThe maximum distance to search
xAxis
IXAxisThe X-Axis of assigned to the datasource. If not specified, uses the bottom axis.
yAxis
IYAxisThe 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
dataSource
IDataSourceThe data source
mouseLocation
Coordinatesthe mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)
renderInfo
RenderDetailsmaxDistance
floatThe maximum distance to search
xAxis
IXAxisThe X-Axis of assigned to the datasource. If not specified, uses the bottom axis.
yAxis
IYAxisThe 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
dataSource
IDataSourceThe data source
mouseLocation
Coordinatesthe mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)
renderInfo
RenderDetailsmaxDistance
floatThe maximum distance to search
xAxis
IXAxisThe 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
dataSource
IDataSourceThe data source
mouseLocation
Coordinatesthe mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)
renderInfo
RenderDetailsmaxDistance
floatThe maximum distance to search
xAxis
IXAxisThe 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
dataSource
IDataSourceThe data source
mouseLocation
Coordinatesthe mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)
renderInfo
RenderDetailsmaxDistance
floatThe maximum distance to search
xAxis
IXAxisThe 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
dataSource
IDataSourceThe 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
dataSource
IDataSourceThe 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
values
IEnumerable<T>The values
comparer
IComparer<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
T
the 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
coordinate
CoordinatesxScalingFactor
doubleThe plottable's X Scaling factor
xOffset
doubleThe plottable's X Offset
yScalingFactor
doubleThe plottable's Y Scaling factor
yOffset
doubleThe 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
value
doublethe value to scale
scalingFactor
doublethe scaling factor to apply.
offset
doublethe 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
collection
IReadOnlyList<T>the collection to select a value from
index
intthe index of the value within the collection
scalingFactor
doublethe scaling factor to apply.
offset
doublethe 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
value
Tthe value to scale
scalingFactor
doublethe scaling factor to apply.
offset
doublethe offset to apply
Returns
- double
value *
scalingFactor
+offset
Type Parameters
T
The 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
collection
T[]the collection to select a value from
index
intthe index of the value within the collection
scalingFactor
doublethe scaling factor to apply.
offset
doublethe 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
pixelCoordinate
Coordinatesthe coordinate to unscale
renderInfo
RenderDetailsRenderDetails used to unscale the coordinate by the pixels/axis
xScalingFactor
doubleThe plottable's X Scaling factor
xOffset
doubleThe plottable's X Offset
yScalingFactor
doubleThe plottable's Y Scaling factor
yOffset
doubleThe plottable's Y OFfset
xaxis
IXAxisThe X-Axis to used to get the pixel scaling from the
renderInfo
. If not found uses the default value from the renderInfo.yaxis
IYAxisThe 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
coordinate
CoordinatesxScalingFactor
doubleThe plottable's X Scaling factor
xOffset
doubleThe plottable's X Offset
yScalingFactor
doubleThe plottable's Y Scaling factor
yOffset
doubleThe plottable's Y OFfset
Returns
- Coordinates
A new Coordinates value
UnscaleXY(double, double, double)
public static double UnscaleXY(double value, double scalingFactor, double offset)