#include #include #include #include #include #include using namespace std; int width=500; int height=400; string y_label=""; double y_range_bottom=0.0; double y_range_step=0.1; double y_range_top=1.0; int bar_gap=5; int plot_margin_top = 30; int plot_margin_right = 30; int plot_margin_left = 70; int plot_margin_bottom = 50; string font_family = "Verdana"; int font_size = 10; int label_rotation = 0; char *colors[] = { "purple","orange","green", "red","blue","magenta","#007F7F","black" }; int color_count = 8; vector< pair > data; void create_graph( string filename ) { FILE* f = fopen( filename.c_str(), "w" ); if ( !f ) { fprintf( stderr, "unable to open %s for writing\n", filename.c_str() ); exit(-1); } int bar_width = (int)(((width-(plot_margin_left+plot_margin_right))-bar_gap)/ (data.size()))-bar_gap; // write the header fprintf( f, "\n" ); fprintf( f, "\n" ); fprintf( f, "\n" ); // draw the background fprintf( f, " \n" ); // draw the left hash-marks for ( double current=y_range_bottom; current<=y_range_top; current+=y_range_step ) { int h = (int)(((height-(plot_margin_top+plot_margin_bottom))*current)/ (y_range_top-y_range_bottom)); fprintf( f, " \n" ); fprintf( f, "%lg\n", current ); fprintf( f, " \n" ); fprintf( f, " \n", plot_margin_left-2, (height-plot_margin_bottom)-h, width-plot_margin_right, (height-plot_margin_bottom)-h ); } // draw the bars int current_color = 0; for ( int i=0; i\n", data[i].first.c_str() ); int x = i*(bar_width+bar_gap)+bar_gap+plot_margin_left; int y = plot_margin_bottom; int w = bar_width; int h = (int)(((height-(plot_margin_top+plot_margin_bottom))*data[i].second)/ (y_range_top-y_range_bottom)); y = (height-y)-h; fprintf( f, " \n", color ); if ( label_rotation == 0 ) { fprintf( f, " \n" ); } else { fprintf( f, " \n", 360-label_rotation ); } fprintf( f, "%s\n", data[i].first.c_str() ); fprintf( f, " \n" ); } // draw the border around the plot area fprintf( f, " \n" ); // draw the y-label fprintf( f, " \n" ); fprintf( f, "%s\n", y_label.c_str() ); fprintf( f, " \n" ); // write the footer fprintf( f, "\n" ); fclose(f); } void usage() { fprintf(stderr,"usage: foobar [options] \n"); fprintf(stderr,"options:\n"); fprintf(stderr," --rotate-labels\n"); fprintf(stderr," --width \n"); fprintf(stderr," --height \n"); exit(-1); } int main(int argc, char** argv) { string filename = ""; string output = ""; for ( int i=1; i tab_delim ? space_delim : tab_delim); if ( svalue == NULL ) { fprintf(stderr, "bad format on line %d: %s\n", line_number, label); delete label; return -1; } *(svalue++) = 0; double value = atof(svalue); data.push_back( pair( label, value ) ); delete[] label; line = ""; getline(in,line); } create_graph(output); return 0; }