plot.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * File : plot.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-09-03 Grissiom first version
  13. */
  14. #include <rtgui/rtgui_system.h>
  15. #include <rtgui/dc.h>
  16. #include <rtgui/widgets/plot.h>
  17. #include <rtgui/widgets/plot_curve.h>
  18. static void _rtgui_plot_constructor(struct rtgui_plot *plot)
  19. {
  20. plot->base_x = plot->base_y = 0;
  21. plot->ptype = RTGUI_PLOT_TYPE_SCAN;
  22. plot->scale_x = plot->scale_y = 1;
  23. rtgui_object_set_event_handler(RTGUI_OBJECT(plot), rtgui_plot_event_handler);
  24. }
  25. static void _rtgui_plot_destructor(struct rtgui_plot *plot)
  26. {
  27. }
  28. DEFINE_CLASS_TYPE(plot, "plot",
  29. RTGUI_MV_VIEW_TYPE,
  30. _rtgui_plot_constructor,
  31. _rtgui_plot_destructor,
  32. sizeof(struct rtgui_plot));
  33. struct rtgui_plot *rtgui_plot_create(void)
  34. {
  35. struct rtgui_plot *plot;
  36. plot = RTGUI_PLOT(rtgui_widget_create(RTGUI_PLOT_TYPE));
  37. return plot;
  38. }
  39. RTM_EXPORT(rtgui_plot_create);
  40. void rtgui_plot_destroy(struct rtgui_plot *plot)
  41. {
  42. rtgui_mv_view_destroy(RTGUI_MV_VIEW(plot));
  43. }
  44. RTM_EXPORT(rtgui_plot_destroy);
  45. void rtgui_plot_set_base(struct rtgui_plot *plot,
  46. rtgui_plot_curve_dtype x, rtgui_plot_curve_dtype y)
  47. {
  48. plot->base_x = x;
  49. plot->base_y = y;
  50. }
  51. RTM_EXPORT(rtgui_plot_set_base);
  52. rt_inline int _rtgui_plot_curve_calc_x(struct rtgui_plot *plot, rtgui_plot_curve_dtype x)
  53. {
  54. return (x - plot->base_x) / plot->scale_x;
  55. }
  56. rt_inline int _rtgui_plot_curve_calc_y(struct rtgui_plot *plot, rtgui_plot_curve_dtype y, rt_uint16_t height)
  57. {
  58. return height - (y - plot->base_y) / plot->scale_y;
  59. }
  60. static void _rtgui_plot_curve_onpaint(
  61. struct rtgui_dc *dc,
  62. struct rtgui_plot *plot,
  63. struct rtgui_plot_curve *curve,
  64. rt_uint16_t start_idx,
  65. rt_uint16_t stop_idx)
  66. {
  67. struct rtgui_rect rect;
  68. rt_uint16_t height;
  69. int last_x, last_y;
  70. rtgui_color_t old_color;
  71. rtgui_plot_curve_dtype *x_data, *y_data;
  72. rtgui_dc_get_rect(dc, &rect);
  73. height = rtgui_rect_height(rect);
  74. old_color = RTGUI_DC_FC(dc);
  75. RTGUI_DC_FC(dc) = curve->color;
  76. x_data = rtgui_plot_curve_get_x(curve);
  77. y_data = rtgui_plot_curve_get_y(curve);
  78. if (x_data)
  79. {
  80. rt_size_t i;
  81. last_x = _rtgui_plot_curve_calc_x(plot, x_data[start_idx]);
  82. last_y = _rtgui_plot_curve_calc_y(plot, y_data[start_idx], height);
  83. for (i = start_idx + 1; i < stop_idx; i++)
  84. {
  85. int cur_x = _rtgui_plot_curve_calc_x(plot, x_data[i]);
  86. int cur_y = _rtgui_plot_curve_calc_y(plot, y_data[i], height);
  87. rtgui_dc_draw_line(dc,
  88. last_x, last_y,
  89. cur_x, cur_y);
  90. last_x = cur_x;
  91. last_y = cur_y;
  92. }
  93. }
  94. else
  95. {
  96. rt_size_t i;
  97. last_x = _rtgui_plot_curve_calc_x(plot, start_idx);
  98. last_y = _rtgui_plot_curve_calc_y(plot, y_data[start_idx], height);
  99. for (i = start_idx + 1; i < stop_idx; i++)
  100. {
  101. int cur_x = _rtgui_plot_curve_calc_x(plot, i);
  102. int cur_y = _rtgui_plot_curve_calc_y(plot, y_data[i], height);
  103. rtgui_dc_draw_line(dc,
  104. last_x, last_y,
  105. cur_x, cur_y);
  106. last_x = cur_x;
  107. last_y = cur_y;
  108. }
  109. }
  110. RTGUI_DC_FC(dc) = old_color;
  111. }
  112. static void _rtgui_plot_draw_curve(struct rtgui_plot *plot, struct rtgui_event *event)
  113. {
  114. int i;
  115. struct rtgui_dc *dc;
  116. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(plot));
  117. if (dc == RT_NULL)
  118. return;
  119. if (RTGUI_MV_VIEW(plot)->model_number == 1)
  120. {
  121. _rtgui_plot_curve_onpaint(dc, plot,
  122. RTGUI_PLOT_CURVE(RTGUI_MV_VIEW(plot)->model),
  123. 0, RTGUI_MV_MODEL(RTGUI_MV_VIEW(plot)->model)->length);
  124. }
  125. else
  126. {
  127. void **curve_array = (void **)RTGUI_MV_VIEW(plot)->model;
  128. for (i = 0; i < RTGUI_MV_VIEW(plot)->model_number; i++)
  129. {
  130. _rtgui_plot_curve_onpaint(dc, plot,
  131. RTGUI_PLOT_CURVE(curve_array[i]),
  132. 0, RTGUI_MV_MODEL(curve_array[i])->length);
  133. }
  134. }
  135. rtgui_dc_end_drawing(dc);
  136. }
  137. static void _rtgui_plot_update_scale(struct rtgui_plot *plot)
  138. {
  139. struct rtgui_plot_curve *curve;
  140. struct rtgui_rect rect;
  141. rtgui_plot_curve_dtype max_x = 0;
  142. rtgui_plot_curve_dtype min_x = 0;
  143. rtgui_plot_curve_dtype max_y = 0;
  144. rtgui_plot_curve_dtype min_y = 0;
  145. rt_uint32_t iter = 0;
  146. rtgui_widget_get_rect(RTGUI_WIDGET(plot), &rect);
  147. curve = RTGUI_PLOT_CURVE(
  148. rtgui_mv_view_foreach_in_model(RTGUI_MV_VIEW(plot), &iter));
  149. max_x = curve->max_x;
  150. min_x = curve->min_x;
  151. max_y = curve->max_y;
  152. min_y = curve->min_y;
  153. while (curve)
  154. {
  155. if (curve->max_x > max_x)
  156. max_x = curve->max_x;
  157. if (curve->min_x < min_x)
  158. min_x = curve->min_x;
  159. if (curve->max_y > max_y)
  160. max_y = curve->max_y;
  161. if (curve->min_y < min_y)
  162. min_y = curve->min_y;
  163. curve = RTGUI_PLOT_CURVE(
  164. rtgui_mv_view_foreach_in_model(RTGUI_MV_VIEW(plot), &iter));
  165. }
  166. plot->scale_x = (max_x - min_x + rtgui_rect_width(rect)) / rtgui_rect_width(rect);
  167. plot->scale_y = (max_y - min_y + rtgui_rect_height(rect)) / rtgui_rect_height(rect);
  168. }
  169. rt_bool_t rtgui_plot_ondraw(struct rtgui_plot *plot, struct rtgui_event *event)
  170. {
  171. struct rtgui_dc *dc;
  172. struct rtgui_rect rect;
  173. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(plot));
  174. if (dc == RT_NULL)
  175. return RT_FALSE;
  176. rtgui_widget_get_rect(RTGUI_WIDGET(plot), &rect);
  177. rtgui_dc_fill_rect(dc, &rect);
  178. _rtgui_plot_draw_curve(plot, event);
  179. rtgui_dc_end_drawing(dc);
  180. return RT_FALSE;
  181. }
  182. rt_bool_t rtgui_plot_onmvmodel(struct rtgui_plot *plot, struct rtgui_event *event)
  183. {
  184. struct rtgui_event_mv_model *emodel = (struct rtgui_event_mv_model *)event;
  185. RT_ASSERT(plot);
  186. RT_ASSERT(event);
  187. switch (plot->ptype)
  188. {
  189. case RTGUI_PLOT_TYPE_SCAN:
  190. _rtgui_plot_update_scale(plot);
  191. rtgui_plot_ondraw(plot, event);
  192. case RTGUI_PLOT_TYPE_INCREMENTAL:
  193. {
  194. rt_uint16_t old_sc_x = plot->scale_x;
  195. rt_uint16_t old_sc_y = plot->scale_y;
  196. _rtgui_plot_update_scale(plot);
  197. if (old_sc_x != plot->scale_x || old_sc_y != plot->scale_y)
  198. {
  199. /* we need to repaint the whole widget as the scale changed. */
  200. rtgui_plot_ondraw(plot, event);
  201. }
  202. else
  203. {
  204. /* get dc for _rtgui_plot_curve_onpaint */
  205. struct rtgui_dc *dc;
  206. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(plot));
  207. if (dc == RT_NULL)
  208. return RT_FALSE;
  209. /* only draw the newly recieved data */
  210. _rtgui_plot_curve_onpaint(dc, plot,
  211. RTGUI_PLOT_CURVE(emodel->model),
  212. emodel->first_data_changed_idx,
  213. emodel->last_data_changed_idx + 1);
  214. rtgui_dc_end_drawing(dc);
  215. }
  216. }
  217. return RT_TRUE;
  218. default:
  219. RT_ASSERT(0);
  220. }
  221. return RT_TRUE;
  222. }
  223. rt_bool_t rtgui_plot_event_handler(struct rtgui_object *object, struct rtgui_event *event)
  224. {
  225. struct rtgui_plot *plot;
  226. RTGUI_WIDGET_EVENT_HANDLER_PREPARE;
  227. plot = RTGUI_PLOT(object);
  228. switch (event->type)
  229. {
  230. case RTGUI_EVENT_PAINT:
  231. _rtgui_plot_update_scale(RTGUI_PLOT(object));
  232. return rtgui_plot_ondraw(plot, event);
  233. case RTGUI_EVENT_MV_MODEL:
  234. return rtgui_plot_onmvmodel(plot, event);
  235. default:
  236. return rtgui_widget_event_handler(object, event);
  237. }
  238. }
  239. RTM_EXPORT(rtgui_plot_event_handler);