/*############################# draw_stat() #############################*/ void draw_stat(void) { draw_x_axis(); draw_x_values(); draw_y_axis(); draw_graph(); return; }
// Create and output the bar chart from the list void bar_chart(Bar *pFirst, uint page_width, uint page_height, char *title) { Bar *pLast = pFirst; // Pointer to previous Bar double max = pFirst->value; // Maximum Bar value - 1st to start double min = pFirst->value; // Minimum Bar value - 1st to start double vert_step = 0.0; // Unit step in vertical direction uint bar_count = 1; // Number of bars - at least 1 uint bar_width = 0; // Width of a Bar uint space = 2; // Spaces between bars char *column = NULL; // Pointer to Bar column section char *blank = NULL; // Blank string for Bar+space double position = 0.0; // Current vertical position on chart bool axis = false; // Indicates axis drawn // Find maximum and minimum of all Bar values while(pLast = pLast->pNext) { ++bar_count; // Increment Bar count max = (max < pLast->value) ? pLast->value : max; min = (min > pLast->value) ? pLast->value : min; } // Always draw chart to horizontal axis if(max < 0.0) max = 0.0; if(min > 0.0) min = 0.0; vert_step = (max - min)/page_height; // Calculate step length // Calculate and check Bar width if((bar_width = page_width/bar_count - space) < 1) { printf_s("\nPage width too narrow.\n"); exit(1); } // Set up a string that will be used to build the columns if(!(column = chart_string(space, bar_width, '#'))) { printf_s("\nFailed to allocate memory in barchart()" " - terminating program.\n"); exit(1); } // Set up a string that will be a blank column if(!(blank = chart_string(space, bar_width, ' '))) { printf_s("\nFailed to allocate memory in barchart()" " - terminating program.\n"); exit(1); } // Draw the Bar chart. It is drawn line by line starting at the top printf_s("\n^ %s\n", title); // Output the chart title position = max; // Start at the top for(uint i = 0 ; i < page_height ; ++i) // page_height lines for chart { // Check if we need to output the horizontal axis if(position <= 0.0 && !axis) { draw_x_axis(bar_count*(bar_width + space)); axis = true; } printf_s("|"); // Output vertical axis pLast = pFirst; // start with the first Bar // For each Bar... for(uint bars = 1 ; bars <= bar_count ; ++bars) { // If position is between axis and value, output column. otherwise blank printf_s("%s", position <= pLast->value && position > 0.0 || position >= pLast->value && position <= 0.0 ? column : blank); pLast = pLast->pNext; } printf_s("\n"); // End the line of output position -= vert_step; // Decrement position } if(!axis) // Horizontal axis? draw_x_axis(bar_count*(bar_width + space)); // No, so draw it else printf_s("v\n"); // -y axis arrow head free(blank); // Free blank string memory free(column); // Free column string memory }