skip to content
==> include/tui/chart.h <==
1/* @ajax/tui-chart: ratatui's Chart, Axis and Dataset: datasets drawn on a canvas (scatter points,
2 * lines, bars or filled areas), x and y axes with labels and titles, and a legend placed where it
3 * fits. */
4#ifndef TUI_CHART_H
5#define TUI_CHART_H
6
7#include "tui/canvas.h"
8#include "tui/layout.h"
9
10typedef enum { TUI_GRAPH_SCATTER, TUI_GRAPH_LINE, TUI_GRAPH_BAR, TUI_GRAPH_AREA } tui_graph_type;
11
12typedef enum {
13    TUI_LEGEND_TOP,
14    TUI_LEGEND_TOP_RIGHT, /* the default */
15    TUI_LEGEND_TOP_LEFT,
16    TUI_LEGEND_LEFT,
17    TUI_LEGEND_RIGHT,
18    TUI_LEGEND_BOTTOM,
19    TUI_LEGEND_BOTTOM_RIGHT,
20    TUI_LEGEND_BOTTOM_LEFT,
21} tui_legend_position;
22
23typedef struct {
24    bool has_title;
25    tui_line title;
26    double bounds[2];
27    const tui_line *labels;
28    size_t nlabels;
29    tui_style style;
30    int8_t labels_alignment; /* TUI_ALIGN_LEFT */
31} tui_axis;
32
33typedef struct {
34    bool has_name;
35    tui_line name;
36    const tui_point *data;
37    size_t n;
38    tui_marker marker; /* TUI_MARKER_DOT by default */
39    tui_graph_type graph_type;
40    tui_style style;
41    double fill_to_y;
42} tui_dataset;
43
44typedef struct {
45    const tui_block *block;
46    tui_axis x_axis, y_axis;
47    const tui_dataset *datasets;
48    size_t ndatasets;
49    tui_style style;
50    tui_constraint hidden_legend[2]; /* Ratio(1, 4) each */
51    bool has_legend;                 /* true */
52    tui_legend_position legend_position;
53} tui_chart;
54
55tui_chart tui_chart_new(const tui_dataset *datasets, size_t n);
56static inline tui_axis tui_axis_new(void) {
57    tui_axis x = {0};
58    x.labels_alignment = TUI_ALIGN_LEFT;
59    return x;
60}
61static inline tui_dataset tui_dataset_new(void) {
62    tui_dataset d = {0};
63    d.marker = TUI_MARKER_DOT;
64    return d;
65}
66
67void tui_chart_render(tui_arena *a, const tui_chart *c, tui_rect area, tui_buffer *buf);
68
69#endif