Guías

Canales matemáticos

Canales de fórmula que se comportan como columnas, una vista previa en el editor y persistencia en tu backend.

Qué es un canal

Un canal matemático es una fórmula con nombre sobre columnas existentes. Aparece en state.numericColumns y cualquier gráfico puede representarlo. Se evalúa por conjunto de datos, así que las funciones de ventana nunca cruzan el límite de un conjunto de datos.

FamiliaFunciones
Aritmética+ − × ÷, abs, sqrt, exp, log, trigonometría, min, max, round, mod
Condicionalif, nullif, clamp
Ventanaderivative, integral, movavg, lag, lead
Filtroslowpass y highpass de Butterworth

FUNCTION_REFERENCE lista todas las funciones con su firma y descripción, lista para el panel de ayuda de un editor.

Crear y editar

TypeScript
const ch = engine.mathChannels.create({ name: 'speed_kmh', formula: 'speed * 3.6', units: 'km/h' })
engine.mathChannels.update(ch.id, { formula: 'lowpass(speed, 5) * 3.6' })
engine.mathChannels.update(ch.id, { enabled: false })
engine.mathChannels.remove(ch.id)

await engine.mathChannels.importMany(channelsFromYourApi)
const { channels, errors } = await engine.mathChannels.importFile(file)   // a JSON export
const blob = engine.mathChannels.exportBlob()

Vista previa del editor

TypeScript
import { newMathChannel, parseFormula } from '@kymoscope/sdk'

const parsed = parseFormula(input.value)
if (!parsed.ok) showError(parsed.message)

const draft = newMathChannel({ name: 'draft', formula: input.value })
const result = engine.mathChannels.preview(draft)   // null when nothing is plotted
// result.status: 'ok' | 'missing-inputs' | 'parse-error' | 'runtime-error' | 'disabled' | 'references-channel'
// result.values: (number | null)[], result.missingCols, result.message

Almacenar canales

Sin almacén, los canales viven en memoria durante la sesión. Con un MathChannelStoreAdapter, el motor aplica cada cambio localmente y llama a tu almacén; tú le devuelves la lista autorizada con sync cada vez que cambia.

TypeScript
const engine = createEngine({
  mathChannels: {
    create: (channel) => api.createChannel(channel),
    update: (id, patch) => api.updateChannel(id, patch),
    remove: (id) => api.deleteChannel(id),
    importMany: (channels) => api.importChannels(channels),
  },
})

engine.mathChannels.sync(await api.listChannels())
api.onChannelsChanged((list) => engine.mathChannels.sync(list))

Un create, update o remove rechazado se notifica como un evento error (la llamada al motor ya ha devuelto). importMany e importFile esperan al almacén y se rechazan si este lo hace.