skip to content
==> tests/chart.c <==
1/* A line chart with both axes' labels and a legend: the layout ratatui gives it. */
2#include "mesh/check.h"
3#include "tui/chart.h"
4
5#include <string.h>
6
7static const char *row(const tui_buffer *b, uint16_t y) {
8    static char out[1024];
9    size_t n = 0;
10    for (uint16_t x = 0; x < b->area.width; x++) {
11        const char *s = tui_cell_symbol(tui_buffer_cell_c(b, x, y));
12        size_t k = strlen(s);
13        memcpy(out + n, s, k);
14        n += k;
15    }
16    out[n] = 0;
17    return out;
18}
19
20int main(void) {
21    tui_arena a = {0};
22    tui_buffer b;
23    tui_buffer_init(&b, (tui_rect){0, 0, 20, 8});
24    tui_point pts[3] = {{0, 0}, {5, 5}, {10, 10}};
25    tui_dataset d = tui_dataset_new();
26    d.has_name = true;
27    d.name = tui_line_from_str(&a, "data");
28    d.data = pts;
29    d.n = 3;
30    d.marker = TUI_MARKER_DOT;
31    d.graph_type = TUI_GRAPH_LINE;
32    tui_chart c = tui_chart_new(&d, 1);
33    tui_line xl[2] = {tui_line_from_str(&a, "0"), tui_line_from_str(&a, "10")};
34    tui_line yl[2] = {tui_line_from_str(&a, "0"), tui_line_from_str(&a, "10")};
35    c.x_axis.bounds[0] = 0;
36    c.x_axis.bounds[1] = 10;
37    c.x_axis.labels = xl;
38    c.x_axis.nlabels = 2;
39    c.y_axis.bounds[0] = 0;
40    c.y_axis.bounds[1] = 10;
41    c.y_axis.labels = yl;
42    c.y_axis.nlabels = 2;
43    tui_chart_render(&a, &c, b.area, &b);
44    CHECK_S(row(&b, 7), "  0               10");
45    CHECK_S(row(&b, 6), "  └─────────────────");
46    CHECK(strstr(row(&b, 0), "10│") == row(&b, 0));
47    tui_buffer_free(&b);
48    tui_arena_free(&a);
49    tui_layout_cache_clear();
50    return mesh_check_done();
51}