X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fchart.c;h=178788255744468d36aa3b42cfb3916b9343f989;hb=e582516e80e5a04f10a651515a35b616911cf4d6;hp=abf4e70b7d2c4b76af55056453c21e3131eeb932;hpb=02ef5fef5288b80a4822e1006f6cb2b1369a55bd;p=pspp-builds.git diff --git a/src/chart.c b/src/chart.c index abf4e70b..17878825 100644 --- a/src/chart.c +++ b/src/chart.c @@ -18,156 +18,38 @@ 02111-1307, USA. */ -#include -#include -#include -#include -#include -#include #include +#include #include "chart.h" - -const char *data_colour[] = { - "brown", - "red", - "orange", - "yellow", - "green", - "blue", - "violet", - "grey", - "pink" -}; - - - -int -chart_initialise(struct chart *chart) +/* Adjust tick to be a sensible value + ie: ... 0.1,0.2,0.5, 1,2,5, 10,20,50 ... */ +double +chart_rounded_tick(double tick) { - chart->pl_params = pl_newplparams(); - - chart->lp = pl_newpl_r ("X",0,stdout,stderr,chart->pl_params); - - if (pl_openpl_r (chart->lp) < 0) /* open Plotter */ - return 1; - - pl_fspace_r (chart->lp, 0.0, 0.0, 1000.0, 1000.0); /* set coordinate system */ - pl_flinewidth_r (chart->lp, 0.25); /* set line thickness */ - pl_pencolorname_r (chart->lp, "black"); - - pl_erase_r (chart->lp); /* erase graphics display */ - pl_filltype_r(chart->lp,0); - - + int i; - pl_savestate_r(chart->lp); - - /* Set default chartetry */ - chart->data_top = 900; - chart->data_right = 800; - chart->data_bottom = 120; - chart->data_left = 150; - chart->abscissa_top = 70; - chart->ordinate_right = 120; - chart->title_bottom = 920; - chart->legend_left = 810; - chart->legend_right = 1000; - chart->font_size = 0; - strcpy(chart->fill_colour,"red"); - - - /* Get default font size */ - if ( !chart->font_size) - chart->font_size = pl_fontsize_r(chart->lp, -1); - - /* Draw the data area */ - pl_box_r(chart->lp, - chart->data_left, chart->data_bottom, - chart->data_right, chart->data_top); - - return 0; - -} - - - -/* Draw a tick mark at position - If label is non zero, then print it at the tick mark -*/ -void -draw_tick(struct chart *chart, - enum tick_orientation orientation, - double position, - const char *label, ...) -{ - const int tickSize = 10; - - pl_savestate_r(chart->lp); - - pl_move_r(chart->lp, chart->data_left, chart->data_bottom); - - if ( orientation == TICK_ABSCISSA ) - pl_flinerel_r(chart->lp, position, 0, position, -tickSize); - else if (orientation == TICK_ORDINATE ) - pl_flinerel_r(chart->lp, 0, position, -tickSize, position); - else - assert(0); - - if ( label ) { - char buf[10]; - va_list ap; - va_start(ap,label); - vsnprintf(buf,10,label,ap); - - if ( orientation == TICK_ABSCISSA ) - pl_alabel_r(chart->lp, 'c','t', buf); - else if (orientation == TICK_ORDINATE ) - { - if ( fabs(position) < DBL_EPSILON ) - pl_moverel_r(chart->lp, 0, 10); - - pl_alabel_r(chart->lp, 'r','c', buf); - } - - va_end(ap); - } + double diff = DBL_MAX; + double t = tick; - pl_restorestate_r(chart->lp); -} - - - + static const double standard_ticks[] = {1, 2, 5, 10}; -void -chart_write_title(struct chart *chart, const char *title) -{ - /* Write the title */ - pl_savestate_r(chart->lp); - pl_ffontsize_r(chart->lp,chart->font_size * 1.5); - pl_move_r(chart->lp,chart->data_left, chart->title_bottom); - pl_alabel_r(chart->lp,0,0,title); - pl_restorestate_r(chart->lp); -} - - - -void -chart_finalise(struct chart *chart) -{ - pl_restorestate_r(chart->lp); + const double factor = pow(10,ceil(log10(standard_ticks[0] / tick))) ; - if (pl_closepl_r (chart->lp) < 0) /* close Plotter */ + for (i = 3 ; i >= 0 ; --i) { - fprintf (stderr, "Couldn't close Plotter\n"); - } - - - pl_deletepl_r(chart->lp); + const double d = fabs( tick - standard_ticks[i] / factor ) ; - pl_deleteplparams(chart->pl_params); + if ( d < diff ) + { + diff = d; + t = standard_ticks[i] / factor ; + } + } + return t; + }