temp-monitor/include/temperature_chart.h
2026-01-11 13:01:53 +01:00

87 lines
2.4 KiB
C++

#ifndef TEMPERATURE_CHART_H
#define TEMPERATURE_CHART_H
#include <gtk/gtk.h>
#include <cairo.h>
#include <string>
#include <vector>
#include <map>
#include <cstdint>
struct DataPoint {
double temperature;
int64_t timestamp;
};
struct SeriesData {
std::vector<DataPoint> points;
GdkRGBA color;
std::string id; // Unique internal ID (device + sensor)
std::string name; // User-friendly display name
};
class TemperatureChart {
public:
TemperatureChart();
~TemperatureChart();
GtkWidget* getWidget() const { return drawingArea; }
bool addTemperatureData(const std::string &device, const std::string &sensor,
double temperature, int64_t timestamp);
void clear();
void draw();
// Get list of series with their colors
std::vector<std::pair<std::string, GdkRGBA>> getSeriesColors() const;
void setSeriesColor(const std::string &seriesName, const GdkRGBA &color);
// Name management
std::string getSeriesName(const std::string &seriesId) const;
void setSeriesName(const std::string &seriesId, const std::string &name);
void drawChart(GtkDrawingArea *area, cairo_t *cr, int width, int height);
private:
void setupColors();
GdkRGBA getColorForSeries(const std::string &seriesKey);
void redraw();
void updateThemeColors();
static void onLeave(GtkEventControllerMotion *motion, gpointer userData);
struct NearestPoint {
bool found;
double temperature;
int64_t timestamp;
std::string seriesName;
double distance;
};
NearestPoint findNearestDataPoint(double mouseX, double mouseY, int width, int height);
void showTooltip(double x, double y, const NearestPoint &point);
void hideTooltip();
GtkWidget *drawingArea;
GtkWidget *tooltipWindow;
GtkWidget *tooltipLabel;
std::map<std::string, SeriesData> seriesMap;
std::map<std::string, GdkRGBA> colorMap;
int maxDataPoints;
guint tickHandler;
double lastMouseX, lastMouseY;
// Cairo drawing state
double minTemp, maxTemp;
int64_t minTime, maxTime;
static constexpr int MAX_TIME_MS = 600000; // 10 minutes in milliseconds
static constexpr double MIN_TEMP = 10.0;
static constexpr double MAX_TEMP = 110.0;
// Theme colors
GdkRGBA bgColor, textColor, gridColor, axisColor;
};
#endif // TEMPERATURE_CHART_H