1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-21 06:53:01 +08:00

Merge pull request #943 from ali-rostami/master

adding area chart
This commit is contained in:
Gabor Kiss-Vamosi 2019-03-17 05:27:16 +01:00 committed by GitHub
commit 80738425f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 27 deletions

View File

@ -33,14 +33,14 @@ static void point_swap(lv_point_t * p1, lv_point_t * p2);
/**********************
* GLOBAL FUNCTIONS
**********************/
#if LV_USE_TRIANGLE != 0
/**
*
* @param points pointer to an array with 3 points
* @param mask the triangle will be drawn only in this mask
* @param color color of the triangle
* @param opa_scale scale down all opacities by the factor
*/
void lv_draw_triangle(const lv_point_t * points, const lv_area_t * mask, lv_color_t color)
void lv_draw_triangle(const lv_point_t * points, const lv_area_t * mask, lv_color_t color, lv_opa_t opa_scale)
{
lv_point_t tri[3];
@ -138,14 +138,12 @@ void lv_draw_triangle(const lv_point_t * points, const lv_area_t * mask, lv_colo
} while(edge2.y == y2_tmp);
}
}
#endif
/**********************
* STATIC FUNCTIONS
**********************/
#if LV_USE_TRIANGLE != 0
/**
* Swap two points
* p1 pointer to the first point
@ -164,5 +162,3 @@ static void point_swap(lv_point_t * p1, lv_point_t * p2)
p2->y = tmp.y;
}
#endif

View File

@ -26,18 +26,15 @@ extern "C" {
/**********************
* GLOBAL PROTOTYPES
**********************/
/*Experimental use for 3D modeling*/
#define LV_USE_TRIANGLE 1
#if LV_USE_TRIANGLE != 0
/**
*
* @param points pointer to an array with 3 points
* @param mask the triangle will be drawn only in this mask
* @param color color of the triangle
* @param opa_scale scale down all opacities by the factor
*/
void lv_draw_triangle(const lv_point_t * points, const lv_area_t * mask, lv_color_t color);
#endif
void lv_draw_triangle(const lv_point_t * points, const lv_area_t * mask, lv_color_t color, lv_opa_t opa_scale);
/**********************
* MACROS

View File

@ -35,6 +35,7 @@ static void lv_chart_draw_lines(lv_obj_t * chart, const lv_area_t * mask);
static void lv_chart_draw_points(lv_obj_t * chart, const lv_area_t * mask);
static void lv_chart_draw_cols(lv_obj_t * chart, const lv_area_t * mask);
static void lv_chart_draw_vertical_lines(lv_obj_t * chart, const lv_area_t * mask);
static void lv_chart_draw_areas(lv_obj_t * chart, const lv_area_t * mask);
/**********************
* STATIC VARIABLES
@ -490,6 +491,7 @@ static bool lv_chart_design(lv_obj_t * chart, const lv_area_t * mask, lv_design_
if(ext->type & LV_CHART_TYPE_COLUMN) lv_chart_draw_cols(chart, mask);
if(ext->type & LV_CHART_TYPE_POINT) lv_chart_draw_points(chart, mask);
if(ext->type & LV_CHART_TYPE_VERTICAL_LINE) lv_chart_draw_vertical_lines(chart, mask);
if(ext->type & LV_CHART_TYPE_AREA) lv_chart_draw_areas(chart, mask);
}
return true;
}
@ -819,4 +821,69 @@ static void lv_chart_draw_vertical_lines(lv_obj_t * chart, const lv_area_t * mas
}
}
}
/**
* Draw the data lines as areas on a chart
* @param obj pointer to chart object
*/
static void lv_chart_draw_areas(lv_obj_t * chart, const lv_area_t * mask)
{
lv_chart_ext_t * ext = lv_obj_get_ext_attr(chart);
uint16_t i;
lv_point_t p1;
lv_point_t p2;
lv_coord_t w = lv_obj_get_width(chart);
lv_coord_t h = lv_obj_get_height(chart);
lv_coord_t x_ofs = chart->coords.x1;
lv_coord_t y_ofs = chart->coords.y1;
int32_t y_tmp;
lv_coord_t p_prev;
lv_coord_t p_act;
lv_chart_series_t * ser;
lv_opa_t opa_scale = lv_obj_get_opa_scale(chart);
lv_style_t style;
lv_style_copy(&style, &lv_style_plain);
style.line.opa = ext->series.opa;
style.line.width = ext->series.width;
/*Go through all data lines*/
LV_LL_READ_BACK(ext->series_ll, ser) {
style.line.color = ser->color;
p1.x = 0 + x_ofs;
p2.x = 0 + x_ofs;
p_prev = ser->start_point;
y_tmp = (int32_t)((int32_t) ser->points[p_prev] - ext->ymin) * h;
y_tmp = y_tmp / (ext->ymax - ext->ymin);
p2.y = h - y_tmp + y_ofs;
for(i = 1; i < ext->point_cnt; i ++) {
p1.x = p2.x;
p1.y = p2.y;
p2.x = ((w * i) / (ext->point_cnt - 1)) + x_ofs;
p_act = (ser->start_point + i) % ext->point_cnt;
y_tmp = (int32_t)((int32_t) ser->points[p_act] - ext->ymin) * h;
y_tmp = y_tmp / (ext->ymax - ext->ymin);
p2.y = h - y_tmp + y_ofs;
if(ser->points[p_prev] != LV_CHART_POINT_DEF && ser->points[p_act] != LV_CHART_POINT_DEF) {
lv_point_t triangle_points[3];
triangle_points[0] = p1;
triangle_points[1].x = p2.x;
triangle_points[1].y = y_ofs + h;
triangle_points[2].x = p1.x;
triangle_points[2].y = y_ofs + h;
lv_draw_triangle(triangle_points, mask, style.line.color, opa_scale);
triangle_points[2] = p2;
lv_draw_triangle(triangle_points, mask, style.line.color, opa_scale);
}
p_prev = p_act;
}
}
}
#endif

