charts nearly finished

git-svn-id: file:///home/jan/tmp/wetterstation/trunk@98 dd492736-c11a-0410-ad51-8c26713eaf7f
This commit is contained in:
losinshi 2006-09-15 16:05:35 +00:00
parent b8188a16e9
commit 1f981bfb92
8 changed files with 74 additions and 17 deletions

View File

@ -37,6 +37,8 @@
#define DEFAULT_SHOW_INTERVAL "2d" #define DEFAULT_SHOW_INTERVAL "2d"
#define DEFAULT_LABEL_INTERVAL "2h" #define DEFAULT_LABEL_INTERVAL "2h"
#define DEFAULT_LABEL_SUM "no" #define DEFAULT_LABEL_SUM "no"
#define DEFAULT_ZERO_MIN "no"
#define DEFAULT_AS_BARS "no"
#define DEFAULT_WIDTH "800" #define DEFAULT_WIDTH "800"
#define DEFAULT_HEIGHT "300" #define DEFAULT_HEIGHT "300"
#define DEFAULT_SENS_ID "1" #define DEFAULT_SENS_ID "1"

Binary file not shown.

View File

@ -25,6 +25,8 @@ typedef struct image_cfg {
double val_koeff; double val_koeff;
char *x_fmt_extra; char *x_fmt_extra;
char *x_fmt; char *x_fmt;
int zero_min;
int bars;
img_color_ptr bg_color; img_color_ptr bg_color;
img_color_ptr dia_bg_color; img_color_ptr dia_bg_color;

View File

@ -53,6 +53,8 @@ static const config_keyword keywords[] = {
{"height", read_int, &(img_cfg.height), DEFAULT_HEIGHT}, {"height", read_int, &(img_cfg.height), DEFAULT_HEIGHT},
{"unit", read_str, &(img_cfg.unit), DEFAULT_UNIT}, {"unit", read_str, &(img_cfg.unit), DEFAULT_UNIT},
{"sensor_id", read_int, &(img_cfg.sens_id), DEFAULT_SENS_ID}, {"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}, {"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}, {"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}, {"x_format", read_fmt_str, &(img_cfg.x_fmt), DEFAULT_X_FORMAT},

View File

@ -28,16 +28,16 @@ static char *get_type_table_by_id(PGconn *, int );
/* Bibt die Liste mit den y-Labels zurueck */ /* Bibt die Liste mit den y-Labels zurueck */
label_list_ptr get_y_label_list(int c_hight, int padding, int zero_min){ label_list_ptr get_y_label_list(int c_hight, int padding){
int max_val = ceil( ( ((double)max->value_sum) / ((double)max->value_count) ) * img_cfg.val_koeff); int max_val = 0;
int min_val = floor( ( ((double)min->value_sum) / ((double)min->value_count) ) * img_cfg.val_koeff); int min_val = 0;
double factor = 0; double factor = 0;
int diff = 0; int diff = 0;
double real_diff = 0; double real_diff = 0;
double padd_val = 0; double padd_val = 0;
int temp = 0; int temp = 0;
double koeff = 1; double koeff = 1;
double interval = 0; double interval = 0;
int num = 0; int num = 0;
int max_num = 0; int max_num = 0;
int i; 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"); 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); DEBUGOUT4(" Max. Wert: %d, Min. Wert: %d (inkl. Koeffizient: %3.3f)\n", max_val, min_val, img_cfg.val_koeff);
if (zero_min){ if (!img_cfg.label_sum){
min_val = 0; 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; diff = max_val - min_val;
factor = ( ((double)c_hight - (2 * padding)) / ((double)diff) ); 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"); DEBUGOUT1("\nBerechne y-Koordinaten:\n");
for (; temp; temp = temp->next){ 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); 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 */ int val_field; /* Id des Wert - Feldes */
long time_temp; /* Hilfsvariable */ long time_temp; /* Hilfsvariable */
int pix_coord; /* x - Koordinate, an die der Wert gehört */ 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; long timestamp;
pix_list_ptr list_ptr = NULL; /* Zeiger auf den Anfang der Wertliste */ pix_list_ptr list_ptr = NULL; /* Zeiger auf den Anfang der Wertliste */
pix_list_ptr temp_ptr = NULL; /* Zeiger zum durchlaufen 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); 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 ); 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); 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++){ for (i = 0; i < PQntuples(res); i++){
timestamp = atol(PQgetvalue(res, i, time_field)); timestamp = atol(PQgetvalue(res, i, time_field));
time_temp = timestamp - base_time; 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) ; 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) ) ); 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; 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 (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; min = temp_ptr;
} else { } else {
min = temp_ptr; min = temp_ptr;
} }
if (max != NULL){ 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; max = temp_ptr;
} else { } else {
max = temp_ptr; max = temp_ptr;
} }
} }
DEBUGOUT2(" %d Werte geholt \n", i); DEBUGOUT2(" %d Werte geholt \n", i);

View File

@ -53,4 +53,4 @@ int scale_y_coords(pix_list_ptr , int );
*/ */
label_list_ptr get_x_label_list(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);

View File

@ -76,6 +76,7 @@ static gdImagePtr draw_image(gdImagePtr img){
int brect[8]; int brect[8];
int y_label_max_width = 0; int y_label_max_width = 0;
int i; int i;
int temp_x2, temp_x1, temp_y1;
char *buff; char *buff;
time_t ts; time_t ts;
@ -144,7 +145,7 @@ static gdImagePtr draw_image(gdImagePtr img){
/* horizontale linien + y - Labels */ /* 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){ 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); 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); y_label_d = calc_text_dim(y_labels->text, 7, 0);
@ -172,12 +173,29 @@ static gdImagePtr draw_image(gdImagePtr img){
/* Werte Zeichnen */ /* Werte Zeichnen */
for (; pix_list->next; pix_list = pix_list->next){ if(!img_cfg.bars){
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); 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 */ /* 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);

View File

@ -7,7 +7,6 @@ table_field temp
gen_interval 1 gen_interval 1
show_interval 10d show_interval 10d
label_interval 1d label_interval 1d
label_sum no
width 800 width 800
height 300 height 300
ensor_id 1 ensor_id 1
@ -18,6 +17,9 @@ x_axis_desc Zeit
y_axis_desc Temparatur (°C) y_axis_desc Temparatur (°C)
x_format %d.%m.%y x_format %d.%m.%y
x_format_extra %d.%m.%y| %H:%M x_format_extra %d.%m.%y| %H:%M
zero_min no
as_bars no
label_sum no