]> git.tdb.fi Git - ext/subsurface.git/blob - profile.c
Print out only simplified depth profile
[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 mintemp, maxtemp;
26         struct plot_data {
27                 int sec;
28                 int pressure, temperature;
29                 /* Depth info */
30                 int val;
31                 int smoothed;
32                 velocity_t velocity;
33                 struct plot_data *min[3];
34                 struct plot_data *max[3];
35                 int avg[3];
36         } entry[];
37 };
38
39 /* convert velocity to colors */
40 typedef struct { double r, g, b; } rgb_t;
41 static const rgb_t rgb[] = {
42         [STABLE]   = {0.0, 0.4, 0.0},
43         [SLOW]     = {0.4, 0.8, 0.0},
44         [MODERATE] = {0.8, 0.8, 0.0},
45         [FAST]     = {0.8, 0.5, 0.0},
46         [CRAZY]    = {1.0, 0.0, 0.0},
47 };
48
49 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
50
51 /* Scale to 0,0 -> maxx,maxy */
52 #define SCALEX(gc,x)  (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
53 #define SCALEY(gc,y)  (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
54 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
55
56 static void move_to(struct graphics_context *gc, double x, double y)
57 {
58         cairo_move_to(gc->cr, SCALE(gc, x, y));
59 }
60
61 static void line_to(struct graphics_context *gc, double x, double y)
62 {
63         cairo_line_to(gc->cr, SCALE(gc, x, y));
64 }
65
66 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
67 {
68         if (gc->printer) {
69                 /* Black is white and white is black */
70                 double sum = r+g+b;
71                 if (sum > 2)
72                         r = g = b = 0;
73                 else if (sum < 1)
74                         r = g = b = 1;
75         }
76         cairo_set_source_rgba(gc->cr, r, g, b, a);
77 }
78
79 void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
80 {
81         set_source_rgba(gc, r, g, b, 1);
82 }
83
84 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
85
86 /*
87  * When showing dive profiles, we scale things to the
88  * current dive. However, we don't scale past less than
89  * 30 minutes or 90 ft, just so that small dives show
90  * up as such.
91  * we also need to add 180 seconds at the end so the min/max
92  * plots correctly
93  */
94 static int get_maxtime(struct plot_info *pi)
95 {
96         int seconds = pi->maxtime;
97         /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
98         return MAX(30*60, ROUND_UP(seconds+150, 60*5));
99 }
100
101 static int get_maxdepth(struct plot_info *pi)
102 {
103         unsigned mm = pi->maxdepth;
104         /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
105         return MAX(30000, ROUND_UP(mm+3000, 10000));
106 }
107
108 typedef struct {
109         int size;
110         double r,g,b;
111         double hpos, vpos;
112 } text_render_options_t;
113
114 #define RIGHT (-1.0)
115 #define CENTER (-0.5)
116 #define LEFT (0.0)
117
118 #define TOP (1)
119 #define MIDDLE (0)
120 #define BOTTOM (-1)
121
122 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
123                       double x, double y, const char *fmt, ...)
124 {
125         cairo_t *cr = gc->cr;
126         cairo_font_extents_t fe;
127         cairo_text_extents_t extents;
128         double dx, dy;
129         char buffer[80];
130         va_list args;
131
132         va_start(args, fmt);
133         vsnprintf(buffer, sizeof(buffer), fmt, args);
134         va_end(args);
135
136         cairo_set_font_size(cr, tro->size);
137         cairo_font_extents(cr, &fe);
138         cairo_text_extents(cr, buffer, &extents);
139         dx = tro->hpos * extents.width + extents.x_bearing;
140         dy = tro->vpos * extents.height + fe.descent;
141
142         move_to(gc, x, y);
143         cairo_rel_move_to(cr, dx, dy);
144
145         cairo_text_path(cr, buffer);
146         set_source_rgb(gc, 0, 0, 0);
147         cairo_stroke(cr);
148
149         move_to(gc, x, y);
150         cairo_rel_move_to(cr, dx, dy);
151
152         set_source_rgb(gc, tro->r, tro->g, tro->b);
153         cairo_show_text(cr, buffer);
154 }
155
156 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
157 {
158         int sec = entry->sec;
159         depth_t depth = { entry->val };
160         const char *fmt;
161         double d;
162
163         switch (output_units.length) {
164         case METERS:
165                 d = depth.mm / 1000.0;
166                 fmt = "%.1f";
167                 break;
168         case FEET:
169                 d = to_feet(depth);
170                 fmt = "%.0f";
171                 break;
172         }
173         plot_text(gc, tro, sec, depth.mm, fmt, d);
174 }
175
176 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
177 {
178         static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
179         static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
180         int i;
181
182         for (i = 0; i < pi->nr; i++) {
183                 struct plot_data *entry = pi->entry + i;
184
185                 if (entry->val < 2000)
186                         continue;
187
188                 if (entry == entry->max[2])
189                         render_depth_sample(gc, entry, &deep);
190
191                 if (entry == entry->min[2])
192                         render_depth_sample(gc, entry, &shallow);
193         }
194 }
195
196 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
197 {
198         int maxtime, maxdepth;
199
200         /* Get plot scaling limits */
201         maxtime = get_maxtime(pi);
202         maxdepth = get_maxdepth(pi);
203
204         gc->leftx = 0; gc->rightx = maxtime;
205         gc->topy = 0; gc->bottomy = maxdepth;
206
207         plot_text_samples(gc, pi);
208 }
209
210 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
211 {
212         int i;
213         struct plot_data *entry = pi->entry;
214
215         cairo_set_source_rgba(gc->cr, 1, 0.2, 0.2, 0.20);
216         move_to(gc, entry->sec, entry->smoothed);
217         for (i = 1; i < pi->nr; i++) {
218                 entry++;
219                 line_to(gc, entry->sec, entry->smoothed);
220         }
221         cairo_stroke(gc->cr);
222 }
223
224 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
225                                 int index, double a)
226 {
227         int i;
228         struct plot_data *entry = pi->entry;
229
230         cairo_set_source_rgba(gc->cr, 1, 0.2, 1, a);
231         move_to(gc, entry->sec, entry->min[index]->val);
232         for (i = 1; i < pi->nr; i++) {
233                 entry++;
234                 line_to(gc, entry->sec, entry->min[index]->val);
235         }
236         for (i = 1; i < pi->nr; i++) {
237                 line_to(gc, entry->sec, entry->max[index]->val);
238                 entry--;
239         }
240         cairo_close_path(gc->cr);
241         cairo_fill(gc->cr);
242 }
243
244 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
245 {
246         if (gc->printer)
247                 return;
248         plot_minmax_profile_minute(gc, pi, 2, 0.1);
249         plot_minmax_profile_minute(gc, pi, 1, 0.1);
250         plot_minmax_profile_minute(gc, pi, 0, 0.1);
251 }
252
253 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
254 {
255         int i;
256         cairo_t *cr = gc->cr;
257         int sec, depth;
258         struct plot_data *entry;
259         int maxtime, maxdepth, marker;
260
261         /* Get plot scaling limits */
262         maxtime = get_maxtime(pi);
263         maxdepth = get_maxdepth(pi);
264
265         /* Time markers: every 5 min */
266         gc->leftx = 0; gc->rightx = maxtime;
267         gc->topy = 0; gc->bottomy = 1.0;
268         for (i = 5*60; i < maxtime; i += 5*60) {
269                 move_to(gc, i, 0);
270                 line_to(gc, i, 1);
271         }
272
273         /* Depth markers: every 30 ft or 10 m*/
274         gc->leftx = 0; gc->rightx = 1.0;
275         gc->topy = 0; gc->bottomy = maxdepth;
276         switch (output_units.length) {
277         case METERS: marker = 10000; break;
278         case FEET: marker = 9144; break;        /* 30 ft */
279         }
280
281         set_source_rgba(gc, 1, 1, 1, 0.5);
282         for (i = marker; i < maxdepth; i += marker) {
283                 move_to(gc, 0, i);
284                 line_to(gc, 1, i);
285         }
286         cairo_stroke(cr);
287
288         /* Show mean depth */
289         set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
290         move_to(gc, 0, pi->meandepth);
291         line_to(gc, 1, pi->meandepth);
292         cairo_stroke(cr);
293
294         gc->leftx = 0; gc->rightx = maxtime;
295
296         /*
297          * These are good for debugging text placement etc,
298          * but not for actual display..
299          */
300         if (0) {
301                 plot_smoothed_profile(gc, pi);
302                 plot_minmax_profile(gc, pi);
303         }
304
305         set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
306
307         /* Do the depth profile for the neat fill */
308         gc->topy = 0; gc->bottomy = maxdepth;
309         set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
310
311         entry = pi->entry;
312         move_to(gc, 0, 0);
313         for (i = 0; i < pi->nr; i++, entry++)
314                 line_to(gc, entry->sec, entry->val);
315         cairo_close_path(gc->cr);
316         if (gc->printer) {
317                 set_source_rgba(gc, 1, 1, 1, 0.2);
318                 cairo_fill_preserve(cr);
319                 set_source_rgb(gc, 1, 1, 1);
320                 cairo_stroke(cr);
321                 return;
322         }
323         cairo_fill(gc->cr);
324
325         /* Now do it again for the velocity colors */
326         entry = pi->entry;
327         for (i = 1; i < pi->nr; i++) {
328                 entry++;
329                 sec = entry->sec;
330                 /* we want to draw the segments in different colors
331                  * representing the vertical velocity, so we need to
332                  * chop this into short segments */
333                 rgb_t color = rgb[entry->velocity];
334                 depth = entry->val;
335                 set_source_rgb(gc, color.r, color.g, color.b);
336                 move_to(gc, entry[-1].sec, entry[-1].val);
337                 line_to(gc, sec, depth);
338                 cairo_stroke(cr);
339         }
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 }