Per iniziare
Guida rapida
Crea un engine, aggiungi dati e monta un grafico, con uno script tag o con npm e React.
Senza una chiave di licenza tutto funziona e ogni grafico mostra un piccolo badge “Powered by Kymoscope”; aggiungi la tua chiave quando ne hai una, come licenseKey, oppure passa license.get per recuperare chiavi nuove dal tuo backend (vedi Rinnovo automatico).
Tag script
Servi kymoscope-embed.js, la sua cartella assets/ e styles/charts.css (vedi Installazione). React è incluso nello script; la tua pagina non ne ha bisogno.
<link rel="stylesheet" href="/vendor/kymoscope/styles/charts.css" />
<div id="chart" style="height: 360px"></div>
<script src="/vendor/kymoscope/kymoscope-embed.js"></script>
<script>
const engine = Kymoscope.createEngine({ licenseKey: window.KYMOSCOPE_LICENSE_KEY })
// 1. Data: numeric columns of equal length.
const n = 2000
const t = new Float64Array(n), rpm = new Float64Array(n)
for (let i = 0; i < n; i++) {
t[i] = i * 0.05
rpm[i] = 6000 + 2500 * Math.sin(t[i] / 1.3)
}
engine.datasets.addColumns({ name: 'Run 42', columns: { t, rpm }, units: { t: 's', rpm: '1/min' } })
// 2. A chart model in the active worksheet, with one series.
const chartId = engine.charts.add('multiband', { title: 'RPM' })
engine.charts.addSeries(chartId, 'rpm')
// 3. A view of that chart in your element. It fills the element.
const view = engine.charts.mount(chartId, document.getElementById('chart'))
</script>La prima colonna numerica (t) è diventata l’asse X del foglio di lavoro. Il grafico non ha barra degli strumenti né menu: la rotellina del mouse lo zooma, e tutto il resto (reimpostare lo zoom, aggiungere una serie, esportare) viene dai tuoi pulsanti che chiamano il motore.
npm e React
import { useEffect, useState } from 'react'
import { createEngine, type KymoscopeEngine } from '@kymoscope/sdk'
import { Chart, KymoscopeProvider, useEngineState } from '@kymoscope/sdk/react'
import '@kymoscope/sdk/styles/charts.css'
export function Analysis({ run }: { run: { name: string; t: number[]; rpm: number[] } }) {
const [engine, setEngine] = useState<KymoscopeEngine | null>(null)
useEffect(() => {
const e = createEngine({ licenseKey: import.meta.env.VITE_KYMOSCOPE_LICENSE_KEY })
e.datasets.addColumns({ name: run.name, columns: { t: run.t, rpm: run.rpm } })
void e.charts.addSeries(e.charts.add('multiband'), 'rpm')
setEngine(e)
return () => e.dispose()
}, [run])
if (!engine) return null
return (
<KymoscopeProvider engine={engine}>
<Charts />
</KymoscopeProvider>
)
}
function Charts() {
const charts = useEngineState((s) => s.worksheets.find((w) => w.id === s.activeWorksheetId)!.charts)
return charts.map((c) => <Chart key={c.id} id={c.id} style={{ height: 320 }} />)
}Crea il motore in un effect, così il secondo montaggio di StrictMode ne ottiene uno nuovo. Vedi Binding React.
Invia un segnale live
Uno stream locale è alimentato dalla tua pagina: nessun server e nessuna cifratura coinvolti. Viene tracciato appena si apre.
const live = engine.live.openLocal('Rig A')
let t = 0
setInterval(() => {
t += 1 / 30
live.push({ t, signals: [{ name: 'load', unit: 'kN', value: 5 + 3 * Math.sin(t) }] })
}, 1000 / 30)Per i dati che arrivano dai dispositivi, vedi Stream live.
Salva ciò che costruiscono gli utenti
// Worksheets and chart settings as JSON (datasets are not included).
engine.subscribe(debounce(() => {
const { json } = engine.io.exportWorkspace()
fetch('/api/workspaces/42', { method: 'PUT', body: JSON.stringify(json) })
}, 500))
// Later, in a new engine:
await engine.io.importWorkspace(await (await fetch('/api/workspaces/42')).json())subscribe scatta una volta per ogni cambiamento di stato effettivo; applica tu il debounce. Vedi Importazione ed esportazione.