ckplot

plot scatter graphs of csv data
git clone git://bvnf.space/ckplot.git
Log | Files | Refs | README

kplot.c (4625B)


      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
#include <bsd/stdio.h>
#include <bsd/stdlib.h>
#include <cairo.h>
#include <cairo-pdf.h>
#include <csv.h>
#include <errno.h>
#include <kplot.h>
#include <math.h>
#include <string.h>
#include <unistd.h>

void
usage(char *argv0) {
    fprintf(stderr, "usage: %s [-HTwoh] [-O|-X] [file.csv]\n", argv0);
    fprintf(stderr, "    -H          treat first line as header\n");
    fprintf(stderr, "    -T format   output format (options \"png\", \"pdf\")"
            " (default: \"pdf\")\n");
    fprintf(stderr, "    -o outfile  output filename\n");
    fprintf(stderr, "    -w width\n");
    fprintf(stderr, "    -h height\n");
    fprintf(stderr, "    -O          data markers are circles\n");
    fprintf(stderr, "    -X          data markers are crosses\n");
}

int
main(int argc, char **argv) {
    int ch, flag_H, flag_o, output_type, width, height, w, h, point_type;
    char *outfile;
    flag_H = flag_o = 0;
    width = 600;
    height = 400;
    point_type = KPLOT_POINTS;
    enum {
        OUT_PNG,
        OUT_PDF
    };
    output_type = OUT_PDF;
    outfile = "out.pdf";
    while ((ch = getopt(argc, argv, "HT:o:w:h:OX")) != -1) {
        switch (ch) {
            case 'H':
                flag_H = 1;
                break;
            case 'T':
                if (strncmp("png", optarg, 3) == 0) {
                    output_type = OUT_PNG;
                    if (flag_o == 0)
                        outfile = "out.png";
                } else if (strncmp("pdf", optarg, 3) == 0)
                    output_type = OUT_PDF;
                else
                    fprintf(stderr, "output format \"%s\" not recognised "
                            "(supported: \"png\", \"pdf\")\n", optarg);
                break;
            case 'o':
                flag_o = 1;
                outfile = optarg;
                break;
            case 'h':
                h = atoi(optarg);
                if (h <= 0) {
                    fprintf(stderr, "bad height \"%s\"\n", optarg);
                    return 1;
                }
                height = h;
                break;
            case 'w':
                w = atoi(optarg);
                if (w <= 0) {
                    fprintf(stderr, "bad width \"%s\"\n", optarg);
                    return 1;
                }
                width = w;
                break;
            case 'O':
                point_type = KPLOT_POINTS;
                break;
            case 'X':
                point_type = KPLOT_MARKS;
                break;
            case '?':
                usage(*argv);
                return 1;
        }
    }

    FILE *f;
    if (argc - optind == 0) {
        f = stdin;
    } else {
        argv += optind;
        f = fopen(*argv, "r");
        if (f == NULL) {
            perror(*argv);
            return 1;
        }
    }

    struct csv *c = csv_create();
    if (csv_read_file(f, c, flag_H) != 0) {
        perror("csv_read_file");
        return 1;
    }
    if (c->cols > 2) {
        fprintf(stderr, "warning: %d columns in file but only using first two\n", c->cols);
    }
    if (c->rows < 1) {
        fprintf(stderr, "no rows found\n");
        csv_destroy(c);
        return 1;
    }

    int rows = c->rows;
    struct kpair points1[c->rows];

    for (int i = 0; i < c->rows; i++) {
        if (c->cols == 1) {
            points1[i].x = i;
            points1[i].y = c->data[i][0];
        } else {
            points1[i].x = c->data[i][0];
            points1[i].y = c->data[i][1];
        }
    }

    char *xlabel, *ylabel;
    if (flag_H) {
        xlabel = c->headers[0];
        ylabel = c->headers[1];
    }
    csv_destroy(c);

    struct kdata *d1;
    struct kplot *p;
    struct kplotcfg kpcfg;
    kplotcfg_defaults(&kpcfg);
    if (flag_H) {
        errno = 0;
        kpcfg.xaxislabel = strdup(xlabel);
        kpcfg.yaxislabel = strdup(ylabel);
        if (errno != 0) {
            perror("strdup");
            return 1;
        }
    }
    cairo_surface_t *surf;
    cairo_t *cr;

    d1 = kdata_array_alloc(points1, rows);
    p = kplot_alloc(&kpcfg);
    kplot_attach_data(p, d1, point_type, NULL);
    kdata_destroy(d1);
    if (output_type == OUT_PNG)
        surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
    else
        surf = cairo_pdf_surface_create(outfile, width, height);
    cr = cairo_create(surf);
    cairo_surface_destroy(surf);
    kplot_draw(p, (double) width, (double) height, cr);
    if (output_type == OUT_PNG)
        cairo_surface_write_to_png(cairo_get_target(cr), outfile);
    cairo_destroy(cr);
    kplot_free(p);
    return 0;
}