aboutsummaryrefslogtreecommitdiff
path: root/src/statistics/gtk_statistics.c
blob: ab64ca5679d5733a3be2944d5311f26b7316bc39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
     This file is part of GNUnet
     (C) 2011 Christian Grothoff (and other contributing authors)

     GNUnet 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; either version 2, or (at your
     option) any later version.

     GNUnet 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 GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.

*/

/**
 * @file src/statistics/gtk_statistics.c
 * @brief widget to display statistics
 * @author Christian Grothoff
 */
#include "gnunet_gtk.h"
#include "gtk_statistics.h"

#define MAX_HISTORY 1280

/**
 * Number of ticks on the x-axis.
 */
#define XTICKS 4

/**
 * Number of ticks on the y-axis.
 */
#define YTICKS 5

/**
 * Additional distance between text and lines / borders in pixels.
 */
#define BORDER 10.0


/**
 * Information about a value we received.
 */
struct HistoricValue
{
  /**
   * Time when the value was recorded.
   */
  uint64_t x;

  /**
   * A value we recorded.
   */
  uint64_t y;

};

/**
 * Historic information we recorded for a value.
 */
struct ValueHistory
{

  /**
   * ID for value updates.
   */
  char *id;

  /**
   * Label for the subsystem.
   */
  char *label;

  /**
   * Color values (rgb).
   */
  double red, green, blue;

  /**
   * Recent values for this number.
   */
  struct HistoricValue history[MAX_HISTORY];

  /**
   * Last offset we wrote to in history.
   */
  unsigned int last_history_offset;

  /**
   * Number of valid entries in the history.
   */
  unsigned int history_size;
};


struct _GtkStatisticsPrivate
{

  /**
   * Values we plot.
   */
  struct ValueHistory **values;

  /**
   * Size of the 'values' array.
   */
  unsigned int num_values;
};


static gboolean gtk_statistics_draw         (GtkWidget      *widget,
					     cairo_t        *cr);

static void gtk_statistics_finalize          (GObject          *object);


G_DEFINE_TYPE (GtkStatistics, gtk_statistics, GTK_TYPE_WIDGET)


#if GTK_MAJOR_VERSION < 3
static gboolean
statistics_expose (GtkWidget *statistics,
		   GdkEventExpose *event)
{
  cairo_t *cr;

  cr = gdk_cairo_create (statistics->window);
  gtk_statistics_draw (statistics, cr);  
  cairo_destroy (cr);  
  return FALSE;
}
#endif


static void
gtk_statistics_class_init (GtkStatisticsClass *class)
{
  GObjectClass *gobject_class;
  GtkWidgetClass *widget_class;

  gobject_class = (GObjectClass*) class;
  widget_class = (GtkWidgetClass*) class;

  gobject_class->finalize = gtk_statistics_finalize;
#if GTK_MAJOR_VERSION >= 3
  widget_class->draw = gtk_statistics_draw;
#endif
  
  g_type_class_add_private (class, sizeof (GtkStatisticsPrivate));
}


static void
gtk_statistics_init (GtkStatistics *statistics)
{
  GtkStatisticsPrivate *priv;

  statistics->priv = G_TYPE_INSTANCE_GET_PRIVATE (statistics,
                                             GTK_TYPE_STATISTICS,
                                             GtkStatisticsPrivate);
  priv = statistics->priv;
#if GTK_MAJOR_VERSION < 3
  g_signal_connect (statistics, "expose-event", G_CALLBACK (statistics_expose), statistics);
#endif
  gtk_widget_set_has_window (GTK_WIDGET (statistics), FALSE);
  priv->values = NULL;
  priv->num_values = 0;
}


/**
 * gtk_statistics_new:
 *
 * Creates a new #GtkStatistics widget.
 *
 * Returns: the new #GtkStatistics widget.
 */
