Table of Contents

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 TList
index int

The starting index of the range to search.

length int

The length of the range to search.

value T

The 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 specified array, if value is found; otherwise, a negative number. If value is not found and value is less than one or more elements in array, the negative number returned is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than all elements in array, 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-sorted array, the return value can be incorrect and a negative number could be returned, even if value is present in array.

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 of array.-or-length is less than zero.

ArgumentException

index and length do not specify a valid range in array.-or-comparer is null, and value is of a type that is not compatible with the elements of array.

InvalidOperationException

comparer is null, and T 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 Coordinates

The value to search for in the collection

indexRange IndexRange

Provides 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 Coordinates

The value to search for in the collection

indexRange IndexRange

Provides 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 double

The value to search for in the collection

indexRange IndexRange

Provides 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 double

The value to search for in the collection

indexRange IndexRange

Provides 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 TList

The collection to search. Collection must be in ascending order.

value TValue

The value to search for in the collection

indexRange IndexRange

Provides 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 IDataSource

The data source

mouseLocation Coordinates

the mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)

renderInfo RenderDetails

LastRender

maxDistance float

The maximum distance to search

xAxis IXAxis

The X-Axis of assigned to the datasource. If not specified, uses the bottom axis.

yAxis IYAxis

The 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

ArgumentNullException

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 IDataSource

The data source

mouseLocation Coordinates

the mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)

renderInfo RenderDetails

LastRender

maxDistance float

The maximum distance to search

xAxis IXAxis

The X-Axis of assigned to the datasource. If not specified, uses the bottom axis.

yAxis IYAxis

The 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

ArgumentNullException

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 IDataSource

The data source

mouseLocation Coordinates

the mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)

renderInfo RenderDetails

LastRender

maxDistance float

The maximum distance to search

xAxis IXAxis

The X-Axis of assigned to the datasource. If not specified, uses the bottom axis.

yAxis IYAxis

The 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

ArgumentNullException

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 IDataSource

The data source

mouseLocation Coordinates

the mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)

renderInfo RenderDetails

LastRender

maxDistance float

The maximum distance to search

xAxis IXAxis

The 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

ArgumentNullException

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 IDataSource

The data source

mouseLocation Coordinates

the mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)

renderInfo RenderDetails

LastRender

maxDistance float

The maximum distance to search

xAxis IXAxis

The 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

ArgumentNullException

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 IDataSource

The data source

mouseLocation Coordinates

the mouse coordinates from the plot. GetCoordinates(Pixel, IXAxis?, IYAxis?)

renderInfo RenderDetails

LastRender

maxDistance float

The maximum distance to search

xAxis IXAxis

The 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

ArgumentNullException

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 IDataSource

The 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 IDataSource

The 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 Coordinates
xScalingFactor double

The plottable's X Scaling factor

xOffset double

The plottable's X Offset

yScalingFactor double

The plottable's Y Scaling factor

yOffset double

The 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 double

the value to scale

scalingFactor double

the scaling factor to apply.

offset double

the 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 int

the index of the value within the collection

scalingFactor double

the scaling factor to apply.

offset double

the 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 T

the value to scale

scalingFactor double

the scaling factor to apply.

offset double

the 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 int

the index of the value within the collection

scalingFactor double

the scaling factor to apply.

offset double

the 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 Coordinates

the coordinate to unscale

renderInfo RenderDetails

RenderDetails used to unscale the coordinate by the pixels/axis

xScalingFactor double

The plottable's X Scaling factor

xOffset double

The plottable's X Offset

yScalingFactor double

The plottable's Y Scaling factor

yOffset double

The plottable's Y OFfset

xaxis IXAxis

The X-Axis to used to get the pixel scaling from the renderInfo. If not found uses the default value from the renderInfo.

yaxis IYAxis

The 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 Coordinates
xScalingFactor double

The plottable's X Scaling factor

xOffset double

The plottable's X Offset

yScalingFactor double

The plottable's Y Scaling factor

yOffset double

The plottable's Y OFfset

Returns

Coordinates

A new Coordinates value

UnscaleXY(double, double, double)

public static double UnscaleXY(double value, double scalingFactor, double offset)

Parameters

value double
scalingFactor double
offset double

Returns

double