@@ -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+
4654typedef 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+
254308static 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 );
0 commit comments