GtkWidget*
gtk_statistics_new ()
{
  GtkStatisticsPrivate *priv;
  GtkStatistics *statistics;

  statistics = g_object_new (GTK_TYPE_STATISTICS, NULL);

  priv = statistics->priv;
  priv->values = NULL;
  priv->num_values = 0;

  return GTK_WIDGET (statistics);
}


/**
 * Add another data series to plot by the statistics widget.
 *
 * @param statistics widget to modify
 * @param id identifier for the series
 * @param label label to use
 * @param color_name name of the color to use
 */
void
gtk_statistics_add_line (GtkStatistics *statistics,
			 const char *id,
			 const char *label,
			 const char *color_name)
{
  GtkStatisticsPrivate *priv;
  struct ValueHistory *vh;
  GdkColor c;

  g_return_if_fail (GTK_IS_STATISTICS (statistics));
  g_return_if_fail (gdk_color_parse (color_name, &c));
  priv = statistics->priv;
  priv->values = g_realloc (priv->values,
			    sizeof (struct ValueHistory*) * (1 + priv->num_values));
  priv->values[priv->num_values++] = vh = g_malloc (sizeof (struct ValueHistory));
  vh->id = strdup (id);
  vh->label = strdup (label);
  vh->red = c.red / 65535.0;
  vh->green = c.green / 65535.0;
  vh->blue = c.blue / 65535.0;
}


/**
 * Add another value to a data series.
 * 
 * @param statistics widget to update
 * @param id identifier of the series
 * @param x new x-value
 * @param y new y-value
 */
void
gtk_statistics_update_value (GtkStatistics      *statistics,
			     const char *id,
			     uint64_t x,
			     uint64_t y)
{
  GtkStatisticsPrivate *priv;
  GtkWidget *widget;
  struct ValueHistory *vh;
  unsigned int i;

  g_return_if_fail (GTK_IS_STATISTICS (statistics));
  priv = statistics->priv;
  for (i=0;i<priv->num_values;i++)
  {
    vh = priv->values[i];
    if (0 != strcmp (id, vh->id))
      continue;

    if (++vh->last_history_offset == MAX_HISTORY)
      vh->last_history_offset = 0;
    if (vh->history_size < MAX_HISTORY)
      vh->history_size++;
    vh->history[vh->last_history_offset].x = x;
    vh->history[vh->last_history_offset].y = y;  
    widget = GTK_WIDGET (statistics);
    if (gtk_widget_is_drawable (widget))
      gtk_widget_queue_draw (widget);
  }
}


/**
 * Convert a number to a nice label for the axis.
 *
 * @param num number to convert
 * @param label where to store the string (must be big enough!)
 */
static void
num_to_label (unsigned long long num,
	      char *label)
{
  if (num > 1000LL * 1000 * 1000 * 1000 * 3)
    sprintf (label, "%llu t", num / 1000LL / 1000LL / 1000LL / 1000LL);
  else if (num > 1000LL * 1000 * 1000 * 3)
    sprintf (label, "%llu g", num / 1000LL / 1000LL / 1000LL);
  else if (num > 1000LL * 1000 * 3)
    sprintf (label, "%llu m", num / 1000LL / 1000LL);
  else if (num > 1000LL * 3)
    sprintf (label, "%llu k", num / 1000LL);
  else
    sprintf (label, "%llu", num);
}


/**
 * Draw the statistics widget.
 *
 * @param widget widget to draw
 * @param cr drawing context
 */
