]> git.tdb.fi Git - ext/subsurface.git/blob - profile.c
Attempt to smooth out the velocity readings
[ext/subsurface.git] / profile.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <string.h>
5 #include <time.h>
6
7 #include "dive.h"
8 #include "display.h"
9 #include "divelist.h"
10
11 int selected_dive = 0;
12
13 typedef enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity_t;
14 /* Plot info with smoothing, velocity indication
15  * and one-, two- and three-minute minimums and maximums */
16 struct plot_info {
17         int nr;
18         int maxtime;
19         int meandepth, maxdepth;
20         int minpressure, maxpressure;
21         int mintemp, maxtemp;
22         struct plot_data {
23                 int sec;
24                 int pressure, temperature;
25                 /* Depth info */
26                 int val;
27                 int smoothed;
28                 velocity_t velocity;
29                 struct plot_data *min[3];
30                 struct plot_data *max[3];
31                 int avg[3];
32         } entry[];
33 };
34
35 /* convert velocity to colors */
36 typedef struct { double r, g, b; } rgb_t;
37 static const rgb_t rgb[] = {
38         [STABLE]   = {0.0, 0.4, 0.0},
39         [SLOW]     = {0.4, 0.8, 0.0},
40         [MODERATE] = {0.8, 0.8, 0.0},
41         [FAST]     = {0.8, 0.5, 0.0},
42         [CRAZY]    = {1.0, 0.0, 0.0},
43 };
44
45 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
46
47 /* Scale to 0,0 -> maxx,maxy */
48 #define SCALEX(gc,x)  (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
49 #define SCALEY(gc,y)  (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
50 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
51
52 static void move_to(struct graphics_context *gc, double x, double y)
53 {
54         cairo_move_to(gc->cr, SCALE(gc, x, y));
55 }
56
57 static void line_to(struct graphics_context *gc, double x, double y)
58 {
59         cairo_line_to(gc->cr, SCALE(gc, x, y));
60 }
61
62 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
63 {
64         if (gc->printer) {
65                 /* Black is white and white is black */
66                 double sum = r+g+b;
67                 if (sum > 2)
68                         r = g = b = 0;
69                 else if (sum < 1)
70                         r = g = b = 1;
71         }
72         cairo_set_source_rgba(gc->cr, r, g, b, a);
73 }
74
75 static void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
76 {
77         set_source_rgba(gc, r, g, b, 1);
78 }
79
80 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
81
82 /*
83  * When showing dive profiles, we scale things to the
84  * current dive. However, we don't scale past less than
85  * 30 minutes or 90 ft, just so that small dives show
86  * up as such.
87  * we also need to add 180 seconds at the end so the min/max
88  * plots correctly
89  */
90 static int get_maxtime(struct plot_info *pi)
91 {
92         int seconds = pi->maxtime;
93         /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
94         return MAX(30*60, ROUND_UP(seconds+150, 60*5));
95 }
96
97 static int get_maxdepth(struct plot_info *pi)
98 {
99         unsigned mm = pi->maxdepth;
100         /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
101         return MAX(30000, ROUND_UP(mm+3000, 10000));
102 }
103
104 typedef struct {
105         int size;
106         double r,g,b;
107         double hpos, vpos;
108 } text_render_options_t;
109
110 #define RIGHT (-1.0)
111 #define CENTER (-0.5)
112 #define LEFT (0.0)
113
114 #define TOP (1)
115 #define MIDDLE (0)
116 #define BOTTOM (-1)
117
118 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
119                       double x, double y, const char *fmt, ...)
120 {
121         cairo_t *cr = gc->cr;
122         cairo_font_extents_t fe;
123         cairo_text_extents_t extents;
124         double dx, dy;
125         char buffer[80];
126         va_list args;
127
128         va_start(args, fmt);
129         vsnprintf(buffer, sizeof(buffer), fmt, args);
130         va_end(args);
131
132         cairo_set_font_size(cr, tro->size);
133         cairo_font_extents(cr, &fe);
134         cairo_text_extents(cr, buffer, &extents);
135         dx = tro->hpos * extents.width + extents.x_bearing;
136         dy = tro->vpos * extents.height + fe.descent;
137
138         move_to(gc, x, y);
139         cairo_rel_move_to(cr, dx, dy);
140
141         cairo_text_path(cr, buffer);
142         set_source_rgb(gc, 0, 0, 0);
143         cairo_stroke(cr);
144
145         move_to(gc, x, y);
146         cairo_rel_move_to(cr, dx, dy);
147
148         set_source_rgb(gc, tro->r, tro->g, tro->b);
149         cairo_show_text(cr, buffer);
150 }
151
152 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
153 {
154         int sec = entry->sec;
155         depth_t depth = { entry->val };
156         const char *fmt;
157         double d;
158
159         switch (output_units.length) {
160         case METERS:
161                 d = depth.mm / 1000.0;
162                 fmt = "%.1f";
163                 break;
164         case FEET:
165                 d = to_feet(depth);
166                 fmt = "%.0f";
167                 break;
168         }
169         plot_text(gc, tro, sec, depth.mm, fmt, d);
170 }
171
172 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
173 {
174         static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
175         static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
176         int i;
177
178         for (i = 0; i < pi->nr; i++) {
179                 struct plot_data *entry = pi->entry + i;
180
181                 if (entry->val < 2000)
182                         continue;
183
184                 if (entry == entry->max[2])
185                         render_depth_sample(gc, entry, &deep);
186
187                 if (entry == entry->min[2])
188                         render_depth_sample(gc, entry, &shallow);
189         }
190 }
191
192 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
193 {
194         int maxtime, maxdepth;
195
196         /* Get plot scaling limits */
197         maxtime = get_maxtime(pi);
198         maxdepth = get_maxdepth(pi);
199
200         gc->leftx = 0; gc->rightx = maxtime;
201         gc->topy = 0; gc->bottomy = maxdepth;
202
203         plot_text_samples(gc, pi);
204 }
205
206 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
207 {
208         int i;
209         struct plot_data *entry = pi->entry;
210
211         cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
212         move_to(gc, entry->sec, entry->smoothed);
213         for (i = 1; i < pi->nr; i++) {
214                 entry++;
215                 line_to(gc, entry->sec, entry->smoothed);
216         }
217         cairo_stroke(gc->cr);
218 }
219
220 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
221                                 int index, double a)
222 {
223         int i;
224         struct plot_data *entry = pi->entry;
225
226         cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
227         move_to(gc, entry->sec, entry->min[index]->val);
228         for (i = 1; i < pi->nr; i++) {
229                 entry++;
230                 line_to(gc, entry->sec, entry->min[index]->val);
231         }
232         for (i = 1; i < pi->nr; i++) {
233                 line_to(gc, entry->sec, entry->max[index]->val);
234                 entry--;
235         }
236         cairo_close_path(gc->cr);
237         cairo_fill(gc->cr);
238 }
239
240 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
241 {
242         if (gc->printer)
243                 return;
244         plot_minmax_profile_minute(gc, pi, 2, 0.1);
245         plot_minmax_profile_minute(gc, pi, 1, 0.1);
246         plot_minmax_profile_minute(gc, pi, 0, 0.1);
247 }
248
249 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
250 {
251         int i;
252         cairo_t *cr = gc->cr;
253         int ends, sec, depth;
254         int *secs;
255         int *depths;
256         struct plot_data *entry;
257         int maxtime, maxdepth, marker;
258
259         /* Get plot scaling limits */
260         maxtime = get_maxtime(pi);
261         maxdepth = get_maxdepth(pi);
262
263         /* Time markers: every 5 min */
264         gc->leftx = 0; gc->rightx = maxtime;
265         gc->topy = 0; gc->bottomy = 1.0;
266         for (i = 5*60; i < maxtime; i += 5*60) {
267                 move_to(gc, i, 0);
268                 line_to(gc, i, 1);
269         }
270
271         /* Depth markers: every 30 ft or 10 m*/
272         gc->leftx = 0; gc->rightx = 1.0;
273         gc->topy = 0; gc->bottomy = maxdepth;
274         switch (output_units.length) {
275         case METERS: marker = 10000; break;
276         case FEET: marker = 9144; break;        /* 30 ft */
277         }
278
279         set_source_rgba(gc, 1, 1, 1, 0.5);
280         for (i = marker; i < maxdepth; i += marker) {
281                 move_to(gc, 0, i);
282                 line_to(gc, 1, i);
283         }
284         cairo_stroke(cr);
285
286         /* Show mean depth */
287         set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
288         move_to(gc, 0, pi->meandepth);
289         line_to(gc, 1, pi->meandepth);
290         cairo_stroke(cr);
291
292         gc->leftx = 0; gc->rightx = maxtime;
293
294         plot_smoothed_profile(gc, pi);
295         plot_minmax_profile(gc, pi);
296
297         entry = pi->entry;
298         set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
299         secs = (int *) malloc(sizeof(int) * pi->nr);
300         depths = (int *) malloc(sizeof(int) * pi->nr);
301         secs[0] = entry->sec;
302         depths[0] = entry->val;
303         for (i = 1; i < pi->nr; i++) {
304                 entry++;
305                 sec = entry->sec;
306                 if (sec <= maxtime || entry->val > 0) {
307                         /* we want to draw the segments in different colors
308                          * representing the vertical velocity, so we need to
309                          * chop this into short segments */
310                         rgb_t color = rgb[entry->velocity];
311                         depth = entry->val;
312                         set_source_rgb(gc, color.r, color.g, color.b);
313                         move_to(gc, secs[i-1], depths[i-1]);
314                         line_to(gc, sec, depth);
315                         cairo_stroke(cr);
316                         ends = i;
317                 }
318                 secs[i] = sec;
319                 depths[i] = depth;
320         }
321         move_to(gc, secs[ends], depths[ends]);
322         gc->topy = 0; gc->bottomy = 1.0;
323         line_to(gc, secs[ends], 0);
324         line_to(gc, secs[0], 0);
325         cairo_close_path(cr);
326         set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
327         cairo_stroke(cr);
328         /* now do it again for the neat fill */
329         gc->topy = 0; gc->bottomy = maxdepth;
330         set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
331         move_to(gc, secs[0], depths[0]);
332         for (i = 1; i <= ends; i++) {
333                 line_to(gc, secs[i],depths[i]);
334         }
335         gc->topy = 0; gc->bottomy = 1.0;
336         line_to(gc, secs[ends], 0);
337         line_to(gc, secs[0], 0);
338         cairo_close_path(gc->cr);
339         cairo_fill(gc->cr);
340 }
341
342 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
343 {
344         int maxtime, mintemp, maxtemp, delta;
345
346         /* Get plot scaling limits */
347         maxtime = get_maxtime(pi);
348         mintemp = pi->mintemp;
349         maxtemp = pi->maxtemp;
350
351         gc->leftx = 0; gc->rightx = maxtime;
352         /* Show temperatures in roughly the lower third, but make sure the scale
353            is at least somewhat reasonable */
354         delta = maxtemp - mintemp;
355         if (delta > 3000) { /* more than 3K in fluctuation */
356                 gc->topy = maxtemp + delta*2;
357                 gc->bottomy = mintemp - delta/2;
358         } else {
359                 gc->topy = maxtemp + 1500 + delta*2;
360                 gc->bottomy = mintemp - delta/2;
361         }
362
363         return maxtemp > mintemp;
364 }
365
366 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
367 {
368         int deg;
369         const char *unit;
370         static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
371         temperature_t temperature = { mkelvin };
372
373         if (output_units.temperature == FAHRENHEIT) {
374                 deg = to_F(temperature);
375                 unit = "F";
376         } else {
377                 deg = to_C(temperature);
378                 unit = "C";
379         }
380         plot_text(gc, &tro, sec, temperature.mkelvin, "%d %s", deg, unit);
381 }
382
383 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
384 {
385         int i;
386         int last = 0, sec = 0;
387         int last_temperature = 0, last_printed_temp = 0;
388
389         if (!setup_temperature_limits(gc, pi))
390                 return;
391
392         for (i = 0; i < pi->nr; i++) {
393                 struct plot_data *entry = pi->entry+i;
394                 int mkelvin = entry->temperature;
395
396                 if (!mkelvin)
397                         continue;
398                 last_temperature = mkelvin;
399                 sec = entry->sec;
400                 if (sec < last + 300)
401                         continue;
402                 last = sec;
403                 plot_single_temp_text(gc,sec,mkelvin);
404                 last_printed_temp = mkelvin;
405         }
406         /* it would be nice to print the end temperature, if it's different */
407         if (abs(last_temperature - last_printed_temp) > 500)
408                 plot_single_temp_text(gc, sec, last_temperature);
409 }
410
411 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
412 {
413         int i;
414         cairo_t *cr = gc->cr;
415         int last = 0;
416
417         if (!setup_temperature_limits(gc, pi))
418                 return;
419
420         set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
421         for (i = 0; i < pi->nr; i++) {
422                 struct plot_data *entry = pi->entry + i;
423                 int mkelvin = entry->temperature;
424                 int sec = entry->sec;
425                 if (!mkelvin) {
426                         if (!last)
427                                 continue;
428                         mkelvin = last;
429                 }
430                 if (last)
431                         line_to(gc, sec, mkelvin);
432                 else
433                         move_to(gc, sec, mkelvin);
434                 last = mkelvin;
435         }
436         cairo_stroke(cr);
437 }
438
439 /* gets both the actual start and end pressure as well as the scaling factors */
440 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
441 {
442         gc->leftx = 0;
443         gc->rightx = get_maxtime(pi);
444
445         gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
446         return pi->maxpressure != 0;
447 }
448
449 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi)
450 {
451         int i;
452
453         if (!get_cylinder_pressure_range(gc, pi))
454                 return;
455
456         cairo_set_source_rgba(gc->cr, 0.2, 1.0, 0.2, 0.80);
457
458         move_to(gc, 0, pi->maxpressure);
459         for (i = 1; i < pi->nr; i++) {
460                 int mbar;
461                 struct plot_data *entry = pi->entry + i;
462
463                 mbar = entry->pressure;
464                 if (!mbar)
465                         continue;
466                 line_to(gc, entry->sec, mbar);
467         }
468         line_to(gc, pi->maxtime, pi->minpressure);
469         cairo_stroke(gc->cr);
470 }
471
472 static int mbar_to_PSI(int mbar)
473 {
474         pressure_t p = {mbar};
475         return to_PSI(p);
476 }
477
478 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
479 {
480         if (get_cylinder_pressure_range(gc, pi)) {
481                 int start, end;
482                 const char *unit = "bar";
483
484                 switch (output_units.pressure) {
485                 case PASCAL:
486                         start = pi->maxpressure * 100;
487                         end = pi->minpressure * 100;
488                         unit = "pascal";
489                         break;
490                 case BAR:
491                         start = (pi->maxpressure + 500) / 1000;
492                         end = (pi->minpressure + 500) / 1000;
493                         unit = "bar";
494                         break;
495                 case PSI:
496                         start = mbar_to_PSI(pi->maxpressure);
497                         end = mbar_to_PSI(pi->minpressure);
498                         unit = "psi";
499                         break;
500                 }
501
502                 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
503                 plot_text(gc, &tro, 0, pi->maxpressure, "%d %s", start, unit);
504                 plot_text(gc, &tro, pi->maxtime, pi->minpressure,
505                           "%d %s", end, unit);
506         }
507 }
508
509 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
510 {
511         struct plot_data *p = entry;
512         int time = entry->sec;
513         int seconds = 90*(index+1);
514         struct plot_data *min, *max;
515         int avg, nr;
516
517         /* Go back 'seconds' in time */
518         while (p > first) {
519                 if (p[-1].sec < time - seconds)
520                         break;
521                 p--;
522         }
523
524         /* Then go forward until we hit an entry past the time */
525         min = max = p;
526         avg = p->val;
527         nr = 1;
528         while (++p < last) {
529                 int val = p->val;
530                 if (p->sec > time + seconds)
531                         break;
532                 avg += val;
533                 nr ++;
534                 if (val < min->val)
535                         min = p;
536                 if (val > max->val)
537                         max = p;
538         }
539         entry->min[index] = min;
540         entry->max[index] = max;
541         entry->avg[index] = (avg + nr/2) / nr;
542 }
543
544 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
545 {
546         analyze_plot_info_minmax_minute(entry, first, last, 0);
547         analyze_plot_info_minmax_minute(entry, first, last, 1);
548         analyze_plot_info_minmax_minute(entry, first, last, 2);
549 }
550
551 static velocity_t velocity(int speed)
552 {
553         velocity_t v;
554
555         if (speed < -304) /* ascent faster than -60ft/min */
556                 v = CRAZY;
557         else if (speed < -152) /* above -30ft/min */
558                 v = FAST;
559         else if (speed < -76) /* -15ft/min */
560                 v = MODERATE;
561         else if (speed < -25) /* -5ft/min */
562                 v = SLOW;
563         else if (speed < 25) /* very hard to find data, but it appears that the recommendations
564                                 for descent are usually about 2x ascent rate; still, we want 
565                                 stable to mean stable */
566                 v = STABLE;
567         else if (speed < 152) /* between 5 and 30ft/min is considered slow */
568                 v = SLOW;
569         else if (speed < 304) /* up to 60ft/min is moderate */
570                 v = MODERATE;
571         else if (speed < 507) /* up to 100ft/min is fast */
572                 v = FAST;
573         else /* more than that is just crazy - you'll blow your ears out */
574                 v = CRAZY;
575
576         return v;
577 }
578 static struct plot_info *analyze_plot_info(struct plot_info *pi)
579 {
580         int i;
581         int nr = pi->nr;
582
583         /* Do pressure min/max based on the non-surface data */
584         for (i = 0; i < nr; i++) {
585                 struct plot_data *entry = pi->entry+i;
586                 int pressure = entry->pressure;
587                 int temperature = entry->temperature;
588
589                 if (pressure) {
590                         if (!pi->minpressure || pressure < pi->minpressure)
591                                 pi->minpressure = pressure;
592                         if (pressure > pi->maxpressure)
593                                 pi->maxpressure = pressure;
594                 }
595
596                 if (temperature) {
597                         if (!pi->mintemp || temperature < pi->mintemp)
598                                 pi->mintemp = temperature;
599                         if (temperature > pi->maxtemp)
600                                 pi->maxtemp = temperature;
601                 }
602         }
603
604         /* Smoothing function: 5-point triangular smooth */
605         for (i = 2; i < nr-1; i++) {
606                 struct plot_data *entry = pi->entry+i;
607                 int val;
608
609                 if (i < nr-2) {
610                         val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
611                         entry->smoothed = (val+4) / 9;
612                 }
613                 /* vertical velocity in mm/sec */
614                 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
615                 if (entry[0].sec - entry[-1].sec) {
616                         entry->velocity = velocity((entry[0].val - entry[-1].val) / (entry[0].sec - entry[-1].sec));
617                         /* if our samples are short and we aren't too FAST*/
618                         if (entry[0].sec - entry[-1].sec < 30 && entry->velocity < FAST) { 
619                                 int past = -2;
620                                 while (pi->entry <= entry-past && entry[0].sec - entry[past].sec < 30)
621                                         past--;
622                                 entry->velocity = velocity((entry[0].val - entry[past].val) / 
623                                                         (entry[0].sec - entry[past].sec));
624                         }
625                 } else
626                         entry->velocity = STABLE;
627         }
628
629         /* One-, two- and three-minute minmax data */
630         for (i = 0; i < nr; i++) {
631                 struct plot_data *entry = pi->entry +i;
632                 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
633         }
634         
635         return pi;
636 }
637
638 /*
639  * Create a plot-info with smoothing and ranged min/max
640  *
641  * This also makes sure that we have extra empty events on both
642  * sides, so that you can do end-points without having to worry
643  * about it.
644  */
645 static struct plot_info *create_plot_info(struct dive *dive)
646 {
647         int lastdepth, lastindex;
648         int i, nr = dive->samples + 4, sec;
649         size_t alloc_size = plot_info_size(nr);
650         struct plot_info *pi;
651
652         pi = malloc(alloc_size);
653         if (!pi)
654                 return pi;
655         memset(pi, 0, alloc_size);
656         pi->nr = nr;
657         sec = 0;
658         lastindex = 0;
659         lastdepth = -1;
660         for (i = 0; i < dive->samples; i++) {
661                 int depth;
662                 struct sample *sample = dive->sample+i;
663                 struct plot_data *entry = pi->entry + i + 2;
664
665                 sec = entry->sec = sample->time.seconds;
666                 depth = entry->val = sample->depth.mm;
667                 entry->pressure = sample->cylinderpressure.mbar;
668                 entry->temperature = sample->temperature.mkelvin;
669
670                 if (depth || lastdepth)
671                         lastindex = i+2;
672
673                 lastdepth = depth;
674                 if (depth > pi->maxdepth)
675                         pi->maxdepth = depth;
676         }
677         if (lastdepth)
678                 lastindex = i + 2;
679         /* Fill in the last two entries with empty values but valid times */
680         i = dive->samples + 2;
681         pi->entry[i].sec = sec + 20;
682         pi->entry[i+1].sec = sec + 40;
683
684         pi->nr = lastindex+1;
685         pi->maxtime = pi->entry[lastindex].sec;
686
687         pi->minpressure = dive->cylinder[0].end.mbar;
688         pi->maxpressure = dive->cylinder[0].start.mbar;
689
690         pi->meandepth = dive->meandepth.mm;
691
692         return analyze_plot_info(pi);
693 }
694
695 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
696 {
697         double topx, topy;
698         struct plot_info *pi = create_plot_info(dive);
699
700         topx = w / 20.0;
701         topy = h / 20.0;
702         cairo_translate(gc->cr, topx, topy);
703         cairo_set_line_width(gc->cr, 2);
704         cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
705         cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
706
707         /*
708          * We can use "cairo_translate()" because that doesn't
709          * scale line width etc. But the actual scaling we need
710          * do set up ourselves..
711          *
712          * Snif. What a pity.
713          */
714         gc->maxx = (w - 2*topx);
715         gc->maxy = (h - 2*topy);
716
717         /* Temperature profile */
718         plot_temperature_profile(gc, pi);
719
720         /* Cylinder pressure plot */
721         plot_cylinder_pressure(gc, pi);
722
723         /* Depth profile */
724         plot_depth_profile(gc, pi);
725
726         /* Text on top of all graphs.. */
727         plot_temperature_text(gc, pi);
728         plot_depth_text(gc, pi);
729         plot_cylinder_pressure_text(gc, pi);
730
731         /* Bounding box last */
732         gc->leftx = 0; gc->rightx = 1.0;
733         gc->topy = 0; gc->bottomy = 1.0;
734
735         set_source_rgb(gc, 1, 1, 1);
736         move_to(gc, 0, 0);
737         line_to(gc, 0, 1);
738         line_to(gc, 1, 1);
739         line_to(gc, 1, 0);
740         cairo_close_path(gc->cr);
741         cairo_stroke(gc->cr);
742
743 }
744
745 static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
746 {
747         struct dive *dive = current_dive;
748         struct graphics_context gc = { .printer = 0 };
749         int w,h;
750
751         w = widget->allocation.width;
752         h = widget->allocation.height;
753
754         gc.cr = gdk_cairo_create(widget->window);
755         set_source_rgb(&gc, 0, 0, 0);
756         cairo_paint(gc.cr);
757
758         if (dive)
759                 plot(&gc, w, h, dive);
760
761         cairo_destroy(gc.cr);
762
763         return FALSE;
764 }
765
766 GtkWidget *dive_profile_widget(void)
767 {
768         GtkWidget *da;
769
770         da = gtk_drawing_area_new();
771         gtk_widget_set_size_request(da, 350, 250);
772         g_signal_connect(da, "expose_event", G_CALLBACK(expose_event), NULL);
773
774         return da;
775 }