X-Git-Url: http://git.scottworley.com/batteryviewer/blobdiff_plain/27db65e22dd1c085204ebb893edd4d9da8c3b267..HEAD:/chart.c?ds=sidebyside diff --git a/chart.c b/chart.c index 8b05d33..1813aa2 100644 --- a/chart.c +++ b/chart.c @@ -1,9 +1,26 @@ -/* +/* batteryviewer: Display battery metrics + * Copyright (C) 2023 Scott Worley + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * * h/t https://ptomato.name/advanced-gtk-techniques/html/custom-container.html * for examples of how to make GTK widgets */ #include "chart.h" +#include #include #define BV_CHART_PRIVATE(obj) \ @@ -13,16 +30,89 @@ typedef struct _BVChartPrivate BVChartPrivate; struct _BVChartPrivate { GArray *points; + float minx, miny, maxx, maxy; }; struct BVChartPoint { float x, y; }; +struct ScreenPoint { + float x, y; +}; + G_DEFINE_TYPE_WITH_CODE(BVChart, bv_chart, GTK_TYPE_DRAWING_AREA, G_ADD_PRIVATE(BVChart)) -static void bv_chart_class_init(BVChartClass *klass __attribute__((unused))) {} +typedef void (*cairo_draw_func)(cairo_t *cr, double x, double y); + +static struct ScreenPoint to_screen(BVChartPrivate *priv, + GtkAllocation *allocation, + struct BVChartPoint *p) { + int margin = 3; + float xscale = (allocation->width - 2 * margin) / (priv->maxx - priv->minx); + float yscale = (allocation->height - 2 * margin) / (priv->miny - priv->maxy); + struct ScreenPoint screen_p = { + .x = xscale * (p->x - priv->minx) + margin, + .y = yscale * (p->y - priv->maxy) + margin, + }; + return screen_p; +} + +static void draw_data(BVChartPrivate *priv, cairo_t *cr, + GtkAllocation *allocation) { + cairo_set_source_rgb(cr, 0.0, 0.0, 1.0); + float gap_threshold = 3.0; + float prevx = 0.0; + for (guint i = 0; i < priv->points->len; i++) { + struct BVChartPoint *p = + &g_array_index(priv->points, struct BVChartPoint, i); + gboolean is_gap = p->x - prevx > gap_threshold; + cairo_draw_func f = is_gap ? cairo_move_to : cairo_line_to; + struct ScreenPoint screen_p = to_screen(priv, allocation, p); + f(cr, screen_p.x, screen_p.y); + prevx = p->x; + } + cairo_stroke(cr); +} + +static void draw_axis(BVChartPrivate *priv, cairo_t *cr, + GtkAllocation *allocation) { + if (priv->miny < 0.0 && priv->maxy > 0.0) { + cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); + struct BVChartPoint p = {.x = priv->minx, .y = 0.0}; + struct ScreenPoint screen_p = to_screen(priv, allocation, &p); + cairo_move_to(cr, screen_p.x, screen_p.y); + p.x = priv->maxx; + screen_p = to_screen(priv, allocation, &p); + cairo_line_to(cr, screen_p.x, screen_p.y); + cairo_stroke(cr); + } +} + +static gboolean bv_chart_draw(GtkWidget *widget, cairo_t *cr) { + BVChart *chart = BV_CHART(widget); + BVChartPrivate *priv = bv_chart_get_instance_private(chart); + + cairo_set_source_rgb(cr, 1.0, 1.0, 1.0); + cairo_paint(cr); + + if (priv->points->len < 2) + return TRUE; + + GtkAllocation allocation; + gtk_widget_get_allocation(widget, &allocation); + + draw_data(priv, cr, &allocation); + draw_axis(priv, cr, &allocation); + + return TRUE; +} + +static void bv_chart_class_init(BVChartClass *klass) { + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); + widget_class->draw = bv_chart_draw; +} static void bv_chart_init(BVChart *chart) { gtk_widget_set_has_window(GTK_WIDGET(chart), FALSE); @@ -31,6 +121,10 @@ static void bv_chart_init(BVChart *chart) { gboolean clear_ = FALSE; priv->points = g_array_new(zero_terminated, clear_, sizeof(struct BVChartPoint)); + priv->minx = FLT_MAX; + priv->miny = FLT_MAX; + priv->maxx = FLT_MIN; + priv->maxy = FLT_MIN; } GtkWidget *bv_chart_new() { @@ -39,6 +133,14 @@ GtkWidget *bv_chart_new() { void bv_chart_add_point(BVChart *chart, float x, float y) { BVChartPrivate *priv = bv_chart_get_instance_private(chart); + if (x < priv->minx) + priv->minx = x; + if (y < priv->miny) + priv->miny = y; + if (x > priv->maxx) + priv->maxx = x; + if (y > priv->maxy) + priv->maxy = y; struct BVChartPoint p = {.x = x, .y = y}; g_array_append_val(priv->points, p); }