Guide

Canali matematici

Canali formula che si comportano come colonne, un’anteprima nell’editor e la persistenza nel tuo backend.

Cos’è un canale

Un canale matematico è una formula con nome su colonne esistenti. Compare in state.numericColumns e può essere tracciato da qualsiasi grafico. Viene valutato per dataset, quindi le funzioni a finestra non attraversano mai il confine di un dataset.

FamigliaFunzioni
Aritmetica+ − × ÷, abs, sqrt, exp, log, trigonometria, min, max, round, mod
Condizionaliif, nullif, clamp
Finestraderivative, integral, movavg, lag, lead
Filtrilowpass e highpass di Butterworth

FUNCTION_REFERENCE elenca ogni funzione con la sua firma e la sua descrizione, pronta per il pannello di aiuto di un editor.

Creare e modificare

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()

Anteprima nell’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

Memorizzare i canali

Senza uno store, i canali vivono in memoria per la sessione. Con un MathChannelStoreAdapter, il motore applica ogni modifica in locale e chiama il tuo store; tu rimandi l’elenco autorevole con sync ogni volta che 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 rifiutato viene segnalato come evento error (la chiamata al motore è già tornata). importMany e importFile attendono lo store e vengono rifiutate se lo store lo fa.