GRAPH_Widgets - How to add a label to axis

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Hi,

      Not sure what kind of label you want.

      If it is just a name of the visible data you might do like below. The code shows two ways of doing it. First is to simply draw string into the graph, the second is to use TEXT widget which are on top of the graph.

      C Source Code

      1. #include "DIALOG.h"
      2. #include <stdlib.h>
      3. /*********************************************************************
      4. *
      5. * Externals
      6. *
      7. **********************************************************************
      8. */
      9. /*********************************************************************
      10. *
      11. * Defines
      12. *
      13. **********************************************************************
      14. */
      15. #define ID_WINDOW_0 (GUI_ID_USER + 0x00)
      16. #define ID_GRAPH_0 (GUI_ID_USER + 0x01)
      17. #define ID_CHECKBOX_0 (GUI_ID_USER + 0x02)
      18. #define ID_CHECKBOX_1 (GUI_ID_USER + 0x03)
      19. #define ID_TEXT_0 (GUI_ID_USER + 0x04)
      20. #define ID_TEXT_1 (GUI_ID_USER + 0x05)
      21. #define DATA0_COLOR GUI_RED
      22. #define DATA1_COLOR GUI_YELLOW
      23. /*********************************************************************
      24. *
      25. * Static data
      26. *
      27. **********************************************************************
      28. */
      29. /*********************************************************************
      30. *
      31. * _aDialogCreate
      32. */
      33. static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
      34. { WINDOW_CreateIndirect, "Window", ID_WINDOW_0, 0, 0, 480, 272, 0, 0x0, 0 },
      35. { GRAPH_CreateIndirect, "Graph", ID_GRAPH_0, 10, 10, 460, 200, 0, 0x0, 0 },
      36. { CHECKBOX_CreateIndirect, "Checkbox0", ID_CHECKBOX_0, 150, 230, 80, 20, 0, 0x0, 0 },
      37. { CHECKBOX_CreateIndirect, "Checkbox1", ID_CHECKBOX_1, 250, 230, 80, 20, 0, 0x0, 0 },
      38. { TEXT_CreateIndirect, "Data 0", ID_TEXT_0, 385, 15, 80, 20, 0, 0x0, 0 },
      39. { TEXT_CreateIndirect, "Data 1", ID_TEXT_1, 385, 45, 80, 20, 0, 0x0, 0 },
      40. };
      41. static GRAPH_DATA_Handle _hDataYT0;
      42. static GRAPH_DATA_Handle _hDataYT1;
      43. /*********************************************************************
      44. *
      45. * Static code
      46. *
      47. **********************************************************************
      48. */
      49. static void _GraphUserDraw(WM_HWIN hGraph, int Stage) {
      50. GUI_RECT Rect;
      51. GUI_RECT TRect;
      52. char * apString[] = {
      53. "Data 0",
      54. "Data 1"
      55. };
      56. WM_HWIN hParent;
      57. WM_HWIN hItem;
      58. switch (Stage) {
      59. case GRAPH_DRAW_LAST:
      60. hParent = WM_GetParent(hGraph);
      61. hItem = WM_GetDialogItem(hParent, ID_CHECKBOX_0);
      62. TRect.x0 = 5;
      63. TRect.y0 = 5;
      64. if (CHECKBOX_GetState(hItem)) {
      65. TRect.x1 = TRect.x0 + 5 + GUI_GetStringDistX(apString[0]);
      66. TRect.y1 = TRect.y0 + 5 + GUI_GetFontDistY();
      67. GUI_SetColor(DATA0_COLOR);
      68. GUI_DrawRectEx(&TRect);
      69. GUI_DispStringInRect(apString[0], &TRect, GUI_TA_HCENTER | GUI_TA_VCENTER);
      70. TRect.y0 = TRect.y1 + 10;
      71. }
      72. hItem = WM_GetDialogItem(hParent, ID_CHECKBOX_1);
      73. if (CHECKBOX_GetState(hItem)) {
      74. TRect.x1 = TRect.x0 + 5 + GUI_GetStringDistX(apString[1]);
      75. TRect.y1 = TRect.y0 + 5 + GUI_GetFontDistY();
      76. GUI_SetColor(DATA1_COLOR);
      77. GUI_DrawRectEx(&TRect);
      78. GUI_DispStringInRect(apString[1], &TRect, GUI_TA_HCENTER | GUI_TA_VCENTER);
      79. }
      80. break;
      81. }
      82. }
      83. /*********************************************************************
      84. *
      85. * _cbDialog
      86. */
      87. static void _cbDialog(WM_MESSAGE * pMsg) {
      88. WM_HWIN hItem;
      89. int NCode;
      90. int Id;
      91. int xSize;
      92. switch (pMsg->MsgId) {
      93. case WM_INIT_DIALOG:
      94. //
      95. // Set bk color of window
      96. //
      97. WINDOW_SetBkColor(pMsg->hWin, GUI_DARKGREEN);
      98. //
      99. // Create data handles
      100. //
      101. xSize = WM_GetWindowSizeX(pMsg->hWin);
      102. _hDataYT0 = GRAPH_DATA_YT_Create(DATA0_COLOR, xSize, NULL, 0);
      103. _hDataYT1 = GRAPH_DATA_YT_Create(DATA1_COLOR, xSize, NULL, 0);
      104. //
      105. // Initialization of 'Graph'
      106. //
      107. hItem = WM_GetDialogItem(pMsg->hWin, ID_GRAPH_0);
      108. GRAPH_SetGridVis(hItem, 1);
      109. GRAPH_SetUserDraw(hItem, _GraphUserDraw);
      110. //
      111. // Initialization of 'Data 0'
      112. //
      113. hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_0);
      114. TEXT_SetTextColor(hItem, DATA0_COLOR);
      115. TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
      116. WM_HideWindow(hItem);
      117. //
      118. // Initialization of 'Data 1'
      119. //
      120. hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_1);
      121. TEXT_SetTextColor(hItem, DATA1_COLOR);
      122. TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
      123. WM_HideWindow(hItem);
      124. //
      125. // Initialization of 'Checkbox0'
      126. //
      127. hItem = WM_GetDialogItem(pMsg->hWin, ID_CHECKBOX_0);
      128. CHECKBOX_SetText(hItem, "Data 0");
      129. CHECKBOX_SetState(hItem, 1);
      130. //
      131. // Initialization of 'Checkbox1'
      132. //
      133. hItem = WM_GetDialogItem(pMsg->hWin, ID_CHECKBOX_1);
      134. CHECKBOX_SetText(hItem, "Data 1");
      135. CHECKBOX_SetState(hItem, 1);
      136. break;
      137. case WM_NOTIFY_PARENT:
      138. Id = WM_GetId(pMsg->hWinSrc);
      139. NCode = pMsg->Data.v;
      140. switch(Id) {
      141. case ID_CHECKBOX_0: // Notifications sent by 'Checkbox'
      142. switch(NCode) {
      143. case WM_NOTIFICATION_VALUE_CHANGED:
      144. if (CHECKBOX_GetState(pMsg->hWinSrc)) {
      145. hItem = WM_GetDialogItem(pMsg->hWin, ID_GRAPH_0);
      146. GRAPH_AttachData(hItem, _hDataYT0);
      147. hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_0);
      148. WM_ShowWindow(hItem);
      149. } else {
      150. hItem = WM_GetDialogItem(pMsg->hWin, ID_GRAPH_0);
      151. GRAPH_DetachData(hItem, _hDataYT0);
      152. hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_0);
      153. WM_HideWindow(hItem);
      154. }
      155. break;
      156. }
      157. break;
      158. case ID_CHECKBOX_1: // Notifications sent by 'Checkbox'
      159. switch(NCode) {
      160. case WM_NOTIFICATION_VALUE_CHANGED:
      161. if (CHECKBOX_GetState(pMsg->hWinSrc)) {
      162. hItem = WM_GetDialogItem(pMsg->hWin, ID_GRAPH_0);
      163. GRAPH_AttachData(hItem, _hDataYT1);
      164. hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_1);
      165. WM_ShowWindow(hItem);
      166. } else {
      167. hItem = WM_GetDialogItem(pMsg->hWin, ID_GRAPH_0);
      168. GRAPH_DetachData(hItem, _hDataYT1);
      169. hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_1);
      170. WM_HideWindow(hItem);
      171. }
      172. break;
      173. }
      174. break;
      175. }
      176. break;
      177. default:
      178. WM_DefaultProc(pMsg);
      179. break;
      180. }
      181. }
      182. /*********************************************************************
      183. *
      184. * Public code
      185. *
      186. **********************************************************************
      187. */
      188. /*********************************************************************
      189. *
      190. * MainTask
      191. */
      192. void MainTask(void) {
      193. GUI_Init();
      194. WM_MULTIBUF_Enable(1);
      195. GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, WM_HBKWIN, 0, 0);
      196. while (1) {
      197. GUI_Delay(50);
      198. GRAPH_DATA_YT_AddValue(_hDataYT0, (rand() % 50) + 10);
      199. GRAPH_DATA_YT_AddValue(_hDataYT1, (rand() % 50) + 50);
      200. }
      201. }
      202. /*************************** End of file ****************************/
      Display All
      Regards,
      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.
    • Hi,

      To get it like in the manual it's almost the same as in my example. Just add a Border to the sides of the graph. This area will be gray by default. Now you cann add a drawing like in my example.

      You also want to refer to the examples found on our website. Just download the archive and take a look into the examples named WIDGET_GraphXY and WIDGET_GraphYT.

      segger.com/downloads/emwin/emWin_Tutorials_SRC

      Regards,
      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.