-
Notifications
You must be signed in to change notification settings - Fork 16
/
draw_line_chart.c
274 lines (218 loc) · 6.93 KB
/
draw_line_chart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* File: canvas.c
* Author: AWTK Develop Team
* Brief: canvas demo
*
* Copyright (c) 2019 - 2021 Guangzhou ZHIYUAN Electronics Co.,Ltd.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* License file for more details.
*
*/
/**
* History:
* ================================================================
* 2019-01-09 Li XianJing <xianjimli@hotmail.com> created
*
*/
#include "awtk.h"
#define MAX_VALUE 1000
#define MAX_DATA_NR 2000
typedef struct _line_chart_info_t {
float max;
float* data;
uint32_t size;
uint32_t capacity;
color_t line_color;
color_t axis_color;
color_t text_color;
color_t bg_color;
uint32_t font_size;
uint32_t title_font_size;
const wchar_t* title;
} line_chart_info_t;
static line_chart_info_t* line_chart_info_create(uint32_t capacity, const wchar_t* title) {
line_chart_info_t* info = TKMEM_ZALLOC(line_chart_info_t);
return_value_if_fail(info != NULL, NULL);
info->line_color = color_parse("black");
info->bg_color = color_parse("#e0e0e0");
info->axis_color = color_parse("red");
info->text_color = color_parse("black");
info->font_size = 18;
info->title_font_size = 32;
info->title = title;
if (capacity > 0) {
info->data = TKMEM_ALLOC(capacity * sizeof(info->data[0]));
if (info->data != NULL) {
info->capacity = capacity;
}
}
return info;
}
static ret_t line_chart_info_destroy(line_chart_info_t* info) {
return_value_if_fail(info != NULL, RET_FAIL);
TKMEM_FREE(info->data);
TKMEM_FREE(info);
return RET_OK;
}
static ret_t line_chart_info_reset(line_chart_info_t* info) {
return_value_if_fail(info != NULL, RET_FAIL);
info->size = 0;
info->max = 0;
return RET_OK;
}
static ret_t line_chart_info_push(line_chart_info_t* info, float value) {
return_value_if_fail(info != NULL && info->size < info->capacity, RET_FAIL);
if (info->size == 0) {
info->max = value;
} else {
if (value > info->max) {
info->max = value;
}
}
/*log_debug("%u %f %f\n", info->size, value, info->max);*/
info->data[info->size++] = value;
return RET_OK;
}
static ret_t draw_line_chart(widget_t* canvas_widget, canvas_t* c, line_chart_info_t* info) {
uint32_t i = 0;
float x = 0;
float y = 0;
char str[32];
float x_scale = 0;
float y_scale = 0;
wstr_t wstr;
int32_t step = 0;
int32_t left_margin = 60;
int32_t right_margin = 10;
int32_t top_margin = 30;
int32_t bottom_margin = 30;
vgcanvas_t* vg = canvas_get_vgcanvas(c);
float y_range = info->max;
rect_t r = rect_init(canvas_widget->x, canvas_widget->y, canvas_widget->w, canvas_widget->h);
wstr_init(&wstr, 100);
/*绘制背景*/
canvas_set_fill_color(c, info->bg_color);
canvas_fill_rect(c, 0, 0, r.w, r.h);
/*准备vgcanvas*/
vgcanvas_save(vg);
vgcanvas_clip_rect(vg, r.x, r.y, r.w, r.h);
vgcanvas_translate(vg, r.x, r.y);
/*绘制坐标轴*/
vgcanvas_begin_path(vg);
vgcanvas_move_to(vg, left_margin, top_margin);
vgcanvas_line_to(vg, left_margin, r.h - bottom_margin);
vgcanvas_line_to(vg, r.w - right_margin, r.h - bottom_margin);
vgcanvas_set_stroke_color(vg, info->axis_color);
vgcanvas_stroke(vg);
/*将坐标原点移动到左下角*/
vgcanvas_translate(vg, left_margin, r.h - bottom_margin);
/*绘制数据线*/
x_scale = (float)(r.w - left_margin - right_margin) / (float)(info->size);
if (y_range > 0) {
y_scale = (float)(r.h - top_margin - bottom_margin) / y_range;
} else {
y_scale = 1;
}
vgcanvas_begin_path(vg);
for (i = 0; i < info->size; i++) {
x = i * x_scale;
y = -(info->max - info->data[i]) * y_scale;
if (i == 0) {
vgcanvas_move_to(vg, x, y);
} else {
vgcanvas_line_to(vg, x, y);
}
}
vgcanvas_set_stroke_color(vg, info->line_color);
vgcanvas_stroke(vg);
vgcanvas_restore(vg);
canvas_set_font(c, NULL, info->font_size);
canvas_set_text_color(c, info->text_color);
canvas_set_text_align(c, ALIGN_H_RIGHT, ALIGN_V_TOP);
/*文字不能用vgcanvas绘制,否则嵌入式平台不支持*/
/*绘制y坐标数值*/
#define NR 5
x = 0;
y = top_margin;
step = (canvas_widget->h - top_margin - bottom_margin) / NR;
for (i = 0; i <= NR; i++, y = y + step) {
r = rect_init(x, y, left_margin-2, 20);
uint32_t value = MAX_VALUE - i * MAX_VALUE / NR;
tk_snprintf(str, sizeof(str), "%u", value);
wstr_set_utf8(&wstr, str);
canvas_draw_text_in_rect(c, wstr.str, wstr.size, &r);
}
/*绘制x坐标数值*/
step = 50;
x = left_margin + step;
y = canvas_widget->h - bottom_margin;
canvas_set_text_align(c, ALIGN_H_CENTER, ALIGN_V_TOP);
for (i = 1; (x + step) < canvas_widget->w; i++, x = x + step) {
uint32_t value = i * step;
r = rect_init(x, y, step, 20);
tk_snprintf(str, sizeof(str), "%u", value);
wstr_set_utf8(&wstr, str);
canvas_draw_text_in_rect(c, wstr.str, wstr.size, &r);
}
/*绘制标题*/
canvas_set_font(c, NULL, info->title_font_size);
r = rect_init(0, 0, canvas_widget->w, top_margin);
canvas_draw_text_in_rect(c, info->title, wcslen(info->title), &r);
wstr_reset(&wstr);
return RET_OK;
}
static ret_t on_paint_event(void* ctx, event_t* evt) {
uint64_t cost = 0;
uint64_t start_time = time_now_ms();
canvas_t* c = paint_event_cast(evt)->c;
widget_t* canvas_widget = WIDGET(evt->target);
line_chart_info_t* info = (line_chart_info_t*)ctx;
draw_line_chart(canvas_widget, c, info);
cost = time_now_ms() - start_time;
log_debug("paint cost: %d ms\n", (int32_t)cost);
return RET_OK;
}
static ret_t on_canvas_destroy(void* ctx, event_t* evt) {
line_chart_info_t* info = (line_chart_info_t*)ctx;
line_chart_info_destroy(info);
return RET_REMOVE;
}
static ret_t line_chart_info_gen_data(line_chart_info_t* info, uint32_t nr) {
line_chart_info_reset(info);
while (nr > 0) {
float value = random() % MAX_VALUE;
line_chart_info_push(info, value);
nr--;
}
return RET_OK;
}
static ret_t on_timer_redraw(const timer_info_t* info) {
widget_t* canvas = WIDGET(info->ctx);
line_chart_info_t* data = (line_chart_info_t*)widget_get_prop_pointer(canvas, "data");
line_chart_info_gen_data(data, MAX_DATA_NR);
widget_invalidate(canvas, NULL);
return RET_REPEAT;
}
ret_t application_init() {
widget_t* win = window_open("line_chart");
widget_t* canvas = widget_lookup(win, "chart", TRUE);
line_chart_info_t* info = line_chart_info_create(2048, L"Canvas demo");
line_chart_info_gen_data(info, MAX_DATA_NR);
widget_add_timer(canvas, on_timer_redraw, 1000);
widget_set_prop_pointer(canvas, "data", info);
widget_on(canvas, EVT_PAINT, on_paint_event, info);
widget_on(canvas, EVT_DESTROY, on_canvas_destroy, info);
return RET_OK;
}
ret_t application_exit() {
log_debug("application_exit\n");
return RET_OK;
}
#undef LCD_WIDTH
#undef LCD_HEIGHT
#define LCD_WIDTH 1024
#define LCD_HEIGHT 720
#include "awtk_main.inc"