8.19.47. Plot
Four read-only sample plots. Two array-form widgets take a
per-frame array<float> and copy it synchronously; two
lambda-form widgets call back once per sample, skipping the
backing array entirely.
plot_lines(IDENT, "title", values, scale_min, scale_max, size)
plot_histogram(IDENT, "title", values, scale_min, scale_max, size)
plot_lines_getter(IDENT, "title", count,
@(idx : int) : float => ...,
overlay, scale_min, scale_max, size)
plot_histogram_getter(IDENT, "title", count,
@(idx : int) : float => ...,
overlay, scale_min, scale_max, size)
All four share PlotState — title + samples round-trip into the
snapshot so visual regression tests can diff the data.
Source: modules/dasImgui/examples/tutorial/plot.das.
8.19.47.1. Walkthrough
1options gen2
2options _comment_hygiene = true
3options gc
4
5require math
6require imgui
7require imgui_app
8require opengl/opengl_boost
9require live/glfw_live
10require live/live_api
11require live/live_commands
12require live/live_vars
13require live_host
14require imgui/imgui_live
15require imgui/imgui_boost_runtime
16require imgui/imgui_boost_v2
17require imgui/imgui_widgets_builtin
18require imgui/imgui_containers_builtin
19require imgui/imgui_visual_aids
20
21// =============================================================================
22// TUTORIAL: plot — four read-only sample-plot widgets.
23//
24// plot_lines(IDENT, "title", values, scale_min, scale_max, size)
25// plot_histogram(IDENT, "title", values, scale_min, scale_max, size)
26// Array form — pass a per-frame array<float>. ImGui copies it
27// synchronously. Pass scale_min = scale_max = FLT_MAX to auto-fit
28// bounds to the data; explicit bounds avoid the per-frame jitter.
29//
30// plot_lines_getter(IDENT, "title", count, getter, overlay,
31// scale_min, scale_max, size)
32// plot_histogram_getter(IDENT, "title", count, getter, overlay,
33// scale_min, scale_max, size)
34// Lambda form — callback (idx : int) : float fires once per sample
35// synchronously inside the C call. Use for synthesized signals or
36// virtualized buffers (avoids building a backing array).
37//
38// All four share PlotState — title + sample telemetry round-trips into
39// the snapshot payload for visual regression testing.
40//
41// STANDALONE: daslang.exe modules/dasImgui/examples/tutorial/plot.das
42// LIVE: daslang-live modules/dasImgui/examples/tutorial/plot.das
43// =============================================================================
44
45let SAMPLES = 64
46let FLT_MAX = 3.40282347e38f
47
48var private g_phase : float = 0.0f
49
50def fill_sine(var arr : array<float>; phase : float; n : int) {
51 arr |> resize(n)
52 for (i in range(n)) {
53 arr[i] = sin(float(i) * 0.2f + phase)
54 }
55}
56
57def fill_bars(var arr : array<float>) {
58 arr |> resize(8)
59 for (i in range(8)) {
60 arr[i] = float(i) * 0.15f + 0.1f
61 }
62}
63
64[export]
65def init() {
66 live_create_window("dasImgui plot tutorial", 800, 720)
67 live_imgui_init(live_window)
68 let io & = unsafe(GetIO())
69 GetStyle().FontScaleMain = 1.4
70}
71
72[export]
73def update() {
74 if (!live_begin_frame()) return
75 begin_frame()
76
77 ImGui_ImplGlfw_NewFrame()
78 apply_synth_io_override()
79 NewFrame()
80
81 g_phase += 0.05f
82 var sine_data : array<float>
83 fill_sine(sine_data, g_phase, SAMPLES)
84 var bar_data : array<float>
85 fill_bars(bar_data)
86
87 SetNextWindowPos(ImVec2(20.0f, 20.0f), ImGuiCond.Always)
88 SetNextWindowSize(ImVec2(760.0f, 680.0f), ImGuiCond.Always)
89 window(PL_WIN, (text = "plot tutorial", closable = false,
90 flags = ImGuiWindowFlags.None)) {
91
92 text("Read-only sample plots - PlotState records title + samples each frame.")
93 text(PL_HINT, (text = "Array form (lines / histogram) vs lambda form (lines_getter / histogram_getter)."))
94 separator()
95
96 // ---- Stage 1: plot_lines (array) — sine wave with explicit bounds ----
97 plot_lines(PL_LINES, "Sine wave", sine_data, -1.2f, 1.2f, float2(0.0f, 80.0f))
98 spacing()
99
100 // ---- Stage 2: plot_histogram (array) — fixed bars ----
101 plot_histogram(PL_HIST, "Histogram", bar_data, 0.0f, 1.5f, float2(0.0f, 80.0f))
102 spacing()
103
104 // ---- Stage 3: plot_lines_getter (lambda) — no backing array ----
105 plot_lines_getter(PL_LINES_G, "sin(i*0.1)", SAMPLES,
106 @(idx : int) : float => sin(float(idx) * 0.1f + g_phase),
107 "", -1.2f, 1.2f, float2(0.0f, 80.0f))
108 spacing()
109
110 // ---- Stage 4: plot_histogram_getter (lambda) ----
111 plot_histogram_getter(PL_HIST_G, "abs(sin(i*0.1))", SAMPLES,
112 @(idx : int) : float => abs(sin(float(idx) * 0.1f + g_phase)),
113 "", 0.0f, 1.2f, float2(0.0f, 80.0f))
114 }
115
116 end_of_frame()
117 Render()
118 var w, h : int
119 live_get_framebuffer_size(w, h)
120 glViewport(0, 0, w, h)
121 glClearColor(0.10f, 0.10f, 0.12f, 1.0f)
122 glClear(GL_COLOR_BUFFER_BIT)
123 live_imgui_render()
124
125 live_end_frame()
126}
127
128[export]
129def shutdown() {
130 live_imgui_shutdown()
131 live_destroy_window()
132}
133
134[export]
135def main() {
136 init()
137 while (!exit_requested()) {
138 update()
139 maybe_collect_gc()
140 }
141 shutdown()
142}
8.19.47.1.1. Requires
Already in the baseline boost layer:
imgui/imgui_widgets_builtin— both array and lambda rails.imgui/imgui_boost_runtime—PlotState.
8.19.47.1.2. Array form vs lambda form
Array (
plot_lines/plot_histogram) — pass avalues : array<float>. ImGui copies element-by-element inside the C call. Use when you already have a backing buffer (telemetry ring, captured samples, table column).Lambda (
*_getter) — passvalues_countand agetter : lambda<(idx : int) : float>. The callback fires once per sample, synchronously inside the C call. Use for synthesized signals (sine, noise, generated frequency response) or virtualized buffers where building an array would waste work.
Both forms are O(n) per frame — the choice is about whether you want to allocate the array or generate on the fly.
8.19.47.1.3. Auto-fit vs explicit bounds
scale_min and scale_max default to FLT_MAX (3.4e38), which
ImGui treats as “auto-fit to the data this frame.” That’s tempting,
but the bounds change every frame — the plot stretches/compresses
as the data range shifts, which reads as jitter:
// Auto-fit — bounds drift per frame:
plot_lines(LATENCY, "ms", samples) // FLT_MAX defaults
// Explicit bounds — calm plot, comparable across frames:
plot_lines(LATENCY, "ms", samples,
0.0f, 200.0f, float2(0.0f, 80.0f)) // 0..200 ms y-axis
Use auto-fit only when you don’t know the data range and don’t care about per-frame stability. For anything you’ll watch over time, explicit bounds.
8.19.47.1.4. Size
The last arg is the plot bbox in pixels — float2(width, height).
width = 0.0f lets ImGui auto-size to the available content width;
height = 0.0f falls back to a small default. Pass 80.0f or
120.0f for the height to keep two plots visible side-by-side.
8.19.47.1.5. The overlay arg
Lambda-form widgets take an extra overlay_text : string arg before
scale_min. Pass "" for no overlay, or a status string that
prints centered on top of the plot:
plot_lines_getter(FRAMETIME, "ms/frame", 60,
@(idx : int) : float => sample_at(idx),
"avg = {avg_ms:.2f}",
0.0f, 33.0f, float2(0.0f, 80.0f))
Useful for the “rolling average” or “max” annotation under the line.
8.19.47.1.6. Driving from outside
PlotState exposes title + samples in the snapshot payload —
no imgui_force_set channel (the samples are caller-pushed). Snapshot
probes are the right tool:
curl -X POST -d '{"name":"imgui_snapshot"}' localhost:9090/command \
| jq '.globals."PL_WIN/PL_LINES".payload'
The samples array gives visual-regression tests something to diff without screenshotting.
See also
Full source: modules/dasImgui/examples/tutorial/plot.das
Features-side demos: modules/dasImgui/examples/features/plot_widgets.das
(array form), modules/dasImgui/examples/features/plot_getter.das (lambda form).
Boost macros — the macro layer.