]> git.scottworley.com Git - batteryviewer/blame - chart.c
Track min & max coordinates
[batteryviewer] / chart.c
CommitLineData
5a945372
SW
1/*
2 * h/t https://ptomato.name/advanced-gtk-techniques/html/custom-container.html
3 * for examples of how to make GTK widgets
4 */
5
6#include "chart.h"
148e0e28 7#include <float.h>
5a945372
SW
8#include <gtk/gtk.h>
9
10#define BV_CHART_PRIVATE(obj) \
11 (G_TYPE_INSTANCE_GET_PRIVATE((obj), BV_CHART_TYPE, BVChartPrivate))
12
13typedef struct _BVChartPrivate BVChartPrivate;
14
15struct _BVChartPrivate {
27db65e2 16 GArray *points;
148e0e28 17 float minx, miny, maxx, maxy;
27db65e2
SW
18};
19
20struct BVChartPoint {
21 float x, y;
5a945372
SW
22};
23
24G_DEFINE_TYPE_WITH_CODE(BVChart, bv_chart, GTK_TYPE_DRAWING_AREA,
25 G_ADD_PRIVATE(BVChart))
26
27static void bv_chart_class_init(BVChartClass *klass __attribute__((unused))) {}
28
29static void bv_chart_init(BVChart *chart) {
30 gtk_widget_set_has_window(GTK_WIDGET(chart), FALSE);
27db65e2
SW
31 BVChartPrivate *priv = bv_chart_get_instance_private(chart);
32 gboolean zero_terminated = FALSE;
33 gboolean clear_ = FALSE;
34 priv->points =
35 g_array_new(zero_terminated, clear_, sizeof(struct BVChartPoint));
148e0e28
SW
36 priv->minx = FLT_MAX;
37 priv->miny = FLT_MAX;
38 priv->maxx = FLT_MIN;
39 priv->maxy = FLT_MIN;
5a945372
SW
40}
41
42GtkWidget *bv_chart_new() {
43 return GTK_WIDGET(g_object_new(bv_chart_get_type(), NULL));
44}
27db65e2
SW
45
46void bv_chart_add_point(BVChart *chart, float x, float y) {
47 BVChartPrivate *priv = bv_chart_get_instance_private(chart);
148e0e28
SW
48 if (x < priv->minx)
49 priv->minx = x;
50 if (y < priv->miny)
51 priv->miny = y;
52 if (x > priv->maxx)
53 priv->maxx = x;
54 if (y > priv->maxy)
55 priv->maxy = y;
27db65e2
SW
56 struct BVChartPoint p = {.x = x, .y = y};
57 g_array_append_val(priv->points, p);
58}