Skip to content

General-purpose utilities for data processing and formatting. These are available as standalone functions or via chart.analysis.

Requirement

While many of these are standalone utilities, the recommended way to use them within a chart is to load the PluginAnalysis.

calculateStats ​

Calculate basic statistics for a dataset.

typescript
import { calculateStats } from 'velo-plot/scientific'

// Use standalone
const stats = calculateStats(yData)

// Or via chart (if PluginAnalysis is loaded)
const stats = chart.analysis.calculateStats(yData)

Returns ​

typescript
interface DataStats {
  min: number     // Minimum value
  max: number     // Maximum value
  mean: number    // Arithmetic mean
  stdDev: number  // Standard deviation
  count: number   // Number of points
}

Example ​

typescript
import { calculateStats } from 'velo-plot/scientific'

const stats = calculateStats(yData)

console.log(`Count: ${stats.count}`)
console.log(`Range: ${stats.min.toFixed(4)} to ${stats.max.toFixed(4)}`)
console.log(`Mean: ${stats.mean.toFixed(4)}`)
console.log(`Std Dev: ${stats.stdDev.toFixed(4)}`)

movingAverage ​

Apply moving average smoothing to reduce noise.

typescript
function movingAverage(
  data: Float32Array | Float64Array | number[],
  windowSize: number
): Float32Array

Parameters ​

ParameterTypeDescription
dataFloat32Array | Float64Array | number[]Input data
windowSizenumberNumber of points to average (should be odd)

Example ​

typescript
import { movingAverage } from 'velo-plot/scientific'

// Smooth noisy data with 5-point moving average
const smoothed = movingAverage(noisyData, 5)

chart.addSeries({
  id: 'smoothed',
  data: { x: xData, y: smoothed },
  style: { color: '#00ff88' },
})

downsampleLTTB ​

Reduce point count using the Largest Triangle Three Buckets (LTTB) algorithm. Preserves visual shape while dramatically reducing data size.

typescript
function downsampleLTTB(
  x: Float32Array | Float64Array,
  y: Float32Array | Float64Array,
  targetPoints: number
): { x: Float32Array; y: Float32Array }

Parameters ​

ParameterTypeDescription
xFloat32Array | Float64ArrayX values
yFloat32Array | Float64ArrayY values
targetPointsnumberDesired number of output points

Example ​

typescript
import { downsampleLTTB } from 'velo-plot/scientific'

// Reduce 10 million points to 10,000 for display
const { x: sampledX, y: sampledY } = downsampleLTTB(
  originalX,
  originalY,
  10000
)

chart.addSeries({
  id: 'data',
  data: { x: sampledX, y: sampledY },
})

When to Use ​

  • Displaying very large datasets (1M+ points)
  • Reducing data transfer size
  • Improving render performance
  • Creating thumbnails or previews

validateData ​

Check for invalid values (NaN, Infinity, -Infinity).

typescript
function validateData(
  data: Float32Array | Float64Array | number[]
): ValidationResult

Returns ​

typescript
interface ValidationResult {
  valid: boolean          // True if all values are finite
  invalidCount: number    // Number of invalid values
  firstInvalidIndex: number  // Index of first invalid (-1 if none)
}

Example ​

typescript
import { validateData } from 'velo-plot/scientific'

const result = validateData(yData)

if (!result.valid) {
  console.warn(`Found ${result.invalidCount} invalid values`)
  console.warn(`First invalid at index ${result.firstInvalidIndex}`)
}

formatWithPrefix ​

Format numbers with automatic SI prefix (k, M, G, m, µ, n, p).

typescript
function formatWithPrefix(
  value: number,
  unit: string,
  decimals?: number
): string

Parameters ​

ParameterTypeDefaultDescription
valuenumberrequiredValue to format
unitstringrequiredUnit symbol (V, A, m, s, etc.)
decimalsnumber2Decimal places

Example ​

typescript
import { formatWithPrefix } from 'velo-plot/scientific'

