Scrolling multi-series history graph (60s window, cur/min/max/avg legend), per-core mini-graph grid, dual up/down network graph with rates + totals, temperature history with threshold line + over-temp colouring. Header-only C++, demo mains, examples, Xvfb screenshots.
302 lines
9.3 KiB
C++
302 lines
9.3 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// 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<double> data;
|
|
};
|
|
|
|
std::vector<Series> 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<double>& 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);
|
|
|
|
const int title_h = 20;
|
|
const int legend_h = 18 * (int)series_.size();
|
|
const int axis_w = 44;
|
|
const int axis_h = 16;
|
|
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.
|
|
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
|
|
|
// Y range.
|
|
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;
|
|
|
|
auto y_at = [&](double v) {
|
|
return py + ph - (int)((v - lo) / (hi - lo) * ph);
|
|
};
|
|
|
|
// Grid + y labels.
|
|
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)
|
|
{
|
|
double v = lo + (hi - lo) * (4 - g) / 4.0;
|
|
draw_text_color(px - 40, py + ph * g / 4 + 4, fmt1(v), Srgb{100, 100, 100});
|
|
}
|
|
// Time labels.
|
|
draw_text_color(px, y2_ - 3, "-" + std::to_string(window_seconds_) + "s", Srgb{100, 100, 100});
|
|
std::string now_s = "now";
|
|
draw_text_color(px + pw - xf::text_width(st, now_s), y2_ - 3, now_s, Srgb{100, 100, 100});
|
|
|
|
// Series lines.
|
|
for (auto& s : series_)
|
|
{
|
|
if (s.data.size() < 2) continue;
|
|
std::vector<XPoint> 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.
|
|
int ly = y1_ + title_h + 4;
|
|
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();
|
|
}
|
|
XSetForeground(display_, gc_, col(s.color));
|
|
XFillRectangle(display_, window_, gc_, x1_ + 8, ly - 2, 18, 4);
|
|
std::string leg = s.name + " cur " + fmt1(cur) + " min " + fmt1(mn) +
|
|
" max " + fmt1(mx) + " avg " + fmt1(sum);
|
|
xf::draw_text_at(display_, window_, gc_, st, x1_ + 32, ly + 8, leg);
|
|
ly += 18;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
};
|