Skip to content

Commit e6dda61

Browse files
authored
Add titles
1 parent 91fed82 commit e6dda61

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

umplot.c

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,20 @@ typedef struct
4343
} Grid;
4444

4545

46+
typedef struct
47+
{
48+
char *x, *y, *graph;
49+
uint32_t color;
50+
int64_t fontSize;
51+
} Titles;
52+
53+
4654
typedef struct
4755
{
4856
Series *series;
4957
int64_t numSeries;
5058
Grid grid;
59+
Titles titles;
5160
} Plot;
5261

5362

@@ -187,7 +196,7 @@ static void drawGraph(const Plot *plot, const ScreenTransform *transform)
187196
}
188197

189198

190-
static void drawGrid(const Plot *plot, const ScreenTransform *transform)
199+
static void drawGrid(const Plot *plot, const ScreenTransform *transform, int *maxYLabelWidth)
191200
{
192201
if (plot->grid.xNumLines <= 0 || plot->grid.yNumLines <= 0)
193202
return;
@@ -231,6 +240,9 @@ static void drawGrid(const Plot *plot, const ScreenTransform *transform)
231240
}
232241

233242
// Horizontal grid
243+
if (maxYLabelWidth)
244+
*maxYLabelWidth = 0;
245+
234246
for (int j = 0, y = startPtScreen.y; y > clientRect.y; j++, y = startPtScreen.y + j * yStep * transform->yScale)
235247
{
236248
// Line
@@ -245,12 +257,54 @@ static void drawGrid(const Plot *plot, const ScreenTransform *transform)
245257
const int labelX = clientRect.x - labelWidth - plot->grid.fontSize;
246258
const int labelY = y - plot->grid.fontSize / 2;
247259

248-
DrawText(label, labelX, labelY, plot->grid.fontSize, *(Color *)&plot->grid.color);
260+
DrawText(label, labelX, labelY, plot->grid.fontSize, *(Color *)&plot->grid.color);
261+
262+
if (maxYLabelWidth && labelWidth > *maxYLabelWidth)
263+
*maxYLabelWidth = labelWidth;
249264
}
250265
}
251266
}
252267

253268

269+
static void drawTitles(const Plot *plot, const ScreenTransform *transform, int maxYLabelWidth)
270+
{
271+
Rectangle clientRect = getClientRect();
272+
273+
// Horizontal axis
274+
if (plot->titles.x && TextLength(plot->titles.x) > 0)
275+
{
276+
const int titleWidth = MeasureText(plot->titles.x, plot->titles.fontSize);
277+
278+
const int titleX = clientRect.x + clientRect.width / 2 - titleWidth / 2;
279+
const int titleY = clientRect.y + clientRect.height + 2 * plot->grid.fontSize + plot->titles.fontSize;
280+
281+
DrawText(plot->titles.x, titleX, titleY, plot->titles.fontSize, *(Color *)&plot->titles.color);
282+
}
283+
284+
// Vertical axis
285+
if (plot->titles.y && TextLength(plot->titles.y) > 0)
286+
{
287+
const int titleWidth = MeasureText(plot->titles.y, plot->titles.fontSize);
288+
289+
const int titleX = clientRect.x - 2 * plot->grid.fontSize - plot->titles.fontSize - maxYLabelWidth;
290+
const int titleY = clientRect.y + clientRect.height / 2 + titleWidth / 2;
291+
292+
DrawTextPro(GetFontDefault(), plot->titles.y, (Vector2){titleX, titleY}, (Vector2){0, 0}, -90.0, plot->titles.fontSize, 1, *(Color *)&plot->titles.color);
293+
}
294+
295+
// Graph
296+
if (plot->titles.graph && TextLength(plot->titles.graph) > 0)
297+
{
298+
const int titleWidth = MeasureText(plot->titles.graph, plot->titles.fontSize);
299+
300+
const int titleX = clientRect.x + clientRect.width / 2 - titleWidth / 2;
301+
const int titleY = clientRect.y - 2 * plot->titles.fontSize;
302+
303+
DrawText(plot->titles.graph, titleX, titleY, plot->titles.fontSize, *(Color *)&plot->titles.color);
304+
}
305+
}
306+
307+
254308
static void drawZoomRect(Rectangle zoomRect, const ScreenTransform *transform)
255309
{
256310
if (zoomRect.width < 0)
@@ -275,7 +329,7 @@ void umplot_plot(UmkaStackSlot *params, UmkaStackSlot *result)
275329
SetTraceLogLevel(LOG_ERROR);
276330
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
277331
InitWindow(640, 480, "UmPlot");
278-
SetTargetFPS(60);
332+
SetTargetFPS(30);
279333

280334
Rectangle clientRect = getClientRect();
281335
Rectangle zoomRect = clientRect;
@@ -327,15 +381,19 @@ void umplot_plot(UmkaStackSlot *params, UmkaStackSlot *result)
327381
BeginDrawing();
328382
ClearBackground(WHITE);
329383

330-
// Graph border
384+
// Border
331385
DrawRectangleLinesEx(clientRect, 1, BLACK);
332386

333387
// Grid
334-
drawGrid(plot, &transform);
388+
int maxYLabelWidth = 0;
389+
drawGrid(plot, &transform, &maxYLabelWidth);
335390

336391
// Graph
337392
drawGraph(plot, &transform);
338393

394+
// Titles
395+
drawTitles(plot, &transform, maxYLabelWidth);
396+
339397
// Zoom rectangle
340398
if (showZoomRect)
341399
drawZoomRect(zoomRect, &transform);

umplot.um

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,25 @@ type (
3131
color: uint32
3232
labelled: bool
3333
fontSize: int
34+
}
35+
36+
Titles* = struct {
37+
x, y, graph: str
38+
color: uint32
39+
fontSize: int
3440
}
3541

3642
Plot* = struct {
3743
series: []Series
3844
grid: Grid
45+
titles: Titles
3946
}
4047

4148
PlotImpl = struct {
4249
series: ^SeriesImpl
4350
numSeries: int
4451
grid: Grid
52+
titles: Titles
4553
}
4654
)
4755

@@ -98,6 +106,7 @@ fn init*(numSeries: int = 1, kind: int = STYLE_LINE): Plot {
98106
}
99107

100108
plt.grid = Grid{xNumLines: 5, yNumLines: 5, color: 0xFF808080, labelled: true, fontSize: 10}
109+
plt.titles = Titles{x: "", y: "", graph: "", color: 0xFF808080, fontSize: 10}
101110
return plt
102111
}
103112

@@ -112,7 +121,7 @@ fn (p: ^Plot) plot*() {
112121
seriesImpl[i] = SeriesImpl{&s.points[0], len(s.points), s.style}
113122
}
114123

115-
plotImpl := PlotImpl{&seriesImpl[0], len(seriesImpl), p.grid}
124+
plotImpl := PlotImpl{&seriesImpl[0], len(seriesImpl), p.grid, p.titles}
116125
umplot_plot(&plotImpl)
117126
}
118127

umplottest.um

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ fn main() {
1212

1313
plt.series[1].style.kind = umplot.STYLE_SCATTER
1414

15+
plt.titles.graph = "UmPlot demo"
16+
plt.titles.x = "Time (seconds)"
17+
plt.titles.y = "Value"
18+
1519
plt.plot()
1620
}

0 commit comments

Comments
 (0)