diff --git a/cronjob/chart/image_file/image_common.h b/cronjob/chart/image_file/image_common.h index 4aa234c..eacf491 100644 --- a/cronjob/chart/image_file/image_common.h +++ b/cronjob/chart/image_file/image_common.h @@ -27,6 +27,7 @@ typedef struct image_cfg { img_color_ptr dia_grid_color; img_color_ptr dia_border_color; img_color_ptr zero_line_color; + img_color_ptr label_color; diff --git a/cronjob/chart/image_file/image_config.c b/cronjob/chart/image_file/image_config.c index cc6b19e..d43d69d 100644 --- a/cronjob/chart/image_file/image_config.c +++ b/cronjob/chart/image_file/image_config.c @@ -58,6 +58,7 @@ static const config_keyword keywords[] = { {"dia_grid_color", read_color, &(img_cfg.dia_grid_color), ""}, {"dia_border_color", read_color, &(img_cfg.dia_border_color), ""}, {"zero_line_color", read_color, &(img_cfg.zero_line_color), ""}, + {"label_color", read_color, &(img_cfg.label_color), ""}, {"", NULL, NULL, ""} }; diff --git a/cronjob/chart/image_file/image_data.c b/cronjob/chart/image_file/image_data.c index 0f9c06d..3ae712e 100644 --- a/cronjob/chart/image_file/image_data.c +++ b/cronjob/chart/image_file/image_data.c @@ -15,6 +15,8 @@ static pix_list_ptr min = NULL; static pix_list_ptr max = NULL; +double real_min = 0; +double real_max = 0; static long base_time; /* Zeit an der 0-Koordinate (lt. Datenbank!) */ @@ -25,6 +27,94 @@ static PGresult *pg_check_exec(PGconn *, char *); static char *get_type_table_by_id(PGconn *, int ); + +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); + double factor = 0; + int diff = 0; + double real_diff = 0; + double padd_val = 0; + int temp = 0; + double koeff = 1; + int interval = 0; + int num = 0; + int i; + int new_val = 0; + char * buff = NULL; + + label_list_ptr ptr = NULL; + label_list_ptr new_ptr = NULL; + label_list_ptr temp_ptr = NULL; + + + 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; + } + + diff = max_val - min_val; + + factor = ( ((double)c_hight - (2 * padding)) / ((double)diff) ); + + padd_val = (1 / factor) * ((double)padding); + + real_min = min_val - padd_val; + real_max = max_val + padd_val; + real_diff = real_max - real_min; + + DEBUGOUT4(" Realer Max. Wert: %3.3f, Realer Min. Wert: %3.3f (inkl. Koeffizient: %3.3f)\n", real_max, real_min, img_cfg.val_koeff); + + temp = floor( ((double)diff) * 0.1); + + while (temp >= 10) { + koeff = koeff * 0.1; + temp = floor(koeff * ((double)temp)); + } + + interval = temp / koeff; + + num = floor( real_diff / interval ); + + DEBUGOUT2(" Interval: %d \n", interval); + + temp = ceil(real_min); + + + buff = malloc(sizeof(char)*BUFFSIZE); + + for (i = 0; i < num; i++){ + new_val = temp + (i * interval); + snprintf(buff, BUFFSIZE, "%d", new_val); + + new_ptr = malloc(sizeof(label_list_t)); + new_ptr->pos = floor( (real_max - ((double)new_val) ) * factor); + new_ptr->value = new_val; + new_ptr->text = strdup(buff); + new_ptr->next = NULL; + + if (ptr != NULL){ + temp_ptr->next = new_ptr; + temp_ptr = temp_ptr->next; + } else { + ptr = new_ptr; + temp_ptr = new_ptr; + } + + DEBUGOUT3(" Label '%s' an Position %d\n", new_ptr->text, new_ptr->pos); + } + + free(buff); + + DEBUGOUT2(" %d Labels generiert\n", num); + + return ptr; + +} + + /* Baut die Liste mit den Labels an der X-Achse */ label_list_ptr get_x_label_list(int c_width){ double factor = ((double)img_cfg.label_interval) * ( ((double)c_width) / ((double)img_cfg.show_interval) ); @@ -34,10 +124,12 @@ label_list_ptr get_x_label_list(int c_width){ label_list_ptr new_ptr = NULL; label_list_ptr temp_ptr = NULL; + DEBUGOUT1("\nBaue x-Labels...\n"); + for ( i = 1; i < num; i++ ) { new_ptr = malloc(sizeof(label_list_t)); new_ptr->pos = floor( ((double)i) * factor); - new_ptr->timestamp = base_time + (i * img_cfg.label_interval); + new_ptr->value = base_time + (i * img_cfg.label_interval); new_ptr->text = "NOT YET IMPLEMENTED"; new_ptr->next = NULL; @@ -48,30 +140,39 @@ label_list_ptr get_x_label_list(int c_width){ ptr = new_ptr; temp_ptr = new_ptr; } + + DEBUGOUT3(" Label '%s' an Position %d\n", new_ptr->text, new_ptr->pos); } - + + DEBUGOUT2(" %d Labels generiert\n", num); + return ptr; } /* Skaliert die X-Koordinaten der Punkte im angegebenem Bereich * ausführliche Beschreibung im header-file */ -int scale_y_coords(pix_list_ptr ptr, int c_height, int max_label, int min_label){ - int range = (max_label - min_label + 1) ; /* Anzahl von 0,1-Schritten */ +int scale_y_coords(pix_list_ptr ptr, int c_height){ + double range = (((real_max - real_min) / img_cfg.val_koeff ) + 1) ; /* Anzahl von 0,1-Schritten */ double pix_per_scale = ((double)c_height) / ((double)range); /* Pixel pro 0,1 */ pix_list_ptr temp = ptr; - int zero_line = floor( ((double)(max_label + 1)) * pix_per_scale); /* Nullinie */ + int zero_line = floor( ((double)((real_max / img_cfg.val_koeff) + 1)) * pix_per_scale); /* Nullinie */ 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); + temp->y_pix_coord = (-1 * floor( ( ((double)temp->value_sum) / ((double)temp->value_count) ) * pix_per_scale)) + zero_line; DEBUGOUT3(" neue y-Koordinate: %d bei x: %d\n",temp->y_pix_coord, temp->x_pix_coord); } - DEBUGOUT2(" Nullinie bei: %d\n", zero_line); + DEBUGOUT2(" Nullinie bei: %d\n", zero_line); + + if ((real_max - real_min + 1) >= real_max){ + return zero_line; + } else { + return -1; + } - return zero_line; } /* Maximaler wert */ diff --git a/cronjob/chart/image_file/image_data.h b/cronjob/chart/image_file/image_data.h index 2b19288..7b9fbfc 100644 --- a/cronjob/chart/image_file/image_data.h +++ b/cronjob/chart/image_file/image_data.h @@ -13,7 +13,7 @@ typedef struct pix_list { typedef struct label_list *label_list_ptr; typedef struct label_list { int pos; - long timestamp; + long value; char *text; label_list_ptr next; } label_list_t; @@ -38,15 +38,15 @@ pix_list_ptr get_max(); /* Skaliert die X-Koordinaten der Punkte im angegebenem Bereich * 1. Argument: die Pix-Liste die skaliert werden soll * 2. Argument: die anzahl der Pixel in y-Richtung - * 3. Argument: max. Wert - * 4. Argument: min. Wert - * Rueckgabe: Position der 0-Linie von oben aus + * Rueckgabe: Position der nullinie. (wenn nicht sichtbar -1) */ -int scale_y_coords(pix_list_ptr , int , int , int ); +int scale_y_coords(pix_list_ptr , int ); /* Baut die Liste mit den Labels an der X-Achse * 1. Argument: Breite des bildes * Rueckgabe: List mit den Labels */ -label_list_ptr get_x_label_list(int c_width); +label_list_ptr get_x_label_list(int ); + +label_list_ptr get_y_label_list(int, int, int); diff --git a/cronjob/chart/image_file/image_draw.c b/cronjob/chart/image_file/image_draw.c index 9cf1c01..2c4a1b9 100644 --- a/cronjob/chart/image_file/image_draw.c +++ b/cronjob/chart/image_file/image_draw.c @@ -49,6 +49,7 @@ static gdImagePtr draw_image(gdImagePtr img){ int dia_width = img_cfg.width - offset_x_left - offset_x_right; int dia_height = img_cfg.height - offset_y_top - offset_y_bottom; int zero_line = 0; + int dia_y_padding = 10; color val_line_c = alloc_alpha_color(img, img_cfg.dia_line_color); color zero_line_c = alloc_alpha_color(img, img_cfg.zero_line_color); @@ -58,23 +59,35 @@ static gdImagePtr draw_image(gdImagePtr img){ /* Werte holen */ pix_list = get_pix_list(dia_width); - /* y-Werte skalieren */ - zero_line = scale_y_coords(pix_list, dia_height, 400, -100); + + + + - x_labels = get_x_label_list(dia_width); /* Diagramhintergrund */ gdImageFilledRectangle(img, offset_x_left, offset_y_top, img_cfg.width - offset_x_right, img_cfg.height - offset_y_bottom, dia_bg_c); + + y_labels = get_y_label_list(dia_height, dia_y_padding, 0); + for (; y_labels; y_labels = y_labels->next){ + gdImageLine(img, offset_x_left, offset_y_top + y_labels->pos, img_cfg.width - offset_x_right, offset_y_top + y_labels->pos, diag_grid_c); + } + + /* y-Werte skalieren */ + zero_line = scale_y_coords(pix_list, dia_height); + /* Vertikale linien + x - Labels*/ + x_labels = get_x_label_list(dia_width); for(; x_labels; x_labels = x_labels->next){ gdImageLine(img, offset_x_left + x_labels->pos, offset_y_top, offset_x_left + x_labels->pos, img_cfg.height - offset_y_bottom, diag_grid_c); } /* Nullinie */ - gdImageLine(img, offset_x_left, zero_line + offset_y_top, img_cfg.width - offset_x_right, zero_line + offset_y_top, zero_line_c); + if (zero_line != -1) + gdImageLine(img, offset_x_left, zero_line + offset_y_top, img_cfg.width - offset_x_right, zero_line + offset_y_top, zero_line_c); /* Werte Zeichnen */ diff --git a/cronjob/chart/image_file/testimage.conf b/cronjob/chart/image_file/testimage.conf index 5993d18..10921cb 100644 --- a/cronjob/chart/image_file/testimage.conf +++ b/cronjob/chart/image_file/testimage.conf @@ -3,7 +3,7 @@ headline Testbild manual_table no manual_table_name auss2_data table_field temp -gen_interval 15 +gen_interval 1 show_interval 10d label_interval 1d label_sum no @@ -18,5 +18,5 @@ zero_line_color BF:80:80:40 #test dia_line_color 60:30:50:30 #test dia_grid_color 00:00:00:EF #test dia_border_color 60:30:50:af #test -#dia_color 60:30:50:af #test +label_color 60:30:50:af #test #dia_color 60:30:50:af #test