# HG changeset patch # User Alys Brooks # Date 2022-01-08 19:46:06 # Node ID fcb79fc7fb1dac8226c3893332d59284bec54891 # Parent 7dba0456323a955d81e97566f1ede66c07ee07ad Handle multiple series scalably. diff --git a/isometric-park-fna/UI/Graph.cs b/isometric-park-fna/UI/Graph.cs --- a/isometric-park-fna/UI/Graph.cs +++ b/isometric-park-fna/UI/Graph.cs @@ -3,6 +3,7 @@ using Num = System.Numerics; using System.Linq; using System.Runtime.InteropServices; +using System.Collections.Generic; using ImGuiNET; using ImPlotNET; @@ -25,6 +26,14 @@ public static bool show_subsidies = false; public static bool show_upkeep = false; + + public static Dictionary> data_sets = new Dictionary>(); + + public static Dictionary data_sets_show = new Dictionary { + {"Total Funds", false}, + {"Subsidies", false}, + {"Upkeep", false}}; + public static void Render(ImFontPtr font, Simulation sim, ImGuiWindowBridgeEngine engine) { bool newShow = true; @@ -48,38 +57,38 @@ } GraphWindow.hadFocus = ImGui.IsWindowFocused(); - // ImGui.Text("Lorem Ipsum"); - - var totals = sim.allBudgets().Select(b => (double)b.money); - var subsidies = sim.allBudgets().Select(b => (double)b.subsidy); - var upkeep = sim.allBudgets().Select(b => (double)b.upkeep); - - var totals_array = totals.ToArray(); - var subsidies_array = subsidies.ToArray(); - var upkeep_array = upkeep.ToArray(); + data_sets["Total Funds"] = sim.allBudgets().Select(b => (double)b.money); + data_sets["Subsidies"] = sim.allBudgets().Select(b => (double)b.subsidy); + data_sets["Upkeep"] = sim.allBudgets().Select(b => (double)b.upkeep); var periods = 12.0d * GraphWindow.year; + var keys = data_sets_show.Keys.ToList(); - // totals.Max() * 1.10d; - ImPlot.SetNextPlotLimits(totals.Count()-periods, totals.Count(), 0.0d, (totals.Count() > 0 )? totals.Max() * 1.05 : 115_000d , ImGuiCond.Always); + var totals = data_sets["Total Funds"]; + + var max = 0.0d; + foreach( var key in keys) { + if (data_sets_show[key] && totals.Count() > 0) { + var series_max = data_sets[key].Max() * 1.10f; + max = Math.Max(series_max, max); + } + } + + ImPlot.SetNextPlotLimits(totals.Count()-periods, totals.Count(), 0.0d, max , ImGuiCond.Always); if( ImPlot.BeginPlot("My Plot")) { // Span totals = new Span(new double[]{0.0, 1.0, 2.0, 9.0}); - if (totals_array.Length > 0 && show_totals) - { - ImPlot.PlotLine("Total Funds", ref totals_array[0], totals_array.Length); - } - if (subsidies_array.Length > 0 && show_subsidies) - { - ImPlot.PlotLine("Subsidies", ref subsidies_array[0], subsidies_array.Length); - } - if (upkeep_array.Length > 0 && show_upkeep) - { - ImPlot.PlotLine("Upkeep", ref upkeep_array[0], upkeep_array.Length); + foreach (var key in keys) { + var show = data_sets_show[key]; + var data = data_sets[key]; + var data_array = data_sets[key].ToArray(); + if (data_array.Length > 0 && show) + { + ImPlot.PlotLine(key, ref data_array[0], data_array.Length); + } } ImPlot.EndPlot(); - } ImGui.RadioButton("1 year", ref GraphWindow.year, 1); @@ -90,13 +99,18 @@ ImGui.SameLine(); ImGui.RadioButton("100 years", ref GraphWindow.year, 100); - if (Menu.activeButton("Totals", show_totals, StyleSets.selected, StyleSets.white)) + foreach (var key in keys) { - GraphWindow.show_totals = !GraphWindow.show_totals; + if (Menu.activeButton(key, data_sets_show[key], StyleSets.selected, StyleSets.white)) + { + data_sets_show[key] = !data_sets_show[key]; + } + } + ImGui.End(); ImGui.GetStyle().WindowMenuButtonPosition = ImGuiDir.Left; StyleSets.defaultSet.pop();