#pragma once #include "core/xframe_core.h" #include "../InstData.h" #include #include #include #include #include // region XHistoryGraph // // A scrolling multi-series time-series line graph in the XFrame format: // push samples per series, keep a fixed time window (60 s default), draw // each series in its own colour with a legend showing current / min / // max / average. Same interaction model as XGraph, raw X11 only. // class XHistoryGraph { private: Display* display_; int screen_; Window window_; GC gc_; SStyle_Manager* styles_; int x1_, y1_, x2_, y2_; std::string name_; std::string text_; std::string style_name_; bool visible_ = true; bool enabled_ = true; bool focused_ = false; struct Series { std::string name; Srgb color{0, 150, 60}; std::vector data; }; std::vector series_; int window_seconds_ = 60; // fixed time window shown int capacity_ = 120; // samples kept per series (2 Hz) double y_min_ = 0.0; // fixed floor; set_y_range overrides double y_max_ = 0.0; // 0 = auto from data bool auto_y_ = true; Srgb white_color_{255, 255, 255}; Srgb grid_color_{225, 225, 225}; Srgb axis_color_{150, 150, 150}; unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); } static std::string fmt1(double v) { char buf[32]; std::snprintf(buf, sizeof(buf), "%.1f", v); return buf; } void draw_text_color(int x, int baseline_y, const std::string& s, Srgb color) { SStyle st = style(); if (!st.font_ || s.empty()) return; XSetFont(display_, gc_, st.font_->fid); XSetForeground(display_, gc_, col(color)); XDrawString(display_, window_, gc_, x, baseline_y, s.c_str(), (int)s.size()); } public: XHistoryGraph(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) { display_ = display; screen_ = screen; window_ = window; gc_ = gc; styles_ = styles; x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height; name_ = "XHistoryGraph"; text_ = "History"; // Preload a demo: two seeded series (CPU-ish sine + noise, MEM-ish drift). add_series("CPU", Srgb{0, 170, 70}); add_series("MEM", Srgb{0, 100, 200}); unsigned int seed = 987654321; auto rnd = [&]() { seed = seed * 1103515245 + 12345; return ((seed >> 16) & 0x7fff) / 32767.0; }; for (int i = 0; i < capacity_; ++i) { double t = (double)i / capacity_; push(0, 55.0 + 30.0 * std::sin(t * 9.0) + (rnd() - 0.5) * 18.0); push(1, 38.0 + 14.0 * std::sin(t * 4.0 + 1.0) + (rnd() - 0.5) * 6.0); } } ~XHistoryGraph() {} // region Standard plumbing void set_bounds(int x, int y, int width, int height) { x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height; } int width() { return x2_ - x1_; } int height() { return y2_ - y1_; } bool contains(int x, int y) { return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; } void set_visible(bool v) { visible_ = v; } bool get_visible() { return visible_; } void set_enabled(bool e) { enabled_ = e; } bool get_enabled() { return enabled_; } void set_style_name(std::string s) { style_name_ = s; } void set_text(std::string t) { text_ = t; } std::string get_text() { return text_; } SStyle style() { return styles_->resolve(style_name_); } // endregion // region Series API void add_series(const std::string& name, Srgb color) { Series s; s.name = name; s.color = color; series_.push_back(s); } void clear_series() { series_.clear(); } int series_count() { return (int)series_.size(); } void set_window_seconds(int seconds) { window_seconds_ = seconds > 0 ? seconds : 60; capacity_ = window_seconds_ * 2; for (auto& s : series_) while ((int)s.data.size() > capacity_) s.data.erase(s.data.begin()); } int get_window_seconds() { return window_seconds_; } void set_y_range(double lo, double hi) { auto_y_ = false; y_min_ = lo; y_max_ = hi; } void set_auto_y(bool on) { auto_y_ = on; } void push(int series, double value) { if (series < 0 || series >= (int)series_.size()) return; series_[series].data.push_back(value); while ((int)series_[series].data.size() > capacity_) series_[series].data.erase(series_[series].data.begin()); } void push_all(const std::vector& values) { for (size_t i = 0; i < values.size() && i < series_.size(); ++i) push((int)i, values[i]); } double last_value(int series) { if (series < 0 || series >= (int)series_.size() || series_[series].data.empty()) return 0.0; return series_[series].data.back(); } // endregion void draw() { if (!visible_) return; SStyle st = style(); int w = width(); int h = height(); XSetForeground(display_, gc_, col(white_color_)); XFillRectangle(display_, window_, gc_, x1_, y1_, w, h); // Scaled fonts (Dec 5, 2026 proportions audit): title 16 px bold, // Y labels + legend 14-16 px, time labels 13-14 px, all derived // from widget height (min 11 px). Fall back to the style font // when a scaled font fails to load. int title_px = xframe::clamp_font_px(0.033 * h, 11, 16); int body_px = xframe::clamp_font_px(0.032 * h, 11, 16); int time_px = xframe::clamp_font_px(0.028 * h, 11, 14); XFontStruct* title_font = xframe::scaled_font(display_, title_px, true); if (!title_font) title_font = st.font_; XFontStruct* body_font = xframe::scaled_font(display_, body_px, false); if (!body_font) body_font = st.font_; XFontStruct* time_font = xframe::scaled_font(display_, time_px, false); if (!time_font) time_font = st.font_; int title_asc = title_font ? title_font->ascent : 11; int body_asc = body_font ? body_font->ascent : 11; const int title_h = title_px + 8; const int axis_h = 16; // Y range first: the widest Y label sets the left margin. double lo = y_min_; double hi = y_max_; if (auto_y_) { lo = 0.0; hi = 1.0; for (auto& s : series_) for (double v : s.data) hi = std::max(hi, v); hi *= 1.05; } if (hi <= lo) hi = lo + 1.0; // Dynamic left margin: measure the widest of the 5 Y labels and // put the plot's left edge after it (labels right-aligned, // ending 4 px left of the plot). The old fixed 44 px margin let // labels wider than 40 px run over the axis line. std::string ylabels[5]; int axis_w = 8; for (int g = 0; g <= 4; ++g) { double v = lo + (hi - lo) * (4 - g) / 4.0; ylabels[g] = fmt1(v); axis_w = std::max(axis_w, xframe::font_text_width(body_font, ylabels[g]) + 8); } // Legend lines first: a series whose full line would run past // the right edge wraps to two lines ("name cur X" / // "min Y max Z avg W"), so the plot height accounts for it. struct LegLine { std::string text; bool swatch; Srgb color{0, 0, 0}; }; std::vector leg_lines; int leg_avail = std::max(20, x2_ - 8 - (x1_ + 32)); for (auto& s : series_) { double cur = 0.0, mn = 0.0, mx = 0.0, sum = 0.0; if (!s.data.empty()) { cur = s.data.back(); mn = mx = s.data[0]; for (double v : s.data) { mn = std::min(mn, v); mx = std::max(mx, v); sum += v; } sum /= (double)s.data.size(); } std::string full = s.name + " cur " + fmt1(cur) + " min " + fmt1(mn) + " max " + fmt1(mx) + " avg " + fmt1(sum); if (xframe::font_text_width(body_font, full) <= leg_avail) { leg_lines.push_back(LegLine{full, true, s.color}); } else { std::string l1 = s.name + " cur " + fmt1(cur); std::string l2 = "min " + fmt1(mn) + " max " + fmt1(mx) + " avg " + fmt1(sum); leg_lines.push_back(LegLine{l1, true, s.color}); leg_lines.push_back(LegLine{l2, false, s.color}); } } int line_h = body_px + 8; int legend_h = line_h * (int)leg_lines.size(); int px = x1_ + axis_w; int py = y1_ + title_h + legend_h + 4; int pw = std::max(10, x2_ - 8 - px); int ph = std::max(10, y2_ - axis_h - py); // Title. xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8, y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20})); auto y_at = [&](double v) { return py + ph - (int)((v - lo) / (hi - lo) * ph); }; // Grid + y labels (right-aligned, clear of the plot). XSetForeground(display_, gc_, col(grid_color_)); for (int g = 0; g <= 4; ++g) { int gy = py + ph * g / 4; XDrawLine(display_, window_, gc_, px, gy, px + pw, gy); } for (int g = 0; g <= 4; ++g) { int gy = py + ph * g / 4; int lw = xframe::font_text_width(body_font, ylabels[g]); int ybase = (g == 0) ? gy + body_asc : (g == 4) ? gy - 2 : gy + body_asc / 2; xframe::draw_text_font(display_, window_, gc_, body_font, px - 4 - lw, ybase, ylabels[g], col(Srgb{100, 100, 100})); } // Time labels. std::string ago = "-" + std::to_string(window_seconds_) + "s"; xframe::draw_text_font(display_, window_, gc_, time_font, px, y2_ - 3, ago, col(Srgb{100, 100, 100})); std::string now_s = "now"; xframe::draw_text_font(display_, window_, gc_, time_font, px + pw - xframe::font_text_width(time_font, now_s), y2_ - 3, now_s, col(Srgb{100, 100, 100})); // Series lines. for (auto& s : series_) { if (s.data.size() < 2) continue; std::vector pts; int n = (int)s.data.size(); for (int i = 0; i < n; ++i) { int cx = px + (int)((double)i / (capacity_ - 1) * pw); pts.push_back(XPoint{(short)cx, (short)y_at(s.data[i])}); } XSetForeground(display_, gc_, col(s.color)); xf::set_line_width(display_, gc_, 2); XDrawLines(display_, window_, gc_, pts.data(), (int)pts.size(), CoordModeOrigin); xf::set_line_width(display_, gc_, 1); } // Legend: swatch + name + current / min / max / avg (wrapped // lines computed above so nothing runs past the right edge). int ly = y1_ + title_h + 4; for (auto& ln : leg_lines) { int base = ly + body_asc; if (ln.swatch) { XSetForeground(display_, gc_, col(ln.color)); XFillRectangle(display_, window_, gc_, x1_ + 8, base - body_asc + 2, 18, 4); } xframe::draw_text_font(display_, window_, gc_, body_font, x1_ + 32, base, ln.text, col(Srgb{20, 20, 20})); ly += line_h; } // Plot frame + outer frame. XSetForeground(display_, gc_, col(axis_color_)); XDrawRectangle(display_, window_, gc_, px, py, pw, ph); XSetForeground(display_, gc_, st.lineColor_); XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h); XFlush(display_); } bool on_button_press(int x, int y, unsigned int button) { (void)x; (void)y; (void)button; return false; } bool on_button_release(int x, int y, unsigned int button) { (void)x; (void)y; (void)button; return false; } bool on_motion(int x, int y) { (void)x; (void)y; return false; } bool on_key(KeySym key, const char* str) { (void)key; (void)str; return false; } };