View File

@ -32,6 +32,18 @@ extern "C" {
/**********************
* TYPEDEFS
**********************/
/*Chart types*/
enum
{
LV_CHART_TYPE_LINE = 0x01, /*Connect the points with lines*/
LV_CHART_TYPE_COLUMN = 0x02, /*Draw columns*/
LV_CHART_TYPE_POINT = 0x04, /*Draw circles on the points*/
LV_CHART_TYPE_VERTICAL_LINE = 0x08, /*Draw vertical lines on points (useful when chart width == point count)*/
LV_CHART_TYPE_AREA = 0x10, /*Draw area chart*/
};
typedef uint8_t lv_chart_type_t;
typedef struct
{
lv_coord_t * points;
@ -44,13 +56,13 @@ typedef struct
{
/*No inherited ext*/ /*Ext. of ancestor*/
/*New data for this type */
lv_ll_t series_ll; /*Linked list for the data line pointers (stores lv_chart_dl_t)*/
lv_coord_t ymin; /*y min value (used to scale the data)*/
lv_coord_t ymax; /*y max value (used to scale the data)*/
uint8_t hdiv_cnt; /*Number of horizontal division lines*/
uint8_t vdiv_cnt; /*Number of vertical division lines*/
uint16_t point_cnt; /*Point number in a data line*/
uint8_t type :4; /*Line, column or point chart (from 'lv_chart_type_t')*/
lv_ll_t series_ll; /*Linked list for the data line pointers (stores lv_chart_dl_t)*/
lv_coord_t ymin; /*y min value (used to scale the data)*/
lv_coord_t ymax; /*y max value (used to scale the data)*/
uint8_t hdiv_cnt; /*Number of horizontal division lines*/
uint8_t vdiv_cnt; /*Number of vertical division lines*/
uint16_t point_cnt; /*Point number in a data line*/
lv_chart_type_t type; /*Line, column or point chart (from 'lv_chart_type_t')*/
struct {
lv_coord_t width; /*Line width or point radius*/
uint8_t num; /*Number of data lines in dl_ll*/
@ -59,15 +71,6 @@ typedef struct
} series;
} lv_chart_ext_t;
/*Chart types*/
enum
{
LV_CHART_TYPE_LINE = 0x01, /*Connect the points with lines*/
LV_CHART_TYPE_COLUMN = 0x02, /*Draw columns*/
LV_CHART_TYPE_POINT = 0x04, /*Draw circles on the points*/
LV_CHART_TYPE_VERTICAL_LINE = 0x08, /*Draw vertical lines on points (useful when chart width == point count)*/
};
typedef uint8_t lv_chart_type_t;
/**********************