diff --git a/ImPlotDemo.lua b/ImPlotDemo.lua new file mode 100644 index 0000000..d2ad8b1 --- /dev/null +++ b/ImPlotDemo.lua @@ -0,0 +1,1153 @@ +-- ImPlot demo (Luau port of implot_demo.cpp). Optional: ImPlot.lua never calls into this file. +-- Usage: require(path.to.ImPlotDemo)(G, ImPlot) -- defines ImPlot.ShowDemoWindow(p_open) -> new p_open +-- [Port] Lua has no function-level 'static': they are fields of the file-level table S (S.FunctionName.x). +-- [Port] Demo_* functions live in the table D (keeps the file chunk under the 200-locals limit). +return function(G, ImPlot) + local ImGui, ImVec2, ImVec4 = G.ImGui, G.ImVec2, G.ImVec4 + local ImGuiCond = G.ImGuiCond + local ImAxis = ImPlot.ImAxis + local ImPlotProp = ImPlot.ImPlotProp + local ImPlotFlags = ImPlot.ImPlotFlags + local ImPlotAxisFlags = ImPlot.ImPlotAxisFlags + local ImPlotMarker = ImPlot.ImPlotMarker + local ImPlotLocation = ImPlot.ImPlotLocation + local ImPlotLegendFlags = ImPlot.ImPlotLegendFlags + local ImPlotColormap = ImPlot.ImPlotColormap + local ImPlotBin = ImPlot.ImPlotBin + local ImPlotHistogramFlags = ImPlot.ImPlotHistogramFlags + local band, bor = bit32.band, bit32.bor + local _ + + local S = {} + local D = {} + + -- [Port] IMGUI_DEMO_MARKER: forwarded to ImGui.DemoMarker when available (no __LINE__ in Lua: line is 0) + local function IMGUI_DEMO_MARKER(section) + if ImGui.DemoMarker then ImGui.DemoMarker("implot_demo.cpp", 0, section) end + end + + -- [Port] CHECKBOX_FLAG(flags, flag) macro: label is the upstream flag name; returns the new flags + local function CHECKBOX_FLAG(flags, name, flag) + local _, v = ImGui.CheckboxFlags(name, flags, flag) + return v + end + + -- [Port] rand() -> math.random + local function RandomRange(min, max) + return min + math.random() * (max - min) + end + + local Gauss = { V1 = 0, V2 = 0, S = 0, phase = 0 } + local function RandomGauss() + local X + if Gauss.phase == 0 then + repeat + local U1 = math.random() + local U2 = math.random() + Gauss.V1 = 2 * U1 - 1 + Gauss.V2 = 2 * U2 - 1 + Gauss.S = Gauss.V1 * Gauss.V1 + Gauss.V2 * Gauss.V2 + until not (Gauss.S >= 1 or Gauss.S == 0) + X = Gauss.V1 * math.sqrt(-2 * math.log(Gauss.S) / Gauss.S) + else + X = Gauss.V2 * math.sqrt(-2 * math.log(Gauss.S) / Gauss.S) + end + Gauss.phase = 1 - Gauss.phase + return X + end + + -- [Port] NormalDistribution -> returns the Data array + local function NormalDistribution(N, mean, sd) + local data = table.create(N) + for i = 1, N do + data[i] = RandomGauss() * sd + mean + end + return data + end + + -- utility structure for realtime plot + -- [Port] ImVector Data -> separate X/Y arrays (Spec.Stride is in elements, no interleaving needed) + local function ScrollingBuffer(max_size) + return { MaxSize = max_size or 2000, Offset = 0, X = {}, Y = {} } + end + local function ScrollingBuffer_AddPoint(b, x, y) + if #b.X < b.MaxSize then + table.insert(b.X, x) + table.insert(b.Y, y) + else + b.X[b.Offset + 1] = x + b.Y[b.Offset + 1] = y + b.Offset = (b.Offset + 1) % b.MaxSize + end + end + + -- utility structure for realtime plot + local function RollingBuffer() + return { Span = 10.0, X = {}, Y = {} } + end + local function RollingBuffer_AddPoint(b, x, y) + local xmod = math.fmod(x, b.Span) + if #b.X > 0 and xmod < b.X[#b.X] then + table.clear(b.X) + table.clear(b.Y) + end + table.insert(b.X, xmod) + table.insert(b.Y, y) + end + + --------------------------------------------------------------------------- + -- [SECTION] Demo Functions + --------------------------------------------------------------------------- + + function D.Demo_Help() + IMGUI_DEMO_MARKER("Demo_Help") + ImGui.Text("ABOUT THIS DEMO:") + ImGui.BulletText("Sections below are demonstrating many aspects of the library.") + ImGui.BulletText("The \"Tools\" menu above gives access to: Style Editors (ImPlot/ImGui)\n" .. + "and Metrics (general purpose Dear ImGui debugging tool).") + ImGui.Separator() + ImGui.Text("PROGRAMMER GUIDE:") + ImGui.BulletText("See the ShowDemoWindow() code in implot_demo.cpp. <- you are here!") + ImGui.BulletText("If you see visual artifacts, do one of the following:") + ImGui.Indent() + ImGui.BulletText("Handle ImGuiBackendFlags_RendererHasVtxOffset for 16-bit indices in your backend.") + ImGui.BulletText("Or, enable 32-bit indices in imconfig.h.") + ImGui.BulletText("Your current configuration is:") + ImGui.Indent() + ImGui.BulletText("ImDrawIdx: %d-bit", 32) -- [Port] ImDrawIdx is unsigned int in the core + local has_vtx_offset = "False" + if band(ImGui.GetIO().BackendFlags, G.ImGuiBackendFlags.RendererHasVtxOffset) ~= 0 then has_vtx_offset = "True" end + ImGui.BulletText("ImGuiBackendFlags_RendererHasVtxOffset: %s", has_vtx_offset) + ImGui.Unindent() + ImGui.Unindent() + ImGui.Separator() + ImGui.Text("USER GUIDE:") + ImPlot.ShowUserGuide() + end + + --------------------------------------------------------------------------- + + function D.Demo_Config() + IMGUI_DEMO_MARKER("Config") + ImGui.ShowFontSelector("Font") + ImGui.ShowStyleSelector("ImGui Style") + ImPlot.ShowStyleSelector("ImPlot Style") + ImPlot.ShowColormapSelector("ImPlot Colormap") + ImPlot.ShowInputMapSelector("Input Map") + ImGui.Separator() + local style = ImPlot.GetStyle() + _, style.UseLocalTime = ImGui.Checkbox("Use Local Time", style.UseLocalTime) + _, style.UseISO8601 = ImGui.Checkbox("Use ISO 8601", style.UseISO8601) + _, style.Use24HourClock = ImGui.Checkbox("Use 24 Hour Clock", style.Use24HourClock) + ImGui.Separator() + if ImPlot.BeginPlot("Preview") then + S.Demo_Config = S.Demo_Config or { now = os.time() } + local now = S.Demo_Config.now + ImPlot.SetupAxisScale(ImAxis.X1, ImPlot.ImPlotScale.Time) + ImPlot.SetupAxisLimits(ImAxis.X1, now, now + 24 * 3600) + for i = 0, 9 do + local x = { now, now + 24 * 3600 } + local y = { 0, i / 9.0 } + ImGui.PushID(i) + ImPlot.PlotLine("##Line", x, y, 2) + ImGui.PopID() + end + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_LinePlots = { xs1 = table.create(1001, 0), ys1 = table.create(1001, 0), xs2 = table.create(20, 0), ys2 = table.create(20, 0) } + function D.Demo_LinePlots() + IMGUI_DEMO_MARKER("Plots/Line Plots") + local s = S.Demo_LinePlots + local xs1, ys1, xs2, ys2 = s.xs1, s.ys1, s.xs2, s.ys2 + local t = ImGui.GetTime() + for i = 0, 1000 do + xs1[i + 1] = i * 0.001 + ys1[i + 1] = 0.5 + 0.5 * math.sin(50 * (xs1[i + 1] + t / 10)) + end + for i = 0, 19 do + xs2[i + 1] = i * 1 / 19.0 + ys2[i + 1] = xs2[i + 1] * xs2[i + 1] + end + if ImPlot.BeginPlot("Line Plots") then + ImPlot.SetupAxes("x", "y") + ImPlot.PlotLine("f(x)", xs1, ys1, 1001) + ImPlot.PlotLine("g(x)", xs2, ys2, 20, { + Marker = ImPlotMarker.Circle, + Flags = ImPlot.ImPlotLineFlags.Segments, + }) + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_FilledLinePlots = { + xs1 = table.create(101, 0), ys1 = table.create(101, 0), ys2 = table.create(101, 0), ys3 = table.create(101, 0), + show_lines = true, show_fills = true, fill_ref = 0, shade_mode = 0, flags = 0, + } + function D.Demo_FilledLinePlots() + IMGUI_DEMO_MARKER("Plots/Filled Line Plots") + local s = S.Demo_FilledLinePlots + local xs1, ys1, ys2, ys3 = s.xs1, s.ys1, s.ys2, s.ys3 + math.randomseed(0) + for i = 1, 101 do + xs1[i] = i - 1 + ys1[i] = RandomRange(400.0, 450.0) + ys2[i] = RandomRange(275.0, 350.0) + ys3[i] = RandomRange(150.0, 225.0) + end + _, s.show_lines = ImGui.Checkbox("Lines", s.show_lines); ImGui.SameLine() + _, s.show_fills = ImGui.Checkbox("Fills", s.show_fills) + if s.show_fills then + ImGui.SameLine() + _, s.shade_mode = ImGui.RadioButton("To -INF", s.shade_mode, 0) + ImGui.SameLine() + _, s.shade_mode = ImGui.RadioButton("To +INF", s.shade_mode, 1) + ImGui.SameLine() + _, s.shade_mode = ImGui.RadioButton("To Ref", s.shade_mode, 2) + if s.shade_mode == 2 then + ImGui.SameLine() + ImGui.SetNextItemWidth(100) + s.fill_ref = ImGui.DragFloat("##Ref", s.fill_ref, 1, -100, 500) + end + end + + if ImPlot.BeginPlot("Stock Prices") then + ImPlot.SetupAxes("Days", "Price") + ImPlot.SetupAxesLimits(0, 100, 0, 500) + if s.show_fills then + local spec = ImPlot.ImPlotSpec() + spec.Flags = s.flags + spec.FillAlpha = 0.25 + local ref = s.fill_ref + if s.shade_mode == 0 then ref = -math.huge elseif s.shade_mode == 1 then ref = math.huge end + ImPlot.PlotShaded("Stock 1", xs1, ys1, 101, ref, spec) + ImPlot.PlotShaded("Stock 2", xs1, ys2, 101, ref, spec) + ImPlot.PlotShaded("Stock 3", xs1, ys3, 101, ref, spec) + end + if s.show_lines then + ImPlot.PlotLine("Stock 1", xs1, ys1, 101) + ImPlot.PlotLine("Stock 2", xs1, ys2, 101) + ImPlot.PlotLine("Stock 3", xs1, ys3, 101) + end + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_ShadedPlots = { + xs = table.create(1001, 0), ys = table.create(1001, 0), ys1 = table.create(1001, 0), + ys2 = table.create(1001, 0), ys3 = table.create(1001, 0), ys4 = table.create(1001, 0), + spec = nil, -- [Port] created on first use (ImPlotSpec(ImPlotProp_FillAlpha, 0.25f)) + } + function D.Demo_ShadedPlots() + IMGUI_DEMO_MARKER("Plots/Shaded Plots") + local s = S.Demo_ShadedPlots + local xs, ys, ys1, ys2, ys3, ys4 = s.xs, s.ys, s.ys1, s.ys2, s.ys3, s.ys4 + math.randomseed(0) + for i = 1, 1001 do + xs[i] = (i - 1) * 0.001 + ys[i] = 0.25 + 0.25 * math.sin(25 * xs[i]) * math.sin(5 * xs[i]) + RandomRange(-0.01, 0.01) + ys1[i] = ys[i] + RandomRange(0.1, 0.12) + ys2[i] = ys[i] - RandomRange(0.1, 0.12) + ys3[i] = 0.75 + 0.2 * math.sin(25 * xs[i]) + ys4[i] = 0.75 + 0.1 * math.cos(25 * xs[i]) + end + if s.spec == nil then s.spec = ImPlot.ImPlotSpec(ImPlotProp.FillAlpha, 0.25) end + local spec = s.spec + spec.FillAlpha = ImGui.DragFloat("Alpha", spec.FillAlpha, 0.01, 0, 1) + + if ImPlot.BeginPlot("Shaded Plots") then + ImPlot.SetupLegend(ImPlotLocation.NorthWest, ImPlotLegendFlags.Reverse) -- reverse legend to match vertical order on plot + ImPlot.PlotShaded("Uncertain Data", xs, ys1, ys2, 1001, spec) + ImPlot.PlotLine("Uncertain Data", xs, ys, 1001, spec) + ImPlot.PlotShaded("Overlapping", xs, ys3, ys4, 1001, spec) + ImPlot.PlotLine("Overlapping", xs, ys3, 1001, spec) + ImPlot.PlotLine("Overlapping", xs, ys4, 1001, spec) + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_ScatterPlots = { xs1 = table.create(100, 0), ys1 = table.create(100, 0), xs2 = table.create(50, 0), ys2 = table.create(50, 0) } + function D.Demo_ScatterPlots() + IMGUI_DEMO_MARKER("Plots/Scatter Plots") + local s = S.Demo_ScatterPlots + math.randomseed(0) + for i = 1, 100 do + s.xs1[i] = (i - 1) * 0.01 + s.ys1[i] = s.xs1[i] + 0.1 * math.random() + end + for i = 1, 50 do + s.xs2[i] = 0.25 + 0.2 * math.random() + s.ys2[i] = 0.75 + 0.2 * math.random() + end + + if ImPlot.BeginPlot("Scatter Plot") then + ImPlot.PlotScatter("Data 1", s.xs1, s.ys1, 100) + ImPlot.PlotScatter("Data 2", s.xs2, s.ys2, 50, { + Marker = ImPlotMarker.Square, + MarkerSize = 6, + LineColor = ImPlot.GetColormapColor(1), + FillColor = ImPlot.GetColormapColor(1), + FillAlpha = 0.25, + }) + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_BubblePlots = { xs = table.create(20, 0), ys1 = table.create(20, 0), ys2 = table.create(20, 0), szs1 = table.create(20, 0), szs2 = table.create(20, 0) } + function D.Demo_BubblePlots() + IMGUI_DEMO_MARKER("Plots/Bubble Plots") + local s = S.Demo_BubblePlots + math.randomseed(0) + for i = 1, 20 do + s.xs[i] = (i - 1) * 0.1 + s.ys1[i] = math.random() + s.ys2[i] = math.random() + + s.szs1[i] = 0.02 + 0.08 * math.random() + s.szs2[i] = 0.02 + 0.08 * math.random() + end + + if ImPlot.BeginPlot("Bubble Plot", ImVec2(-1, 0), ImPlotFlags.Equal) then + ImPlot.PlotBubbles("Data 1", s.xs, s.ys1, s.szs1, 20, { FillAlpha = 0.5 }) + ImPlot.PlotBubbles("Data 2", s.xs, s.ys2, s.szs2, 20, { FillAlpha = 0.5, LineColor = ImVec4(0, 0, 0, 0.0) }) + + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_PolygonPlots = { + -- Triangle (convex) + tri_xs = { 0.5, 1.0, 0.0 }, + tri_ys = { 1.0, 0.0, 0.0 }, + pent_xs = table.create(5, 0), pent_ys = table.create(5, 0), + star_xs = table.create(10, 0), star_ys = table.create(10, 0), + } + function D.Demo_PolygonPlots() + IMGUI_DEMO_MARKER("Plots/Polygon Plots") + local s = S.Demo_PolygonPlots + -- Pentagon (convex) + for i = 0, 4 do + local angle = i * 2.0 * 3.14159 / 5.0 - 3.14159 / 2.0 + s.pent_xs[i + 1] = 3.0 + 0.8 * math.cos(angle) + s.pent_ys[i + 1] = 0.5 + 0.8 * math.sin(angle) + end + + -- Star (concave), counter-clockwise + for i = 0, 9 do + local angle = i * 2.0 * 3.14159 / 10.0 - 3.14159 / 2.0 + local radius = 0.3 + if i % 2 == 0 then radius = 0.8 end + s.star_xs[i + 1] = 5.5 + radius * math.cos(angle) + s.star_ys[i + 1] = 0.5 + radius * math.sin(angle) + end + + -- [Port] PlotPolygon is not ported yet in ImPlot.lua + if ImPlot.PlotPolygon == nil then + ImGui.TextDisabled("[Port] TODO: PlotPolygon is not ported yet") + return + end + if ImPlot.BeginPlot("Polygon Plot", ImVec2(-1, 0), ImPlotFlags.Equal) then + ImPlot.PlotPolygon("Triangle", s.tri_xs, s.tri_ys, 3, { + FillAlpha = 0.5, + }) + ImPlot.PlotPolygon("Pentagon", s.pent_xs, s.pent_ys, 5, { + FillAlpha = 0.5, + FillColor = ImVec4(0, 1, 0, 1), + }) + ImPlot.PlotPolygon("Star (Concave)", s.star_xs, s.star_ys, 10, { + FillAlpha = 0.5, + FillColor = ImVec4(1, 1, 0, 1), + Flags = ImPlot.ImPlotPolygonFlags.Concave, + }) + + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_StairstepPlots = { ys1 = table.create(21, 0), ys2 = table.create(21, 0), flags = 0 } + function D.Demo_StairstepPlots() + IMGUI_DEMO_MARKER("Plots/Stairstep Plots") + local s = S.Demo_StairstepPlots + for i = 0, 20 do + s.ys1[i + 1] = 0.75 + 0.2 * math.sin(10 * i * 0.05) + s.ys2[i + 1] = 0.25 + 0.2 * math.sin(10 * i * 0.05) + end + local ImPlotStairsFlags = ImPlot.ImPlotStairsFlags + s.flags = CHECKBOX_FLAG(s.flags, "ImPlotStairsFlags_Shaded", ImPlotStairsFlags.Shaded) + if ImPlot.BeginPlot("Stairstep Plot") then + ImPlot.SetupAxes("x", "f(x)") + ImPlot.SetupAxesLimits(0, 1, 0, 1) + ImPlot.PlotLine("##1", s.ys1, 21, 0.05, 0, { LineColor = ImVec4(0.5, 0.5, 0.5, 1.0) }) + ImPlot.PlotLine("##2", s.ys2, 21, 0.05, 0, { LineColor = ImVec4(0.5, 0.5, 0.5, 1.0) }) + + local spec = ImPlot.ImPlotSpec() + spec.Flags = s.flags + spec.FillAlpha = 0.25 + spec.Marker = ImPlotMarker.Auto + ImPlot.PlotStairs("Post Step (default)", s.ys1, 21, 0.05, 0, spec) + + spec.Flags = bor(s.flags, ImPlotStairsFlags.PreStep) + spec.FillAlpha = 0.25 + spec.Marker = ImPlotMarker.Auto + ImPlot.PlotStairs("Pre Step", s.ys2, 21, 0.05, 0, spec) + + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_BarPlots = { data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } } + function D.Demo_BarPlots() + IMGUI_DEMO_MARKER("Plots/Bar Plots") + local data = S.Demo_BarPlots.data + if ImPlot.BeginPlot("Bar Plot") then + ImPlot.PlotBars("Vertical", data, 10, 0.7, 1) + ImPlot.PlotBars("Horizontal", data, 10, 0.4, 1, { Flags = ImPlot.ImPlotBarsFlags.Horizontal }) + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_BarGroups = { + data = { 83, 67, 23, 89, 83, 78, 91, 82, 85, 90, -- midterm + 80, 62, 56, 99, 55, 78, 88, 78, 90, 100, -- final + 80, 69, 52, 92, 72, 78, 75, 76, 89, 95 }, -- course + ilabels = { "Midterm Exam", "Final Exam", "Course Grade" }, + glabels = { "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10" }, + positions = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, + items = 3, + groups = 10, + size = 0.67, + flags = 0, + horz = false, + } + function D.Demo_BarGroups() + IMGUI_DEMO_MARKER("Plots/Bar Groups") + local s = S.Demo_BarGroups + local ImPlotBarGroupsFlags = ImPlot.ImPlotBarGroupsFlags + + _, s.flags = ImGui.CheckboxFlags("Stacked", s.flags, ImPlotBarGroupsFlags.Stacked) + ImGui.SameLine() + _, s.horz = ImGui.Checkbox("Horizontal", s.horz) + + s.items = ImGui.SliderInt("Items", s.items, 1, 3) + s.size = ImGui.SliderFloat("Size", s.size, 0, 1) + + if ImPlot.BeginPlot("Bar Group") then + ImPlot.SetupLegend(ImPlotLocation.East, ImPlotLegendFlags.Outside) + if s.horz then + ImPlot.SetupAxes("Score", "Student", ImPlotAxisFlags.AutoFit, ImPlotAxisFlags.AutoFit) + ImPlot.SetupAxisTicks(ImAxis.Y1, s.positions, s.groups, s.glabels) + ImPlot.PlotBarGroups(s.ilabels, s.data, s.items, s.groups, s.size, 0, { Flags = bor(s.flags, ImPlotBarGroupsFlags.Horizontal) }) + else + ImPlot.SetupAxes("Student", "Score", ImPlotAxisFlags.AutoFit, ImPlotAxisFlags.AutoFit) + ImPlot.SetupAxisTicks(ImAxis.X1, s.positions, s.groups, s.glabels) + ImPlot.PlotBarGroups(s.ilabels, s.data, s.items, s.groups, s.size, 0, { Flags = s.flags }) + end + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_BarStacks = { + Liars = -1, + diverging = true, + politicians = { "Trump", "Bachman", "Cruz", "Gingrich", "Palin", "Santorum", "Walker", "Perry", "Ryan", "McCain", "Rubio", "Romney", "Rand Paul", "Christie", "Biden", "Kasich", "Sanders", "J Bush", "H Clinton", "Obama" }, + data_reg = { 18, 26, 7, 14, 10, 8, 6, 11, 4, 4, 3, 8, 6, 8, 6, 5, 0, 3, 1, 2, -- Pants on Fire + 43, 36, 30, 21, 30, 27, 25, 17, 11, 22, 15, 16, 16, 17, 12, 12, 14, 6, 13, 12, -- False + 16, 13, 28, 22, 15, 21, 15, 18, 30, 17, 24, 18, 13, 10, 14, 15, 17, 22, 14, 12, -- Mostly False + 17, 10, 13, 25, 12, 22, 19, 26, 23, 17, 22, 27, 20, 26, 29, 17, 18, 22, 21, 27, -- Half True + 5, 7, 16, 10, 10, 12, 23, 13, 17, 20, 22, 16, 23, 19, 20, 26, 36, 29, 27, 26, -- Mostly True + 1, 8, 6, 8, 23, 10, 12, 15, 15, 20, 14, 15, 22, 20, 19, 25, 15, 18, 24, 21 }, -- True + labels_reg = { "Pants on Fire", "False", "Mostly False", "Half True", "Mostly True", "True" }, + data_div = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -- Pants on Fire (dummy, to order legend logically) + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -- False (dummy, to order legend logically) + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -- Mostly False (dummy, to order legend logically) + -16, -13, -28, -22, -15, -21, -15, -18, -30, -17, -24, -18, -13, -10, -14, -15, -17, -22, -14, -12, -- Mostly False + -43, -36, -30, -21, -30, -27, -25, -17, -11, -22, -15, -16, -16, -17, -12, -12, -14, -6, -13, -12, -- False + -18, -26, -7, -14, -10, -8, -6, -11, -4, -4, -3, -8, -6, -8, -6, -5, 0, -3, -1, -2, -- Pants on Fire + 17, 10, 13, 25, 12, 22, 19, 26, 23, 17, 22, 27, 20, 26, 29, 17, 18, 22, 21, 27, -- Half True + 5, 7, 16, 10, 10, 12, 23, 13, 17, 20, 22, 16, 23, 19, 20, 26, 36, 29, 27, 26, -- Mostly True + 1, 8, 6, 8, 23, 10, 12, 15, 15, 20, 14, 15, 22, 20, 19, 25, 15, 18, 24, 21 }, -- True + labels_div = { "Pants on Fire", "False", "Mostly False", "Mostly False", "False", "Pants on Fire", "Half True", "Mostly True", "True" }, + } + function D.Demo_BarStacks() + IMGUI_DEMO_MARKER("Plots/Bar Stacks") + local s = S.Demo_BarStacks + + if s.Liars == -1 then + -- [Port] reuse the colormap if a previous demo run (same context) already added it + s.Liars = ImPlot.GetColormapIndex("Liars") + if s.Liars == -1 then + local Liars_Data = { 4282515870, 4282609140, 4287357182, 4294630301, 4294945280, 4294921472 } + s.Liars = ImPlot.AddColormap("Liars", Liars_Data, 6) + end + end + + _, s.diverging = ImGui.Checkbox("Diverging", s.diverging) + + ImPlot.PushColormap(s.Liars) + if ImPlot.BeginPlot("PolitiFact: Who Lies More?", ImVec2(-1, ImGui.GetTextLineHeight() * 25), ImPlotFlags.NoMouseText) then + ImPlot.SetupLegend(ImPlotLocation.South, bor(ImPlotLegendFlags.Outside, ImPlotLegendFlags.Horizontal)) + ImPlot.SetupAxes(nil, nil, bor(ImPlotAxisFlags.AutoFit, ImPlotAxisFlags.NoDecorations), bor(ImPlotAxisFlags.AutoFit, ImPlotAxisFlags.Invert)) + ImPlot.SetupAxisTicks(ImAxis.Y1, 0, 19, 20, s.politicians, false) + local spec = ImPlot.ImPlotSpec(); spec.Flags = bor(ImPlot.ImPlotBarGroupsFlags.Stacked, ImPlot.ImPlotBarGroupsFlags.Horizontal) + if s.diverging then + ImPlot.PlotBarGroups(s.labels_div, s.data_div, 9, 20, 0.75, 0, spec) + else + ImPlot.PlotBarGroups(s.labels_reg, s.data_reg, 6, 20, 0.75, 0, spec) + end + ImPlot.EndPlot() + end + ImPlot.PopColormap() + end + + --------------------------------------------------------------------------- + + S.Demo_ErrorBars = { + xs = { 1, 2, 3, 4, 5 }, + bar = { 1, 2, 5, 3, 4 }, + lin1 = { 8, 8, 9, 7, 8 }, + lin2 = { 6, 7, 6, 9, 6 }, + err1 = { 0.2, 0.4, 0.2, 0.6, 0.4 }, + err2 = { 0.4, 0.2, 0.4, 0.8, 0.6 }, + err3 = { 0.09, 0.14, 0.09, 0.12, 0.16 }, + err4 = { 0.02, 0.08, 0.15, 0.05, 0.2 }, + } + function D.Demo_ErrorBars() + IMGUI_DEMO_MARKER("Plots/Error Bars") + local s = S.Demo_ErrorBars + local xs = s.xs + + if ImPlot.BeginPlot("##ErrorBars") then + ImPlot.SetupAxesLimits(0, 6, 0, 10) + + ImPlot.PlotBars("Bar", xs, s.bar, 5, 0.5) + ImPlot.PlotErrorBars("Bar", xs, s.bar, s.err1, 5) + + ImPlot.PlotErrorBars("Line", xs, s.lin1, s.err1, s.err2, 5, { LineColor = ImPlot.GetColormapColor(1), Size = 0 }) + ImPlot.PlotLine("Line", xs, s.lin1, 5, { Marker = ImPlotMarker.Square }) + + local spec = ImPlot.ImPlotSpec() + spec.LineColor = ImPlot.GetColormapColor(2) + spec.Size = 6 + spec.LineWeight = 1.5 + ImPlot.PlotErrorBars("Scatter", xs, s.lin2, s.err2, 5, spec) + spec.Flags = ImPlot.ImPlotErrorBarsFlags.Horizontal + ImPlot.PlotErrorBars("Scatter", xs, s.lin2, s.err3, s.err4, 5, spec) + ImPlot.PlotScatter("Scatter", xs, s.lin2, 5) + + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_StemPlots = { xs = table.create(51, 0), ys1 = table.create(51, 0), ys2 = table.create(51, 0) } + function D.Demo_StemPlots() + IMGUI_DEMO_MARKER("Plots/Stem Plots") + local s = S.Demo_StemPlots + for i = 1, 51 do + local x = (i - 1) * 0.02 + s.xs[i] = x + s.ys1[i] = 1.0 + 0.5 * math.sin(25 * x) * math.cos(2 * x) + s.ys2[i] = 0.5 + 0.25 * math.sin(10 * x) * math.sin(x) + end + if ImPlot.BeginPlot("Stem Plots") then + ImPlot.SetupAxisLimits(ImAxis.X1, 0, 1.0) + ImPlot.SetupAxisLimits(ImAxis.Y1, 0, 1.6) + ImPlot.PlotStems("Stems 1", s.xs, s.ys1, 51) + ImPlot.PlotStems("Stems 2", s.xs, s.ys2, 51, 0, { Marker = ImPlotMarker.Circle }) + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_InfiniteLines = { vals = { 0.25, 0.5, 0.75 } } + function D.Demo_InfiniteLines() + IMGUI_DEMO_MARKER("Plots/Infinite Lines") + local vals = S.Demo_InfiniteLines.vals + if ImPlot.BeginPlot("##Infinite") then + ImPlot.SetupAxes(nil, nil, ImPlotAxisFlags.NoInitialFit, ImPlotAxisFlags.NoInitialFit) + ImPlot.PlotInfLines("Vertical", vals, 3) + ImPlot.PlotInfLines("Horizontal", vals, 3, { Flags = ImPlot.ImPlotInfLinesFlags.Horizontal }) + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_PieCharts = { + labels1 = { "Frogs", "Hogs", "Dogs", "Logs" }, + data1 = { 0.15, 0.30, 0.2, 0.05 }, + flags = 0, + labels2 = { "A", "B", "C", "D", "E" }, + data2 = { 1, 1, 2, 3, 5 }, + } + function D.Demo_PieCharts() + IMGUI_DEMO_MARKER("Plots/Pie Charts") + local s = S.Demo_PieCharts + local ImPlotPieChartFlags = ImPlot.ImPlotPieChartFlags + ImGui.SetNextItemWidth(250) + ImGui.DragFloat4("Values", s.data1, 0.01, 0, 1) + s.flags = CHECKBOX_FLAG(s.flags, "ImPlotPieChartFlags_Normalize", ImPlotPieChartFlags.Normalize) + s.flags = CHECKBOX_FLAG(s.flags, "ImPlotPieChartFlags_IgnoreHidden", ImPlotPieChartFlags.IgnoreHidden) + s.flags = CHECKBOX_FLAG(s.flags, "ImPlotPieChartFlags_Exploding", ImPlotPieChartFlags.Exploding) + s.flags = CHECKBOX_FLAG(s.flags, "ImPlotPieChartFlags_NoSliceBorder", ImPlotPieChartFlags.NoSliceBorder) + + local sz = ImGui.GetTextLineHeight() * 16 + if ImPlot.BeginPlot("##Pie1", ImVec2(sz, sz), bor(ImPlotFlags.Equal, ImPlotFlags.NoMouseText)) then + ImPlot.SetupAxes(nil, nil, ImPlotAxisFlags.NoDecorations, ImPlotAxisFlags.NoDecorations) + ImPlot.SetupAxesLimits(0, 1, 0, 1) + ImPlot.PlotPieChart(s.labels1, s.data1, 4, 0.5, 0.5, 0.4, "%.2f", 90, { Flags = s.flags }) + ImPlot.EndPlot() + end + + ImGui.SameLine() + + ImPlot.PushColormap(ImPlotColormap.Pastel) + if ImPlot.BeginPlot("##Pie2", ImVec2(sz, sz), bor(ImPlotFlags.Equal, ImPlotFlags.NoMouseText)) then + ImPlot.SetupAxes(nil, nil, ImPlotAxisFlags.NoDecorations, ImPlotAxisFlags.NoDecorations) + ImPlot.SetupAxesLimits(0, 1, 0, 1) + ImPlot.PlotPieChart(s.labels2, s.data2, 5, 0.5, 0.5, 0.4, "%.0f", 180, { Flags = s.flags }) + ImPlot.EndPlot() + end + ImPlot.PopColormap() + end + + --------------------------------------------------------------------------- + + S.Demo_Heatmaps = { + -- [Port] float values1[7][7] -> flat row-major array + values1 = { 0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0, + 2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0, + 1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0, + 0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0, + 0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0, + 1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1, + 0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3 }, + scale_min = 0, + scale_max = 6.3, + xlabels = { "C1", "C2", "C3", "C4", "C5", "C6", "C7" }, + ylabels = { "R1", "R2", "R3", "R4", "R5", "R6", "R7" }, + map = ImPlotColormap.Viridis, + hm_flags = 0, + axes_flags = bor(ImPlotAxisFlags.Lock, ImPlotAxisFlags.NoGridLines, ImPlotAxisFlags.NoTickMarks), + values2 = table.create(80 * 80, 0), + } + function D.Demo_Heatmaps() + IMGUI_DEMO_MARKER("Plots/Heatmaps") + local s = S.Demo_Heatmaps + + if ImPlot.ColormapButton(ImPlot.GetColormapName(s.map), ImVec2(225, 0), s.map) then + s.map = (s.map + 1) % ImPlot.GetColormapCount() + -- We bust the color cache of our plots so that item colors will + -- resample the new colormap in the event that they have already + -- been created. See documentation in implot.h. + ImPlot.BustColorCache("##Heatmap1") + ImPlot.BustColorCache("##Heatmap2") + end + + ImGui.SameLine() + ImGui.LabelText("##Colormap Index", "%s", "Change Colormap") + ImGui.SetNextItemWidth(225) + s.scale_min, s.scale_max = ImGui.DragFloatRange2("Min / Max", s.scale_min, s.scale_max, 0.01, -20, 20) + + _, s.hm_flags = ImGui.CheckboxFlags("Column Major", s.hm_flags, ImPlot.ImPlotHeatmapFlags.ColMajor) + + local axes_flags = s.axes_flags + + ImPlot.PushColormap(s.map) + + local sz = ImGui.GetTextLineHeight() * 14 + if ImPlot.BeginPlot("##Heatmap1", ImVec2(sz, sz), bor(ImPlotFlags.NoLegend, ImPlotFlags.NoMouseText)) then + ImPlot.SetupAxes(nil, nil, axes_flags, axes_flags) + ImPlot.SetupAxisTicks(ImAxis.X1, 0 + 1.0 / 14.0, 1 - 1.0 / 14.0, 7, s.xlabels) + ImPlot.SetupAxisTicks(ImAxis.Y1, 1 - 1.0 / 14.0, 0 + 1.0 / 14.0, 7, s.ylabels) + ImPlot.PlotHeatmap("heat", s.values1, 7, 7, s.scale_min, s.scale_max, "%g", ImPlot.ImPlotPoint(0, 0), ImPlot.ImPlotPoint(1, 1), { Flags = s.hm_flags }) + ImPlot.EndPlot() + end + ImGui.SameLine() + ImPlot.ColormapScale("##HeatScale", s.scale_min, s.scale_max, ImVec2(60, 225)) + + ImGui.SameLine() + + local size = 80 + local values2 = s.values2 + math.randomseed(math.floor(ImGui.GetTime() * 1000000)) + for i = 1, size * size do + values2[i] = RandomRange(0.0, 1.0) + end + + if ImPlot.BeginPlot("##Heatmap2", ImVec2(sz, sz)) then + ImPlot.SetupAxes(nil, nil, ImPlotAxisFlags.NoDecorations, ImPlotAxisFlags.NoDecorations) + ImPlot.SetupAxesLimits(-1, 1, -1, 1) + ImPlot.PlotHeatmap("heat1", values2, size, size, 0, 1, false) + ImPlot.PlotHeatmap("heat2", values2, size, size, 0, 1, false, ImPlot.ImPlotPoint(-1, -1), ImPlot.ImPlotPoint(0, 0)) + ImPlot.EndPlot() + end + ImPlot.PopColormap() + end + + --------------------------------------------------------------------------- + + S.Demo_Histogram = { + hist_flags = ImPlotHistogramFlags.Density, + bins = 50, + mu = 5, + sigma = 2, + range = false, + rrange = { -3, 13 }, -- [Port] rmin, rmax -> one array for DragFloat2 + dist = nil, -- [Port] NormalDistribution<10000> created on first use + x = table.create(100, 0), + y = table.create(100, 0), + } + function D.Demo_Histogram() + IMGUI_DEMO_MARKER("Plots/Histogram") + local s = S.Demo_Histogram + ImGui.SetNextItemWidth(200) + _, s.bins = ImGui.RadioButton("Sqrt", s.bins, ImPlotBin.Sqrt); ImGui.SameLine() + _, s.bins = ImGui.RadioButton("Sturges", s.bins, ImPlotBin.Sturges); ImGui.SameLine() + _, s.bins = ImGui.RadioButton("Rice", s.bins, ImPlotBin.Rice); ImGui.SameLine() + _, s.bins = ImGui.RadioButton("Scott", s.bins, ImPlotBin.Scott); ImGui.SameLine() + if ImGui.RadioButtonEx("N Bins", s.bins >= 0) then s.bins = 50 end + if s.bins >= 0 then + ImGui.SameLine() + ImGui.SetNextItemWidth(200) + s.bins = ImGui.SliderInt("##Bins", s.bins, 1, 100) + end + _, s.hist_flags = ImGui.CheckboxFlags("Horizontal", s.hist_flags, ImPlotHistogramFlags.Horizontal) + ImGui.SameLine() + _, s.hist_flags = ImGui.CheckboxFlags("Density", s.hist_flags, ImPlotHistogramFlags.Density) + ImGui.SameLine() + _, s.hist_flags = ImGui.CheckboxFlags("Cumulative", s.hist_flags, ImPlotHistogramFlags.Cumulative) + + _, s.range = ImGui.Checkbox("Range", s.range) + if s.range then + ImGui.SameLine() + ImGui.SetNextItemWidth(200) + ImGui.DragFloat2("##Range", s.rrange, 0.1, -3, 13) + ImGui.SameLine() + _, s.hist_flags = ImGui.CheckboxFlags("Exclude Outliers", s.hist_flags, ImPlotHistogramFlags.NoOutliers) + end + if s.dist == nil then s.dist = NormalDistribution(10000, s.mu, s.sigma) end + local mu, sigma, x, y = s.mu, s.sigma, s.x, s.y + local hist_flags = s.hist_flags + if band(hist_flags, ImPlotHistogramFlags.Density) ~= 0 then + for i = 1, 100 do + x[i] = -3 + 16 * (i - 1) / 99.0 + y[i] = math.exp(-(x[i] - mu) * (x[i] - mu) / (2 * sigma * sigma)) / (sigma * math.sqrt(2 * 3.141592653589793238)) + end + if band(hist_flags, ImPlotHistogramFlags.Cumulative) ~= 0 then + for i = 2, 100 do + y[i] += y[i - 1] + end + for i = 1, 100 do + y[i] /= y[100] + end + end + end + + if ImPlot.BeginPlot("##Histograms") then + ImPlot.SetupAxes(nil, nil, ImPlotAxisFlags.AutoFit, ImPlotAxisFlags.AutoFit) + local range = ImPlot.ImPlotRange() + if s.range then range = ImPlot.ImPlotRange(s.rrange[1], s.rrange[2]) end + ImPlot.PlotHistogram("Empirical", s.dist, 10000, s.bins, 1.0, range, { + FillAlpha = 0.5, + Flags = hist_flags, + }) + if band(hist_flags, ImPlotHistogramFlags.Density) ~= 0 and band(hist_flags, ImPlotHistogramFlags.NoOutliers) == 0 then + if band(hist_flags, ImPlotHistogramFlags.Horizontal) ~= 0 then + ImPlot.PlotLine("Theoretical", y, x, 100) + else + ImPlot.PlotLine("Theoretical", x, y, 100) + end + end + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_Histogram2D = { count = 50000, xybins = { 100, 100 }, hist_flags = 0, dist1 = nil, dist2 = nil } + function D.Demo_Histogram2D() + IMGUI_DEMO_MARKER("Plots/Histogram 2D") + local s = S.Demo_Histogram2D + + s.count = ImGui.SliderInt("Count", s.count, 100, 100000) + ImGui.SliderInt2("Bins", s.xybins, 1, 500) + ImGui.SameLine() + _, s.hist_flags = ImGui.CheckboxFlags("Density", s.hist_flags, ImPlotHistogramFlags.Density) + + if s.dist1 == nil then + s.dist1 = NormalDistribution(100000, 1, 2) + s.dist2 = NormalDistribution(100000, 1, 1) + end + local max_count = 0 + local flags = bor(ImPlotAxisFlags.AutoFit, ImPlotAxisFlags.Foreground) + ImPlot.PushColormap("Hot") + if ImPlot.BeginPlot("##Hist2D", ImVec2(ImGui.GetContentRegionAvail().x - 100 - ImGui.GetStyle().ItemSpacing.x, 0)) then + ImPlot.SetupAxes(nil, nil, flags, flags) + ImPlot.SetupAxesLimits(-6, 6, -6, 6) + max_count = ImPlot.PlotHistogram2D("Hist2D", s.dist1, s.dist2, s.count, s.xybins[1], s.xybins[2], ImPlot.ImPlotRect(-6, 6, -6, 6), { Flags = s.hist_flags }) + ImPlot.EndPlot() + end + ImGui.SameLine() + local label = "Count" + if band(s.hist_flags, ImPlotHistogramFlags.Density) ~= 0 then label = "Density" end + ImPlot.ColormapScale(label, 0, max_count, ImVec2(100, 0)) + ImPlot.PopColormap() + end + + --------------------------------------------------------------------------- + + S.Demo_DigitalPlots = { + paused = false, + dataDigital = { ScrollingBuffer(), ScrollingBuffer(), ScrollingBuffer() }, + dataAnalog = { ScrollingBuffer(), ScrollingBuffer() }, + showDigital = { true, false, false }, + showAnalog = { true, false }, + t = 0, + last_t = 0, + } + function D.Demo_DigitalPlots() + IMGUI_DEMO_MARKER("Plots/Digital Plots") + local s = S.Demo_DigitalPlots + ImGui.BulletText("Digital plots do not respond to Y drag and zoom, so that") + ImGui.Indent() + ImGui.Text("you can drag analog plots over the rising/falling digital edge.") + ImGui.Unindent() + + local dataDigital, dataAnalog, showDigital, showAnalog = s.dataDigital, s.dataAnalog, s.showDigital, s.showAnalog + + _, s.paused = ImGui.Checkbox("Pause", s.paused) + _, showDigital[1] = ImGui.Checkbox("digital_0", showDigital[1]); ImGui.SameLine() + _, showDigital[2] = ImGui.Checkbox("digital_1", showDigital[2]); ImGui.SameLine() + _, showDigital[3] = ImGui.Checkbox("digital_2", showDigital[3]); ImGui.SameLine() + _, showAnalog[1] = ImGui.Checkbox("analog_0", showAnalog[1]); ImGui.SameLine() + _, showAnalog[2] = ImGui.Checkbox("analog_1", showAnalog[2]) + + -- [Port] bool -> 0/1 for digital signal values + local function b2n(b) if b then return 1 end return 0 end + if not s.paused then + s.t += ImGui.GetIO().DeltaTime + local t = s.t + if t - s.last_t >= 0.01 then + s.last_t = t + -- Digital signal values + if showDigital[1] then + ScrollingBuffer_AddPoint(dataDigital[1], t, b2n(math.sin(2 * t) > 0.45)) + end + if showDigital[2] then + ScrollingBuffer_AddPoint(dataDigital[2], t, b2n(math.sin(2 * t) < 0.45)) + end + if showDigital[3] then + ScrollingBuffer_AddPoint(dataDigital[3], t, b2n(math.sin(50 * t) > 0.5)) + end + -- Analog signal values + if showAnalog[1] then + ScrollingBuffer_AddPoint(dataAnalog[1], t, math.sin(2 * t)) + end + if showAnalog[2] then + ScrollingBuffer_AddPoint(dataAnalog[2], t, math.cos(2 * t)) + end + end + end + if ImPlot.BeginPlot("##Digital") then + local cond = ImGuiCond.Always + if s.paused then cond = ImGuiCond.Once end + ImPlot.SetupAxisLimits(ImAxis.X1, s.t - 10.0, s.t, cond) + ImPlot.SetupAxisLimits(ImAxis.Y1, -1, 1) + for i = 1, 3 do + local d = dataDigital[i] + if showDigital[i] and #d.X > 0 then + ImPlot.PlotDigital(string.format("digital_%d", i - 1), d.X, d.Y, #d.X, { + Offset = d.Offset, + Size = i * 4, + }) + end + end + for i = 1, 2 do + local d = dataAnalog[i] + if showAnalog[i] then + if #d.X > 0 then + ImPlot.PlotLine(string.format("analog_%d", i - 1), d.X, d.Y, #d.X, { + Offset = d.Offset, + }) + end + end + end + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + -- [Port] ImVec2/ImVec4 statics -> arrays for SliderFloat2/ColorEdit4 + S.Demo_Images = { bmin = { 0, 0 }, bmax = { 1, 1 }, uv0 = { 0, 0 }, uv1 = { 1, 1 }, tint = { 1, 1, 1, 1 } } + function D.Demo_Images() + IMGUI_DEMO_MARKER("Plots/Images") + local s = S.Demo_Images + ImGui.BulletText("Below we are displaying the font texture, which is the only texture we have\naccess to in this demo.") + ImGui.BulletText("Use the 'ImTextureID' type as storage to pass pointers or identifiers to your\nown texture data.") + ImGui.BulletText("See ImGui Wiki page 'Image Loading and Displaying Examples'.") + ImGui.SliderFloat2("Min", s.bmin, -2, 2, "%.1f") + ImGui.SliderFloat2("Max", s.bmax, -2, 2, "%.1f") + ImGui.SliderFloat2("UV0", s.uv0, -2, 2, "%.1f") + ImGui.SliderFloat2("UV1", s.uv1, -2, 2, "%.1f") + ImGui.ColorEdit4("Tint", s.tint) + if ImPlot.BeginPlot("##image") then + -- We use the font atlas ImTextureRef for this demo, but in your real code when you submit + -- an image that you have loaded yourself, you would normally have a ImTextureID which works + -- just as well (as ImTextureRef can be constructed from ImTextureID). + ImPlot.PlotImage("my image", ImGui.GetIO().Fonts.TexRef, ImPlot.ImPlotPoint(s.bmin[1], s.bmin[2]), ImPlot.ImPlotPoint(s.bmax[1], s.bmax[2]), + ImVec2(s.uv0[1], s.uv0[2]), ImVec2(s.uv1[1], s.uv1[2]), ImVec4(s.tint[1], s.tint[2], s.tint[3], s.tint[4])) + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + + S.Demo_RealtimePlots = { + sdata1 = ScrollingBuffer(), sdata2 = ScrollingBuffer(), + rdata1 = RollingBuffer(), rdata2 = RollingBuffer(), + t = 0, last_t = 0.0, + history = 10.0, + flags = ImPlotAxisFlags.NoTickLabels, + } + function D.Demo_RealtimePlots() + IMGUI_DEMO_MARKER("Plots/Realtime Plots") + local s = S.Demo_RealtimePlots + ImGui.BulletText("Move your mouse to change the data!") + local sdata1, sdata2, rdata1, rdata2 = s.sdata1, s.sdata2, s.rdata1, s.rdata2 + local mouse = ImGui.GetMousePos() + + -- Add points to the buffers every 0.02 seconds + if s.t == 0 or s.t - s.last_t >= 0.02 then + ScrollingBuffer_AddPoint(sdata1, s.t, mouse.x * 0.0005) + RollingBuffer_AddPoint(rdata1, s.t, mouse.x * 0.0005) + ScrollingBuffer_AddPoint(sdata2, s.t, mouse.y * 0.0005) + RollingBuffer_AddPoint(rdata2, s.t, mouse.y * 0.0005) + s.last_t = s.t + end + s.t += ImGui.GetIO().DeltaTime + local t = s.t + + s.history = ImGui.SliderFloat("History", s.history, 1, 30, "%.1f s") + local history = s.history + rdata1.Span = history + rdata2.Span = history + + local flags = s.flags + + if ImPlot.BeginPlot("##Scrolling", ImVec2(-1, ImGui.GetTextLineHeight() * 10)) then + ImPlot.SetupAxes(nil, nil, flags, flags) + ImPlot.SetupAxisLimits(ImAxis.X1, t - history, t, ImGuiCond.Always) + ImPlot.SetupAxisLimits(ImAxis.Y1, 0, 1) + local spec = ImPlot.ImPlotSpec() + spec.Offset = sdata1.Offset + spec.FillAlpha = 0.5 + ImPlot.PlotShaded("Mouse X", sdata1.X, sdata1.Y, #sdata1.X, -math.huge, spec) + spec.Offset = sdata2.Offset + ImPlot.PlotLine("Mouse Y", sdata2.X, sdata2.Y, #sdata2.X, spec) + ImPlot.EndPlot() + end + if ImPlot.BeginPlot("##Rolling", ImVec2(-1, ImGui.GetTextLineHeight() * 10)) then + ImPlot.SetupAxes(nil, nil, flags, flags) + ImPlot.SetupAxisLimits(ImAxis.X1, 0, history, ImGuiCond.Always) + ImPlot.SetupAxisLimits(ImAxis.Y1, 0, 1) + local spec = ImPlot.ImPlotSpec() + spec.Offset = 0 + ImPlot.PlotLine("Mouse X", rdata1.X, rdata1.Y, #rdata1.X, spec) + ImPlot.PlotLine("Mouse Y", rdata2.X, rdata2.Y, #rdata2.X, spec) + ImPlot.EndPlot() + end + end + + --------------------------------------------------------------------------- + -- [Port] TODO: the remaining demos are not ported yet; stubs show a placeholder + --------------------------------------------------------------------------- + + for _, name in { + "MarkersAndText", "NaNValues", "PerIndexColors", "LogScale", "SymmetricLogScale", "TimeScale", "CustomScale", + "MultipleAxes", "TickLabels", "LinkedAxes", "AxisConstraints", "EqualAxes", "AutoFittingData", "SubplotsSizing", + "SubplotItemSharing", "SubplotAxisLinking", "Tables", "LegendOptions", "LegendPopups", "DragPoints", "DragLines", + "DragRects", "Querying", "Annotations", "Tags", "DragAndDrop", "ColormapWidgets", "ItemStylingAndSpec", + "OffsetAndStride", "CustomStyles", "CustomDataAndGetters", "CustomRendering", "CustomPlottersAndTooltips", + } do + if D["Demo_" .. name] == nil then + D["Demo_" .. name] = function() + ImGui.TextDisabled("[Port] TODO: Demo_" .. name) + end + end + end + + --------------------------------------------------------------------------- + -- DEMO WINDOW + --------------------------------------------------------------------------- + + local function DemoHeader(label, demo) + if ImGui.TreeNodeEx(label) then + demo() + ImGui.TreePop() + end + end + + S.ShowDemoWindow = { + show_implot_metrics = false, + show_implot_style_editor = false, + show_imgui_metrics = false, + show_imgui_style_editor = false, + show_imgui_demo = false, + } + + -- [Port] p_open: true/false/nil (nil = no close button). Returns the new p_open value. + function ImPlot.ShowDemoWindow(p_open) + local s = S.ShowDemoWindow + + if s.show_implot_metrics then + s.show_implot_metrics = ImPlot.ShowMetricsWindow(s.show_implot_metrics) + end + if s.show_implot_style_editor then + ImGui.SetNextWindowSize(ImVec2(415, 762), ImGuiCond.Appearing) + s.show_implot_style_editor = ImGui.Begin("Style Editor (ImPlot)", s.show_implot_style_editor) + ImPlot.ShowStyleEditor() + ImGui.End() + end + if s.show_imgui_style_editor then + s.show_imgui_style_editor = ImGui.Begin("Style Editor (ImGui)", s.show_imgui_style_editor) + ImGui.ShowStyleEditor() + ImGui.End() + end + if s.show_imgui_metrics then + s.show_imgui_metrics = ImGui.ShowMetricsWindow(s.show_imgui_metrics) + end + if s.show_imgui_demo then + s.show_imgui_demo = ImGui.ShowDemoWindow(s.show_imgui_demo) + end + ImGui.SetNextWindowPos(ImVec2(50, 50), ImGuiCond.FirstUseEver) + ImGui.SetNextWindowSize(ImVec2(600, 750), ImGuiCond.FirstUseEver) + p_open = ImGui.Begin("ImPlot Demo", p_open, G.ImGuiWindowFlags.MenuBar) + if ImGui.BeginMenuBar() then + if ImGui.BeginMenu("Tools") then + _, s.show_implot_metrics = ImGui.MenuItem("Metrics", nil, s.show_implot_metrics) + _, s.show_implot_style_editor = ImGui.MenuItem("Style Editor", nil, s.show_implot_style_editor) + ImGui.Separator() + _, s.show_imgui_metrics = ImGui.MenuItem("ImGui Metrics", nil, s.show_imgui_metrics) + _, s.show_imgui_style_editor = ImGui.MenuItem("ImGui Style Editor", nil, s.show_imgui_style_editor) + _, s.show_imgui_demo = ImGui.MenuItem("ImGui Demo", nil, s.show_imgui_demo) + ImGui.EndMenu() + end + ImGui.EndMenuBar() + end + ------------------------------------------------------------------------- + ImGui.Text("ImPlot says hello! (%s)", "Luau port") -- [Port] no IMPLOT_VERSION/IMPLOT_VERSION_NUM + -- display warning about 16-bit indices + -- [Port] ImDrawIdx is 32-bit in the core: the warning never shows. + + ImGui.Spacing() + + if ImGui.BeginTabBar("ImPlotDemoTabs") then + if ImGui.BeginTabItem("Plots") then + DemoHeader("Line Plots", D.Demo_LinePlots) + DemoHeader("Filled Line Plots", D.Demo_FilledLinePlots) + DemoHeader("Shaded Plots##", D.Demo_ShadedPlots) + DemoHeader("Scatter Plots", D.Demo_ScatterPlots) + DemoHeader("Bubble Plots", D.Demo_BubblePlots) + DemoHeader("Polygon Plots", D.Demo_PolygonPlots) + DemoHeader("Realtime Plots", D.Demo_RealtimePlots) + DemoHeader("Stairstep Plots", D.Demo_StairstepPlots) + DemoHeader("Bar Plots", D.Demo_BarPlots) + DemoHeader("Bar Groups", D.Demo_BarGroups) + DemoHeader("Bar Stacks", D.Demo_BarStacks) + DemoHeader("Error Bars", D.Demo_ErrorBars) + DemoHeader("Stem Plots##", D.Demo_StemPlots) + DemoHeader("Infinite Lines", D.Demo_InfiniteLines) + DemoHeader("Pie Charts", D.Demo_PieCharts) + DemoHeader("Heatmaps", D.Demo_Heatmaps) + DemoHeader("Histogram", D.Demo_Histogram) + DemoHeader("Histogram 2D", D.Demo_Histogram2D) + DemoHeader("Digital Plots", D.Demo_DigitalPlots) + DemoHeader("Images", D.Demo_Images) + DemoHeader("Markers and Text", D.Demo_MarkersAndText) + DemoHeader("NaN Values", D.Demo_NaNValues) + DemoHeader("Per-Index Colors", D.Demo_PerIndexColors) + ImGui.EndTabItem() + end + if ImGui.BeginTabItem("Subplots") then + DemoHeader("Sizing", D.Demo_SubplotsSizing) + DemoHeader("Item Sharing", D.Demo_SubplotItemSharing) + DemoHeader("Axis Linking", D.Demo_SubplotAxisLinking) + DemoHeader("Tables", D.Demo_Tables) + ImGui.EndTabItem() + end + if ImGui.BeginTabItem("Axes") then + DemoHeader("Log Scale", D.Demo_LogScale) + DemoHeader("Symmetric Log Scale", D.Demo_SymmetricLogScale) + DemoHeader("Time Scale", D.Demo_TimeScale) + DemoHeader("Custom Scale", D.Demo_CustomScale) + DemoHeader("Multiple Axes", D.Demo_MultipleAxes) + DemoHeader("Tick Labels", D.Demo_TickLabels) + DemoHeader("Linked Axes", D.Demo_LinkedAxes) + DemoHeader("Axis Constraints", D.Demo_AxisConstraints) + DemoHeader("Equal Axes", D.Demo_EqualAxes) + DemoHeader("Auto-Fitting Data", D.Demo_AutoFittingData) + ImGui.EndTabItem() + end + if ImGui.BeginTabItem("Tools") then + DemoHeader("Item Styling and Spec", D.Demo_ItemStylingAndSpec) + DemoHeader("Offset and Stride", D.Demo_OffsetAndStride) + DemoHeader("Drag Points", D.Demo_DragPoints) + DemoHeader("Drag Lines", D.Demo_DragLines) + DemoHeader("Drag Rects", D.Demo_DragRects) + DemoHeader("Querying", D.Demo_Querying) + DemoHeader("Annotations", D.Demo_Annotations) + DemoHeader("Tags", D.Demo_Tags) + DemoHeader("Drag and Drop", D.Demo_DragAndDrop) + DemoHeader("Legend Options", D.Demo_LegendOptions) + DemoHeader("Legend Popups", D.Demo_LegendPopups) + DemoHeader("Colormap Widgets", D.Demo_ColormapWidgets) + ImGui.EndTabItem() + end + if ImGui.BeginTabItem("Custom") then + DemoHeader("Custom Styles", D.Demo_CustomStyles) + DemoHeader("Custom Data and Getters", D.Demo_CustomDataAndGetters) + DemoHeader("Custom Rendering", D.Demo_CustomRendering) + DemoHeader("Custom Plotters and Tooltips", D.Demo_CustomPlottersAndTooltips) + ImGui.EndTabItem() + end + if ImGui.BeginTabItem("Config") then + D.Demo_Config() + ImGui.EndTabItem() + end + if ImGui.BeginTabItem("Help") then + D.Demo_Help() + ImGui.EndTabItem() + end + ImGui.EndTabBar() + end + ImGui.End() + return p_open + end + + return ImPlot.ShowDemoWindow +end diff --git a/default.project.json b/default.project.json index fc2fa0f..31c7e49 100644 --- a/default.project.json +++ b/default.project.json @@ -4,7 +4,8 @@ "$className": "DataModel", "ReplicatedStorage": { "ImGui": { "$path": "tools/core/build/ImGuiTyped.luau", "Impl": { "$path": "tools/core/build/ImGui.luau" } }, - "ImPlot": { "$path": "ImPlot.lua" } + "ImPlot": { "$path": "ImPlot.lua" }, + "ImPlotDemo": { "$path": "ImPlotDemo.lua" } }, "StarterPlayer": { "StarterPlayerScripts": { "ImPlotExample": { "$path": "examples/roblox/main.client.luau" } } } } diff --git a/tests/t_demo.luau b/tests/t_demo.luau new file mode 100644 index 0000000..7d339ab --- /dev/null +++ b/tests/t_demo.luau @@ -0,0 +1,61 @@ +local RS = game:GetService("ReplicatedStorage") +local G = require(RS:WaitForChild("ImGui")) +local ImPlot = require(RS:WaitForChild("ImPlot"))(G) +require(RS:WaitForChild("ImPlotDemo"))(G, ImPlot) +local ImGui = G.ImGui + +ImGui.CreateContext() +G.ImGui_ImplRoblox.Init() +G.ImGui_ImplRoblox.SetRenderRate(0) +ImPlot.CreateContext() +local io = ImGui.GetIO() + +-- force every DemoHeader open, and select one tab per phase +local TreeNodeEx, BeginTabItem = ImGui.TreeNodeEx, ImGui.BeginTabItem +ImGui.TreeNodeEx = function(label, ...) + ImGui.SetNextItemOpen(true) + return TreeNodeEx(label, ...) +end +local tabs = { "Plots", "Subplots", "Axes", "Tools", "Custom", "Config", "Help" } +local tab, seen = 1, {} +ImGui.BeginTabItem = function(label, p_open, flags) + if label == tabs[tab] then flags = bit32.bor(flags or 0, G.ImGuiTabItemFlags.SetSelected) end + local r = BeginTabItem(label, p_open, flags) + if r then seen[label] = true end + return r +end + +local BeginPlot, plotted = ImPlot.BeginPlot, {} +ImPlot.BeginPlot = function(title, ...) + local r = BeginPlot(title, ...) + if r then plotted[title] = true end + return r +end + +local frame, done = 0, false +game:GetService("RunService").Stepped:Connect(function() + if done then return end + frame += 1 + tab = math.floor((frame - 1) / 14) % #tabs + 1 + G.ImGui_ImplRoblox.NewFrame() + io.DeltaTime = 1 / 60 + io.DisplaySize = G.ImVec2(1000, 30000) + ImGui.NewFrame() + -- tall window so every open demo is visible (clipped plots return false from BeginPlot) + if frame > 1 then ImGui.SetWindowSize("ImPlot Demo", G.ImVec2(900, 30000)) end + assert(ImPlot.ShowDemoWindow(true) == true) + ImGui.Render() + G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData()) + if frame == 100 then + done = true + for _, t in tabs do assert(seen[t], "tab never opened: " .. t) end + -- every ported Plots/Config demo submitted its plot + for _, p in { "Line Plots", "Stock Prices", "Shaded Plots", "Scatter Plot", "Bubble Plot", "##Scrolling", "##Rolling", + "Stairstep Plot", "Bar Plot", "Bar Group", "PolitiFact: Who Lies More?", "##ErrorBars", "Stem Plots", "##Infinite", + "##Pie1", "##Pie2", "##Heatmap1", "##Heatmap2", "##Histograms", "##Hist2D", "##Digital", "##image", "Preview" } do + assert(plotted[p], "plot never began: " .. p) + end + ImPlot.DestroyContext() + print("OK") + end +end) diff --git a/tools/test.sh b/tools/test.sh index 40ac305..1e6b086 100755 --- a/tools/test.sh +++ b/tools/test.sh @@ -3,7 +3,7 @@ cd "$(dirname "$0")/.." fail=0 for t in tests/t_*.luau; do - out=$(python3 tools/core/tools/test/run.py --module ImPlot=ImPlot.lua --main "$t" --quiet --roblox 2>&1) + out=$(python3 tools/core/tools/test/run.py --module ImPlot=ImPlot.lua --module ImPlotDemo=ImPlotDemo.lua --main "$t" --quiet --roblox 2>&1) if echo "$out" | grep -q "^OK"; then echo "PASS $t"; else echo "FAIL $t"; echo "$out" | tail -20; fail=1; fi done exit $fail