diff --git a/cronjob/chart/definitions.h b/cronjob/chart/definitions.h index d403dae..b607040 100644 --- a/cronjob/chart/definitions.h +++ b/cronjob/chart/definitions.h @@ -37,6 +37,8 @@ #define DEFAULT_SHOW_INTERVAL "2d" #define DEFAULT_LABEL_INTERVAL "2h" #define DEFAULT_LABEL_SUM "no" +#define DEFAULT_ZERO_MIN "no" +#define DEFAULT_AS_BARS "no" #define DEFAULT_WIDTH "800" #define DEFAULT_HEIGHT "300" #define DEFAULT_SENS_ID "1" diff --git a/cronjob/chart/image_file/FreeSans.ttf b/cronjob/chart/image_file/FreeSans.ttf new file mode 100644 index 0000000..ea5632e Binary files /dev/null and b/cronjob/chart/image_file/FreeSans.ttf differ diff --git a/cronjob/chart/image_file/image_common.h b/cronjob/chart/image_file/image_common.h index dead559..4469c22 100644 --- a/cronjob/chart/image_file/image_common.h +++ b/cronjob/chart/image_file/image_common.h @@ -25,6 +25,8 @@ typedef struct image_cfg { double val_koeff; char *x_fmt_extra; char *x_fmt; + int zero_min; + int bars; img_color_ptr bg_color; img_color_ptr dia_bg_color; diff --git a/cronjob/chart/image_file/image_config.c b/cronjob/chart/image_file/image_config.c index a06d181..46744cd 100644 --- a/cronjob/chart/image_file/image_config.c +++ b/cronjob/chart/image_file/image_config.c @@ -53,6 +53,8 @@ static const config_keyword keywords[] = { {"height", read_int, &(img_cfg.height), DEFAULT_HEIGHT}, {"unit", read_str, &(img_cfg.unit), DEFAULT_UNIT}, {"sensor_id", read_int, &(img_cfg.sens_id), DEFAULT_SENS_ID}, + {"zero_min", read_yn, &(img_cfg.zero_min), DEFAULT_ZERO_MIN}, + {"as_bars", read_yn, &(img_cfg.bars), DEFAULT_AS_BARS}, {"x_axis_desc", read_str, &(img_cfg.x_desc), DEFAULT_X_AX_DESC}, {"y_axis_desc", read_str, &(img_cfg.y_desc), DEFAULT_Y_AX_DESC}, {"x_format", read_fmt_str, &(img_cfg.x_fmt), DEFAULT_X_FORMAT}, diff --git a/cronjob/chart/image_file/image_data.c b/cronjob/chart/image_file/image_data.c index c550c28..f4db420 100644 --- a/cronjob/chart/image_file/image_data.c +++ b/cronjob/chart/image_file/image_data.c @@ -28,16 +28,16 @@ static char *get_type_table_by_id(PGconn *, int ); /* Bibt die Liste mit den y-Labels zurueck */ -label_list_ptr get_y_label_list(int c_hight, int padding, int zero_min){ - int max_val = ceil( ( ((double)max->value_sum) / ((double)max->value_count) ) * img_cfg.val_koeff); - int min_val = floor( ( ((double)min->value_sum) / ((double)min->value_count) ) * img_cfg.val_koeff); +label_list_ptr get_y_label_list(int c_hight, int padding){ + int max_val = 0; + int min_val = 0; double factor = 0; int diff = 0; double real_diff = 0; double padd_val = 0; int temp = 0; double koeff = 1; - double interval = 0; + double interval = 0; int num = 0; int max_num = 0; int i; @@ -52,9 +52,16 @@ label_list_ptr get_y_label_list(int c_hight, int padding, int zero_min){ DEBUGOUT1("\nBaue y-Labels...\n"); DEBUGOUT4(" Max. Wert: %d, Min. Wert: %d (inkl. Koeffizient: %3.3f)\n", max_val, min_val, img_cfg.val_koeff); - if (zero_min){ - min_val = 0; + if (!img_cfg.label_sum){ + max_val = ceil( ( ((double)max->value_sum) / ((double)max->value_count) ) * img_cfg.val_koeff); + min_val = floor( ( ((double)min->value_sum) / ((double)min->value_count) ) * img_cfg.val_koeff); + } else { + max_val = ceil( ( ((double)max->value_sum) ) * img_cfg.val_koeff); + min_val = floor( ( ((double)min->value_sum) ) * img_cfg.val_koeff); } + if (img_cfg.zero_min){ + min_val = 0; + } diff = max_val - min_val; factor = ( ((double)c_hight - (2 * padding)) / ((double)diff) ); @@ -171,7 +178,11 @@ int scale_y_coords(pix_list_ptr ptr, int c_height){ DEBUGOUT1("\nBerechne y-Koordinaten:\n"); for (; temp; temp = temp->next){ - temp->y_pix_coord = (-1 * floor( ( ((double)temp->value_sum) / ((double)temp->value_count) ) * pix_per_scale)) + zero_line; + if(!img_cfg.label_sum){ + temp->y_pix_coord = (-1 * floor( ( ((double)temp->value_sum) / ((double)temp->value_count) ) * pix_per_scale)) + zero_line; + } else { + temp->y_pix_coord = (-1 * floor( ((double)temp->value_sum) * pix_per_scale)) + zero_line; + } DEBUGOUT3(" neue y-Koordinate: %d bei x: %d\n",temp->y_pix_coord, temp->x_pix_coord); } @@ -224,7 +235,7 @@ pix_list_ptr get_pix_list(int c_width){ int val_field; /* Id des Wert - Feldes */ long time_temp; /* Hilfsvariable */ int pix_coord; /* x - Koordinate, an die der Wert gehört */ - int i; /* Laufvariable zum durchlaufen des Datenbank-resuls */ + int i, s, t, u; /* Laufvariable zum durchlaufen des Datenbank-resuls */ long timestamp; pix_list_ptr list_ptr = NULL; /* Zeiger auf den Anfang der Wertliste */ pix_list_ptr temp_ptr = NULL; /* Zeiger zum durchlaufen der Wertliste */ @@ -238,6 +249,7 @@ pix_list_ptr get_pix_list(int c_width){ table = get_type_table_by_id(conn, img_cfg.sens_id); } + snprintf(query, BUFFSIZE, "SELECT round(date_part('epoch', current_timestamp)) AS now, round(date_part('epoch', timestamp)) AS times, %s AS val FROM %s WHERE sens_id=%d AND timestamp > (current_timestamp - INTERVAL '%d seconds') ORDER BY times ASC", img_cfg.table_field, table, img_cfg.sens_id, img_cfg.show_interval ); res = pg_check_exec(conn, query); @@ -250,6 +262,12 @@ pix_list_ptr get_pix_list(int c_width){ for (i = 0; i < PQntuples(res); i++){ timestamp = atol(PQgetvalue(res, i, time_field)); time_temp = timestamp - base_time; + if(img_cfg.bars){ + time_temp --; + if(time_temp < 1) + time_temp++; + time_temp = floor( ((double)time_temp) / ((double)img_cfg.label_interval) ) * img_cfg.label_interval; + } pix_coord = floor( ((double)time_temp) * seconds_per_pix) ; temp_ptr = add_pix_value(temp_ptr, timestamp, pix_coord, atoi( PQgetvalue(res, i, val_field) ) ); @@ -263,20 +281,33 @@ pix_list_ptr get_pix_list(int c_width){ max = temp_ptr; } - /* Min / Max ermitteln */ + } + + /* Min / Max ermitteln */ + temp_ptr = list_ptr; + u = 1; + s = 1; + t = 1; + for (; temp_ptr; temp_ptr = temp_ptr->next){ + if(!img_cfg.label_sum){ + t = temp_ptr->value_count; + s = min->value_count; + u = max->value_count; + } if (min != NULL){ - if ( (temp_ptr->value_sum / temp_ptr->value_count) < (min->value_sum / min->value_count) ) + if ( (temp_ptr->value_sum / t) < (min->value_sum / s) ) min = temp_ptr; } else { min = temp_ptr; } if (max != NULL){ - if ( (temp_ptr->value_sum / temp_ptr->value_count) > (max->value_sum / max->value_count) ) + if ( (temp_ptr->value_sum / t) > (max->value_sum / u) ) max = temp_ptr; } else { max = temp_ptr; } + } DEBUGOUT2(" %d Werte geholt \n", i); diff --git a/cronjob/chart/image_file/image_data.h b/cronjob/chart/image_file/image_data.h index a23a9f3..8686af9 100644 --- a/cronjob/chart/image_file/image_data.h +++ b/cronjob/chart/image_file/image_data.h @@ -53,4 +53,4 @@ int scale_y_coords(pix_list_ptr , int ); */ label_list_ptr get_x_label_list(int ); -label_list_ptr get_y_label_list(int, int, int); +label_list_ptr get_y_label_list(int, int); diff --git a/cronjob/chart/image_file/image_draw.c b/cronjob/chart/image_file/image_draw.c index f327c26..6258895 100644 --- a/cronjob/chart/image_file/image_draw.c +++ b/cronjob/chart/image_file/image_draw.c @@ -76,6 +76,7 @@ static gdImagePtr draw_image(gdImagePtr img){ int brect[8]; int y_label_max_width = 0; int i; + int temp_x2, temp_x1, temp_y1; char *buff; time_t ts; @@ -144,7 +145,7 @@ static gdImagePtr draw_image(gdImagePtr img){ /* horizontale linien + y - Labels */ - y_labels = get_y_label_list(dia_height, dia_y_padding, 0); + y_labels = get_y_label_list(dia_height, dia_y_padding); for (; y_labels; y_labels = y_labels->next){ gdImageLine(img, offset_x_left - 2, offset_y_top + y_labels->pos, img_cfg.width - offset_x_right, offset_y_top + y_labels->pos, diag_grid_x_c); y_label_d = calc_text_dim(y_labels->text, 7, 0); @@ -172,12 +173,29 @@ static gdImagePtr draw_image(gdImagePtr img){ /* Werte Zeichnen */ - for (; pix_list->next; pix_list = pix_list->next){ - gdImageLine(img, (offset_x_left + pix_list->x_pix_coord), (offset_y_top + pix_list->y_pix_coord), (offset_x_left + pix_list->next->x_pix_coord), (offset_y_top + pix_list->next->y_pix_coord), val_line_c); + if(!img_cfg.bars){ + for (; pix_list->next; pix_list = pix_list->next){ + gdImageLine(img, (offset_x_left + pix_list->x_pix_coord), (offset_y_top + pix_list->y_pix_coord), (offset_x_left + pix_list->next->x_pix_coord), (offset_y_top + pix_list->next->y_pix_coord), val_line_c); + } + } else { + if (zero_line != -1){ + temp_y1 = zero_line + offset_y_top; + } else { + temp_y1 = img_cfg.height - offset_y_bottom; + } + for (; pix_list; pix_list = pix_list->next){ + temp_x1 = pix_list->x_pix_coord + offset_x_left; + if (pix_list->next != NULL){ + temp_x2 = pix_list->next->x_pix_coord + offset_x_left; + } else { + temp_x2 = offset_x_left + dia_width; + } + gdImageFilledRectangle(img, temp_x1 + 3, (offset_y_top + pix_list->y_pix_coord), temp_x2 - 3 , temp_y1 , val_line_c); + } } /* Rahmen */ - gdImageRectangle(img, offset_x_left, offset_y_top, img_cfg.width - offset_x_right, img_cfg.height - offset_y_bottom, dia_border_c); + gdImageRectangle(img, offset_x_left, offset_y_top, img_cfg.width - offset_x_right, img_cfg.height - offset_y_bottom, dia_border_c); diff --git a/cronjob/chart/image_file/testimage.conf b/cronjob/chart/image_file/testimage.conf index deb76e4..3c3d15c 100644 --- a/cronjob/chart/image_file/testimage.conf +++ b/cronjob/chart/image_file/testimage.conf @@ -7,7 +7,6 @@ table_field temp gen_interval 1 show_interval 10d label_interval 1d -label_sum no width 800 height 300 ensor_id 1 @@ -18,6 +17,9 @@ x_axis_desc Zeit y_axis_desc Temparatur (°C) x_format %d.%m.%y x_format_extra %d.%m.%y| %H:%M +zero_min no +as_bars no +label_sum no