Gallery

Live telemetry

Four traces scrolling at 60 samples a second above a 100,000-row data grid that jumps to any row without slowing down.

Live telemetry: a still of the demo

Run it on your machine:

go run github.com/amken3d/immygo/examples/live-data@latest

Source

live-data/livedata/livedata.go
// Package livedata is the live-data example's app: a telemetry dashboard
// with four traces scrolling at 60 samples a second over a data grid of
// 100,000 rows. It is a package so that other programs (the README GIF
// recorder, the immygo.app demo bundle) can run it; main.go runs it.
package livedata
import (
"fmt"
"math"
"strconv"
"time"
"github.com/amken3d/immygo/theme"
"github.com/amken3d/immygo/ui"
)
// Rows is the number of rows in the grid.
const Rows = 100_000
const samples = 600
var colors = []string{"0078d4", "107c10", "d83b01", "8764b8"}
// signal is a deterministic test signal in [-1, 1].
func signal(ch int, t float64) float32 {
return float32(math.Sin(t*0.05*float64(ch+1))*0.6 + math.Sin(t*0.013*float64(ch+3))*0.4)
}
// Main runs the dashboard in a window. It blocks until the window closes.
func Main() {
build, th := New()
ui.Run("Live telemetry", build, ui.Size(960, 540), ui.Theme(th), ui.ContinuousRedraw())
}
// New builds the dashboard. The traces move with wall-clock time, so the
// caller must redraw continuously (Main uses ui.ContinuousRedraw).
func New() (build func() ui.View, th *theme.Theme) {
rows := make([][]string, Rows)
for r := range rows {
status := "OK"
switch {
case r%17 == 0:
status = "FAULT"
case r%5 == 0:
status = "WARN"
}
rows[r] = []string{
commas(r),
fmt.Sprintf("axis-%d", r%7),
fmt.Sprintf("%.3f mm", float64(r)*0.125),
fmt.Sprintf("%.2f mm/s", float64(r%997)*0.5),
fmt.Sprintf("%.1f A", float64(r%53)*0.1),
status,
}
}
grid := ui.DataGrid(
ui.Col("ID"), ui.Col("Axis"), ui.Col("Position"),
ui.Col("Velocity"), ui.Col("Current"), ui.Col("Status"),
).Striped(true).Rows(rows)
top := ui.Button("Top").Outline().OnClick(func() { grid.ScrollToRow(0) })
mid := ui.Button("Row 50,000").Outline().OnClick(func() { grid.ScrollToRow(50_000) })
end := ui.Button("End").Outline().OnClick(func() { grid.ScrollToRow(Rows - 1) })
start := time.Now()
chart := func(ch int, t float64) ui.View {
return ui.Card(ui.VStack(
ui.Text(fmt.Sprintf("Channel %d", ch+1)).Caption(),
ui.Polyline(samples, func(k int) (x, y float32) {
v := signal(ch, t-float64(samples-1-k))
return float32(k) / float32(samples-1), 0.5 - 0.45*v
}).Color(ui.Hex(colors[ch])).StrokeWidth(1.5).Height(64),
).Spacing(4))
}
build = func() ui.View {
t := time.Since(start).Seconds() * 60 // samples
charts := make([]ui.View, len(colors))
for ch := range charts {
charts[ch] = ui.Flex(1, chart(ch, t))
}
first, n := grid.VisibleRows()
status := fmt.Sprintf("Rows %s–%s of %s", commas(first+1), commas(min(first+n, Rows)), commas(Rows))
return ui.VStack(
ui.HStack(
ui.Text("Live telemetry").Title(),
ui.Spacer(),
ui.Badge(commas(Rows)+" rows"),
ui.Badge(fmt.Sprintf("%d × %d samples", len(colors), samples)).Secondary(),
).Spacing(8).Center(),
ui.HStack(charts...).Spacing(12),
ui.Flex(1, grid),
ui.HStack(ui.Text(status).Small(), ui.Spacer(), top, mid, end).Spacing(8).Center(),
).Spacing(12).Padding(16)
}
return build, theme.FluentLight()
}
// commas formats n with thousands separators.
func commas(n int) string {
s := strconv.Itoa(n)
for i := len(s) - 3; i > 0; i -= 3 {
s = s[:i] + "," + s[i:]
}
return s
}