8.18.1.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.18.1.47.1. Walkthrough

  1options gen2
  2options _comment_hygiene = true
  3
  4require math
  5require imgui
  6require imgui_app
  7require opengl/opengl_boost
  8require live/glfw_live
  9require live/live_api
 10require live/live_commands
 11require live/live_vars
 12require live_host
 13require imgui/imgui_live
 14require imgui/imgui_boost_runtime
 15require imgui/imgui_boost_v2
 16require imgui/imgui_widgets_builtin
 17require imgui/imgui_containers_builtin
 18require imgui/imgui_visual_aids
 19
 20// =============================================================================
 21// TUTORIAL: plot — four read-only sample-plot widgets.
 22//
 23//   plot_lines(IDENT, "title", values, scale_min, scale_max, size)
 24//   plot_histogram(IDENT, "title", values, scale_min, scale_max, size)
 25//     Array form — pass a per-frame array<float>. ImGui copies it
 26//     synchronously. Pass scale_min = scale_max = FLT_MAX to auto-fit
 27//     bounds to the data; explicit bounds avoid the per-frame jitter.
 28//
 29//   plot_lines_getter(IDENT, "title", count, getter, overlay,
 30//                     scale_min, scale_max, size)
 31//   plot_histogram_getter(IDENT, "title", count, getter, overlay,
 32//                         scale_min, scale_max, size)
 33//     Lambda form — callback (idx : int) : float fires once per sample
 34//     synchronously inside the C call. Use for synthesized signals or
 35//     virtualized buffers (avoids building a backing array).
 36//
 37// All four share PlotState — title + sample telemetry round-trips into
 38// the snapshot payload for visual regression testing.
 39//
 40// STANDALONE: daslang.exe modules/dasImgui/examples/tutorial/plot.das
 41// LIVE:       daslang-live modules/dasImgui/examples/tutorial/plot.das
 42// =============================================================================
 43
 44let SAMPLES = 64
 45let FLT_MAX = 3.40282347e38f
 46
 47var private g_phase : float = 0.0f
 48
 49def fill_sine(var arr : array<float>; phase : float; n : int) {
 50    arr |> resize(n)
 51    for (i in range(n)) {
 52        arr[i] = sin(float(i) * 0.2f + phase)
 53    }
 54}
 55
 56def fill_bars(var arr : array<float>) {
 57    arr |> resize(8)
 58    for (i in range(8)) {
 59        arr[i] = float(i) * 0.15f + 0.1f
 60    }
 61}
 62
 63[export]
 64def init() {
 65    live_create_window("dasImgui plot tutorial", 800, 720)
 66    live_imgui_init(live_window)
 67    let io & = unsafe(GetIO())
 68    GetStyle().FontScaleMain = 1.4
 69}
 70
 71[export]
 72def update() {
 73    if (!live_begin_frame()) return
 74    begin_frame()
 75
 76    ImGui_ImplGlfw_NewFrame()
 77    apply_synth_io_override()
 78    NewFrame()
 79
 80    g_phase += 0.05f
 81    var sine_data : array<float>
 82    fill_sine(sine_data, g_phase, SAMPLES)
 83    var bar_data : array<float>
 84    fill_bars(bar_data)
 85
 86    SetNextWindowPos(ImVec2(20.0f, 20.0f), ImGuiCond.Always)
 87    SetNextWindowSize(ImVec2(760.0f, 680.0f), ImGuiCond.Always)
 88    window(PL_WIN, (text = "plot tutorial", closable = false,
 89                    flags = ImGuiWindowFlags.None)) {
 90
 91        text("Read-only sample plots - PlotState records title + samples each frame.")
 92        text(PL_HINT, (text = "Array form (lines / histogram) vs lambda form (lines_getter / histogram_getter)."))
 93        separator()
 94
 95        // ---- Stage 1: plot_lines (array) — sine wave with explicit bounds ----
 96        plot_lines(PL_LINES, "Sine wave", sine_data, -1.2f, 1.2f, float2(0.0f, 80.0f))
 97        spacing()
 98
 99        // ---- Stage 2: plot_histogram (array) — fixed bars ----
100        plot_histogram(PL_HIST, "Histogram", bar_data, 0.0f, 1.5f, float2(0.0f, 80.0f))
101        spacing()
102
103        // ---- Stage 3: plot_lines_getter (lambda) — no backing array ----
104        plot_lines_getter(PL_LINES_G, "sin(i*0.1)", SAMPLES,
105                          @(idx : int) : float => sin(float(idx) * 0.1f + g_phase),
106                          "", -1.2f, 1.2f, float2(0.0f, 80.0f))
107        spacing()
108
109        // ---- Stage 4: plot_histogram_getter (lambda) ----
110        plot_histogram_getter(PL_HIST_G, "abs(sin(i*0.1))", SAMPLES,
111                              @(idx : int) : float => abs(sin(float(idx) * 0.1f + g_phase)),
112                              "", 0.0f, 1.2f, float2(0.0f, 80.0f))
113    }
114
115    end_of_frame()
116    Render()
117    var w, h : int
118    live_get_framebuffer_size(w, h)
119    glViewport(0, 0, w, h)
120    glClearColor(0.10f, 0.10f, 0.12f, 1.0f)
121    glClear(GL_COLOR_BUFFER_BIT)
122    live_imgui_render()
123
124    live_end_frame()
125}
126
127[export]
128def shutdown() {
129    live_imgui_shutdown()
130    live_destroy_window()
131}
132
133[export]
134def main() {
135    init()
136    while (!exit_requested()) {
137        update()
138    }
139    shutdown()
140}

8.18.1.47.1.1. Requires

Already in the baseline boost layer:

  • imgui/imgui_widgets_builtin — both array and lambda rails.

  • imgui/imgui_boost_runtimePlotState.

8.18.1.47.1.2. Array form vs lambda form

  • Array (plot_lines / plot_histogram) — pass a values : 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) — pass values_count and a getter : 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.18.1.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.18.1.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.18.1.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.18.1.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.