formatWithPrefix(0.000001, 'A')    // "1.00 µA"
formatWithPrefix(0.5, 'V')         // "500.00 mV"
formatWithPrefix(1500, 'm')        // "1.50 km"
formatWithPrefix(2.5e-9, 's')      // "2.50 ns"
formatWithPrefix(1e6, 'Hz')        // "1.00 MHz"

formatValue ​

Format a number with automatic scientific notation for extreme values.

typescript
function formatValue(value: number, decimals?: number): string

Example ​

typescript
import { formatValue } from 'velo-plot/scientific'

formatValue(123.456)      // "123.456"
formatValue(0.0001)       // "1.000e-4"
formatValue(1234567)      // "1.235e+6"

formatScientific ​

Always format in scientific notation.

typescript
function formatScientific(value: number, decimals?: number): string

Example ​

typescript
import { formatScientific } from 'velo-plot/scientific'

formatScientific(123.456)    // "1.23e+2"
formatScientific(0.001)      // "1.00e-3"

getBestPrefix ​

Get the optimal SI prefix for a value.

typescript
function getBestPrefix(value: number): PrefixInfo

Returns ​

typescript
interface PrefixInfo {
  symbol: string   // 'p' | 'n' | 'µ' | 'm' | '' | 'k' | 'M' | 'G'
  factor: number   // Multiplication factor (e.g., 1e-6 for µ)
}

Example ​

typescript
import { getBestPrefix } from 'velo-plot/scientific'

const prefix = getBestPrefix(0.000001)
// { symbol: 'µ', factor: 1e-6 }

const scaled = 0.000001 / prefix.factor  // 1
console.log(`${scaled} ${prefix.symbol}A`)  // "1 µA"

integrate ​

Numerical integration using the trapezoidal rule. Essential for calculating areas under curves (e.g., total charge, energy).

typescript
function integrate(
  x: number[] | Float32Array,
  y: number[] | Float32Array,
  xMin?: number,
  xMax?: number
): number

Parameters ​

NameTypeDescription
xnumber[] | Float32ArrayX values (must be sorted).
ynumber[] | Float32ArrayY values.
xMinnumberOptional: start of integration range (in X units).
xMaxnumberOptional: end of integration range (in X units).

Example ​

typescript
import { integrate } from 'velo-plot/scientific'

const area = integrate(xData, yData, 0.2, 0.8)
// Or via chart: chart.analysis.integrate(...)
console.log(`Peak Area: ${area.toFixed(4)}`)

subtractBaseline ​

Subtract a linear baseline from a dataset. Useful for removing drift or constant backgrounds.

typescript
function subtractBaseline(
  x: number[] | Float32Array,
  y: number[] | Float32Array,
  x1: number,
  x2: number
): Float32Array

Parameters ​

NameTypeDescription
xnumber[] | Float32ArrayX values.
ynumber[] | Float32ArrayY values.
x1numberX coordinate of the first baseline point.
x2numberX coordinate of the second baseline point.

Example ​

typescript
import { subtractBaseline } from 'velo-plot/scientific'

// Subtract background using points at x=0.1 and x=0.9
const correctedY = subtractBaseline(xData, yData, 0.1, 0.9)
// Or via chart: chart.analysis.subtractBaseline(...)

chart.addSeries({
  id: 'corrected',
  data: { x: xData, y: correctedY }
})

Known limitations ​

  • Bundle entry — pick velo-plot, velo-plot/trading, velo-plot/scientific, or velo-plot/full. See Bundle Architecture.
  • WebGPU renderer (renderer: 'webgpu') requires an extended bundle entry and is experimental — use WebGL (default) for production.
  • SVG — exportSVG() and renderer: 'svg' require an extended bundle. LaTeX labels and contour overlays are partial; 3D is not supported in SVG. See SVG examples.
  • See Plugin status registry for plugin maturity (complete, partial, experimental).

Released under the MIT License.