Filled line graph with emwin - no create handle function for vertical line or filled polygon

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

    • Filled line graph with emwin - no create handle function for vertical line or filled polygon

      Hi,

      I'm struggling with how to plot a filled line graph using emWin widget.
      I know that there is no ready to use widget for a filled graph and that this topic has been already asked in the past in this forum, here:
      How do plot a filled line graph using emWin widget?

      But I really don't understand how I can achieve the result with the suggestion provided.

      If I have the following to create a YT Graph:

      hData = GRAPH_DATA_YT_Create( ... );
      GRAPH_AttachData(hGraph, hData);
      GRAPH_DATA_YT_AddValue(hData, ....);

      I would like to do the same with something that for each new value, build a vertical line,
      but there is no "create" function that allows me to obtaon a handle doing something like this:

      hLine = GRAPH_LINE_Create ();

      Alternatively, I would like to make a filled polygon adding each new value,
      but still there is no "create" polygon handle like:

      hPolygon = GRAPH_POLYGON_Create ();

      Please can you help me?
      Many thanks in advance.
      Best,

      Giovanni
    • Hi,

      I guess you have already a solution, but I'll post it just in case other people have a similar problem.

      Here is the code showing how to hook into the drawing process of the GRAPH widget and fill the area under the GRAPH. You might want to improve it by searching the largest rectangular area under the GRAPH and fill it with GUI_FillRect() instead of line by line.

      You can use the switch USE_PUNCH_OUT_DEVICE fill the the area with a gradient.

      C Source Code

      1. #include "DIALOG.h"
      2. #include "stdlib.h"
      3. /*********************************************************************
      4. *
      5. * Defines
      6. *
      7. **********************************************************************
      8. */
      9. #define USE_PUNCH_OUT_DEVICE 0
      10. #define LEFT_BORDER (30)
      11. #define Y_OFFSET (75)
      12. static GRAPH_DATA_Handle _hData;
      13. /*********************************************************************
      14. *
      15. * Static code
      16. *
      17. **********************************************************************
      18. */
      19. /*********************************************************************
      20. *
      21. * _UserDrawGraph
      22. *
      23. * Function description:
      24. * This function is periodically called: after the border was drawn,
      25. * before anything is drawn except the background being filled,
      26. * and at last when the final drawing operation is done.
      27. *
      28. * Notes:
      29. * Therefore you're not able to completely draw the widget yourself,
      30. * but instead just add drawings.
      31. *
      32. */
      33. static void _UserDrawGraph(WM_HWIN hWin, int Stage) {
      34. GUI_RECT Rect;
      35. int xSize;
      36. int i;
      37. I16 Value;
      38. #if USE_PUNCH_OUT_DEVICE
      39. int ySize;
      40. GUI_MEMDEV_Handle hMemGradient;
      41. GUI_MEMDEV_Handle hMemMask;
      42. #endif
      43. switch (Stage) {
      44. case GRAPH_DRAW_LAST:
      45. #if USE_PUNCH_OUT_DEVICE
      46. WM_GetClientRect(&Rect);
      47. xSize = WM_GetWindowSizeX(hWin);
      48. ySize = WM_GetWindowSizeY(hWin);
      49. hMemGradient = GUI_MEMDEV_CreateFixed32(Rect.x0, Rect.y0, xSize - 1, ySize - 1);
      50. hMemMask = GUI_MEMDEV_CreateFixed (Rect.x0, Rect.y0, xSize - 1, ySize - 1, GUI_MEMDEV_NOTRANS, GUI_MEMDEV_APILIST_8, GUICC_8);
      51. GUI_MEMDEV_Select(hMemGradient);
      52. GUI_SetBkColor(GUI_TRANSPARENT);
      53. GUI_Clear();
      54. GUI_DrawGradientV(Rect.x0, Rect.y0, Rect.x1, Rect.y1 + 60, GUI_RED, GUI_TRANSPARENT);
      55. GUI_MEMDEV_Select(hMemMask);
      56. GUI_SetBkColor(GUI_BLACK);
      57. GUI_Clear();
      58. for (i = 0; i < xSize; i++) {
      59. if (GRAPH_DATA_YT_GetValue(_hData, &Value, i) == 1) {
      60. break;
      61. }
      62. if (Value) {
      63. GUI_SetColor(GUI_WHITE);
      64. GUI_DrawVLine(Rect.x0 + i + LEFT_BORDER + 1, Rect.y1 - Value - Y_OFFSET, Rect.y1);
      65. }
      66. }
      67. GUI_MEMDEV_Select(0);
      68. GUI_MEMDEV_PunchOutDevice(hMemGradient, hMemMask);
      69. GUI_MEMDEV_Write(hMemGradient);
      70. GUI_MEMDEV_Delete(hMemGradient);
      71. GUI_MEMDEV_Delete(hMemMask);
      72. #else
      73. GUI_USE_PARA(hWin);
      74. xSize = LCD_GetXSize();
      75. WM_GetClientRect(&Rect);
      76. for (i = 0; i < xSize; i++) {
      77. if (GRAPH_DATA_YT_GetValue(_hData, &Value, i) == 1) {
      78. break;
      79. }
      80. if (Value) {
      81. GUI_SetColor(GUI_RED);
      82. GUI_DrawVLine(Rect.x0 + i + LEFT_BORDER + 1, Rect.y1 - Value - Y_OFFSET, Rect.y1);
      83. }
      84. }
      85. #endif
      86. break;
      87. }
      88. }
      89. /*********************************************************************
      90. *
      91. * Public code
      92. *
      93. **********************************************************************
      94. */
      95. /*********************************************************************
      96. *
      97. * MainTask
      98. */
      99. void MainTask(void) {
      100. GRAPH_SCALE_Handle hScale;
      101. WM_HWIN hGraph;
      102. int Angle;
      103. int Value;
      104. //
      105. // Init GUI
      106. //
      107. GUI_Init();
      108. //
      109. // Enable multi-buffering to avoid flickering during the continuous update of the graph.
      110. //
      111. WM_MULTIBUF_Enable(1);
      112. //
      113. // Create graph widget
      114. // For more explanation on how to create graphs,
      115. // see the sample GUI_GRAPH_GraphUsage.c
      116. //
      117. hGraph = GRAPH_CreateEx(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, GRAPH_CF_GRID_FIXED_X, GUI_ID_GRAPH0);
      118. GRAPH_SetGridVis(hGraph, 1);
      119. GRAPH_SetGridDistX(hGraph, 10);
      120. GRAPH_SetGridDistY(hGraph, 10);
      121. GRAPH_SetGridOffY(hGraph, -5);
      122. GRAPH_SetBorder(hGraph, LEFT_BORDER, 0, 0, 0);
      123. GRAPH_SetColor(hGraph, GUI_WHITE, GRAPH_CI_BK);
      124. GRAPH_SetColor(hGraph, GUI_GRAY_E7, GRAPH_CI_GRID);
      125. GRAPH_SetColor(hGraph, GUI_WHITE, GRAPH_CI_BORDER);
      126. //
      127. // Create data object
      128. //
      129. _hData = GRAPH_DATA_YT_Create(GUI_GRAY_7C, LCD_GetXSize() - LEFT_BORDER, NULL, 0);
      130. GRAPH_DATA_YT_SetAlign(_hData, GRAPH_ALIGN_LEFT);
      131. GRAPH_DATA_YT_SetOffY(_hData, 75);
      132. GRAPH_AttachData(hGraph, _hData);
      133. //
      134. // Create scale object
      135. //
      136. hScale = GRAPH_SCALE_Create(15, GUI_TA_HCENTER | GUI_TA_VCENTER, GRAPH_SCALE_CF_VERTICAL, 1);
      137. GRAPH_SCALE_SetNumDecs(hScale, 0);
      138. GRAPH_SCALE_SetFactor(hScale, 1);
      139. GRAPH_SCALE_SetTickDist(hScale, 20);
      140. GRAPH_SCALE_SetOff(hScale, 130);
      141. GRAPH_SCALE_SetTextColor(hScale, GUI_GRAY_C8);
      142. GRAPH_AttachScale(hGraph, hScale);
      143. //
      144. // Set a user draw function to the graph
      145. //
      146. GRAPH_SetUserDraw(hGraph, _UserDrawGraph);
      147. //
      148. // Continuously feed the graph with data to create a sine curve
      149. //
      150. Angle = 0;
      151. while (1) {
      152. GUI_Delay(10);
      153. Value = GUI__SinHQ(((Angle++) % 360) * 1000);
      154. Value = (50 * Value) >> 16;
      155. GRAPH_DATA_YT_AddValue(_hData, (I16)Value + 60);
      156. }
      157. }
      158. /*************************** End of file ****************************/
      Display All
      Best 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.