static gboolean
gtk_statistics_draw (GtkWidget *widget,
                cairo_t   *cr)
{
  GtkStatistics *statistics = GTK_STATISTICS (widget);
  GtkStatisticsPrivate *priv = statistics->priv;
  int width;
  int height;
  uint64_t xmin;
  uint64_t xmax;
  uint64_t ymax;
  unsigned int i;
  unsigned int j;
  struct ValueHistory *vh;
  struct HistoricValue *hv;
  char label[64];
  cairo_text_extents_t te;
  cairo_text_extents_t tex_max;
  cairo_text_extents_t tey_max;
  double rx;
  double ry;
  unsigned int h;
  GtkAllocation alloc;

  /* collect basic data */
  xmin = UINT64_MAX;
  xmax = 0;
  ymax = 0;
  for (i=0;i<priv->num_values;i++)
  {
    vh = priv->values[i];
    for (j=0;j<vh->history_size;j++)
    {
      hv = &vh->history[(vh->last_history_offset - j + MAX_HISTORY) % MAX_HISTORY];
      xmin = GNUNET_MIN (hv->x, xmin);
      xmax = GNUNET_MAX (hv->x, xmax);
      ymax = GNUNET_MAX (hv->y, ymax);
    }
  }
  /* round to 10x for nicer legends */
  while (0 != (ymax % 10)) ymax++;
  while (0 != (xmax % 10)) xmax++;
  while (0 != (xmin % 10)) xmin--;

  gtk_widget_get_allocation (widget, &alloc);
  width = alloc.width;
  height = alloc.height;

  /* fill with black background */
  cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
  cairo_paint(cr);
  
 if ( (0 == priv->num_values) || (ymax == 0) )
    return FALSE; /* done */
  /* select font */
  cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
  cairo_select_font_face (cr, "Georgia",
			  CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
  cairo_set_font_size (cr, 
		       GNUNET_MIN (20, height / (2 * XTICKS)));

  /* find out needed space for axis labels */
  tex_max.width = 0;
  tex_max.height = 0;
  for (i=0;i<XTICKS;i++)
  {
    num_to_label ((unsigned long long) (xmin + (xmax - xmin) * ((uint64_t) i) / (XTICKS - 1)),
		  label);
    cairo_text_extents (cr, label, &te);
    tex_max.width = GNUNET_MAX (te.width, tex_max.width);
    tex_max.height = GNUNET_MAX (te.height, tex_max.height);
  }
  tey_max.width = 0;
  tey_max.height = 0;
  for (i=0;i<YTICKS;i++)
  {
    num_to_label ((unsigned long long) ymax * ((uint64_t) i) / (YTICKS - 1),
		  label);
    cairo_text_extents (cr, label, &te);
    tey_max.width = GNUNET_MAX (te.width, tey_max.width);
    tey_max.height = GNUNET_MAX (te.height, tey_max.height);
  }

  /* draw y-axis labels */
  for (i=0;i<YTICKS;i++)
  {
    num_to_label ((unsigned long long) ymax * ((uint64_t) i) / (YTICKS - 1),
		  label);
    cairo_text_extents (cr, label, &te);
    cairo_move_to (cr, 
		   BORDER + tey_max.width - te.width,
		   BORDER + tey_max.height + (YTICKS - i - 1) * (height - 2.0 * BORDER - tey_max.height - tex_max.height) / (double) (YTICKS - 1));
    cairo_show_text (cr, label);


    cairo_move_to (cr, 
		   2.0 * BORDER + tey_max.width,
		   BORDER + tey_max.height / 2.0 + (YTICKS - i - 1) * (height - 2.0 * BORDER - tex_max.height - tey_max.height) / (double) (YTICKS - 1));
    cairo_line_to (cr, 
		   2.0 * BORDER + tey_max.width + BORDER / 2.0,
		   BORDER + tey_max.height / 2.0 + (YTICKS - i - 1) * (height - 2.0 * BORDER - tex_max.height - tey_max.height) / (double) (YTICKS - 1));

    cairo_stroke (cr);
  }

  /* draw x-axis labels */
  for (i=0;i<XTICKS;i++)
  {
    num_to_label ((unsigned long long) (xmin + (xmax - xmin) * ((uint64_t) i) / (XTICKS - 1)),
		  label);
    cairo_text_extents (cr, label, &te);
    if (i != 0)
    {
      cairo_move_to (cr, 
		     2.0 * BORDER + tey_max.width + (width - tey_max.width - tex_max.width / 2.0 - 3.0 * BORDER) * i / (XTICKS - 1.0) - te.width / 2.0,
		     height - BORDER / 2.0 - tex_max.height / 2.0);
      cairo_show_text (cr, label);
    }
    cairo_move_to (cr, 
		   2.0 * BORDER + tey_max.width + (width - tey_max.width - tex_max.width / 2.0 - 3.0 * BORDER) * i / (XTICKS - 1.0),
		   height - BORDER - tey_max.height / 2.0 - tex_max.height);
    cairo_line_to (cr, 
		   2.0 * BORDER + tey_max.width + (width - tey_max.width - tex_max.width / 2.0 - 3.0 * BORDER) * i / (XTICKS - 1.0),
		   height - BORDER - tey_max.height / 2.0 - tex_max.height - BORDER / 2.0);

    cairo_stroke (cr);
  }

  /* plot border */
  cairo_set_line_width (cr, 1.0);
  cairo_rectangle (cr, 
		   tey_max.width + BORDER * 2.0, 
		   BORDER + tey_max.height / 2.0, 
		   width - BORDER * 3.0 - tey_max.width - tex_max.width / 2.0, 
		   height - BORDER * 2.0 - tey_max.height - tex_max.height);
  cairo_stroke (cr);

  /* finally, plot lines */
  cairo_set_line_width (cr, 2.0);

  cairo_set_font_size (cr, 
		       GNUNET_MIN (20, (height - 3.0 * BORDER - tex_max.height - tey_max.height / 2) / (priv->num_values + 1)));

  h = 2.0 * BORDER + tey_max.height / 2;

  for (i=0;i<priv->num_values;i++)
  {
    vh = priv->values[i];
    cairo_set_source_rgb(cr, vh->red, vh->green, vh->blue);
    cairo_text_extents (cr, vh->label, &te);
    h += te.height / 2;
    cairo_move_to (cr, 
		   3.0 * BORDER + tey_max.width,
		   h);
    h += te.height / 2 + 1.0;
    cairo_show_text (cr, vh->label);
    if (xmax == xmin) 
      continue;

    for (j=0;j<vh->history_size;j++)
    {
      hv = &vh->history[(vh->last_history_offset - j + MAX_HISTORY) % MAX_HISTORY];
      rx = (hv->x - xmin) / (double) (xmax - xmin);
      ry = hv->y / (double) ymax;
      
      rx = tey_max.width + BORDER * 2.0 + (rx * (width - BORDER * 3.0 - tey_max.width - tex_max.width / 2.0));
      ry = BORDER + tex_max.height / 2.0 + (1.0 - ry) * (height - BORDER * 2.0 - tey_max.height - tex_max.height);

      /* if y-values are small, offset y-values a bit to allow overlapping curves to still show up */
      if (ymax < height / (priv->num_values * 4))
	ry += priv->num_values * 2 - (4 * i);
      if (j == 0) 
	cairo_move_to (cr, 
		       width - BORDER - tex_max.width / 2.0, 
		       ry);      
      cairo_line_to (cr, rx, ry);
    }
    cairo_stroke (cr);
  }
  return FALSE;
}


/**
 * Free memory used by statistics object.
 *
 * @param object object to release
 */
static void
gtk_statistics_finalize (GObject *object)
{
  GtkStatistics *label = GTK_STATISTICS (object);
  GtkStatisticsPrivate *priv = label->priv;
  unsigned int i;

  for (i=0;i<priv->num_values;i++)
  {
    g_free (priv->values[i]->label);
    g_free (priv->values[i]->id);
    g_free (priv->values[i]);  
  }
  g_free (priv->values);  
  G_OBJECT_CLASS (gtk_statistics_parent_class)->finalize (object);
}


/* end of gtk_statistics.c */