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