|
|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T d
Length: 6597 (0x19c5)
Types: TextFile
Names: »dataplot.c«
└─⟦87ddcff64⟧ Bits:30001253 CPHDIST85 Tape, 1985 Autumn Conference Copenhagen
└─⟦this⟧ »cph85dist/stat/src/dataplot.c«
/*LINTLIBRARY*/
#include "unixstat.h"
PGM(dataplot,scatter plot,5.1,3/11/85)
/* Copyright (c) 1984 Gary Perlman (see Copyright file) */
#define MAX_WIDTH 100 /* plot at most this wide */
#define MAX_HEIGHT 100 /* plot at most this high */
#define MIN_PLOT 5 /* plot at least 5 x 5 */
#define EPSILON (.000000001)
#define scale(x,min,max,width) ((int) (width*((x)-(min))/((max)-(min)+EPSILON)))
typedef struct
{
char *name; /* name of the dataset */
float *data; /* where the data are read in */
int count; /* actual count of how many data there are */
int maxcount; /* maximum allowed number of data */
double plotmin; /* minimum plotting value of data */
int setmin; /* has the min been set? if not use data */
double plotmax; /* maximum plotting data value */
int setmax; /* has the max been set? if not use data */
int plotsize; /* size of plotting dimension */
int border; /* character plotted on the border */
} DATASET;
dataplot (x, y)
DATASET *x, *y;
{
int elt, col, row; /* loop counters */
int plot[MAX_HEIGHT][MAX_WIDTH]; /* xy plot constructed here */
int bad; /* have we got a bad point? */
double X, Y; /* xy data points */
if (x->count != y->count)
{
fprintf (stderr, "dataplot: dataset dimensions do not match\n");
return;
}
if (x->plotsize > MAX_WIDTH) x->plotsize = MAX_WIDTH;
else if (x->plotsize < MIN_PLOT) x->plotsize = MIN_PLOT;
if (y->plotsize > MAX_HEIGHT) y->plotsize = MAX_HEIGHT;
else if (y->plotsize < MIN_PLOT) y->plotsize = MIN_PLOT;
if (!x->setmin || !y->setmin || !x->setmax || y->setmax)
{
if (!x->setmin) x->plotmin = x->data[0];
if (!x->setmax) x->plotmax = x->data[0];
if (!y->setmin) y->plotmin = y->data[0];
if (!y->setmax) y->plotmax = y->data[0];
for (elt = 0; elt < x->count; elt++)
{
X = x->data[elt];
Y = y->data[elt];
if (!x->setmin && X < x->plotmin) x->plotmin = X;
if (!x->setmax && X > x->plotmax) x->plotmax = X;
if (!y->setmin && Y < y->plotmin) y->plotmin = Y;
if (!y->setmax && Y > y->plotmax) y->plotmax = Y;
}
}
for (row = 0; row < y->plotsize; row++)
for (col = 0; col < x->plotsize; col++)
plot[row][col] = 0;
for (elt = 0; elt < x->count; elt++)
{
bad = 0;
X = x->data[elt];
Y = y->data[elt];
if (Y < y->plotmin || Y > y->plotmax) bad++;
else row = scale (Y, y->plotmin, y->plotmax, y->plotsize);
if (X < x->plotmin || X > x->plotmax) bad++;
else col = scale (X, x->plotmin, x->plotmax, x->plotsize);
if (bad)
fprintf (stderr, "dataplot: (%g,%g) out of plotting bounds\n", X,Y);
else plot[row][col]++;
}
if (x->border)
{
if (y->border) putchar (y->border);
for (col = 0; col < x->plotsize; col++) putchar (x->border);
if (y->border) putchar (y->border);
printf (" %g\n", y->plotmax);
}
for (row = y->plotsize-1; row >= 0; row--)
{
if (y->border) putchar (y->border);
for (col = 0; col < x->plotsize; col++)
if (plot[row][col])
if (plot[row][col] > 9) putchar ('*');
else putchar (plot[row][col]+'0');
else putchar (' ');
if (y->border) putchar (y->border);
if (y->name && *(y->name))
if (row == y->plotsize/2)
printf (" %s", y->name);
putchar ('\n');
}
if (x->border)
{
char dataline[MAX_WIDTH];
int width = x->plotsize + (y->border ? 2 : 0);
int len1, len2;
int i;
if (y->border) putchar (y->border);
for (col = 0; col < x->plotsize; col++) putchar (x->border);
if (y->border) putchar (y->border);
printf (" %g\n", y->plotmin);
VOID sprintf (dataline, "%-.3f", x->plotmin);
len1 = strlen (dataline);
printf ("%s", dataline);
VOID sprintf (dataline, "%.3f", x->plotmax);
len2 = strlen (dataline);
for (i = len1+len2; i < width; i++)
putchar (' ');
puts (dataline);
}
if (x->name && *(x->name))
{
elt = (x->plotsize - strlen (x->name)) / 2;
while (--elt > 0) putchar (' ');
puts (x->name);
}
}
#ifdef STANDALONE
#include "unixstat.h"
DATASET x, y;
#define MAXDATA 1000 /* at most this many points */
float xdata[MAXDATA], ydata[MAXDATA];
main (argc, argv) char **argv;
{
x.maxcount = y.maxcount = MAXDATA;
x.name = "Column 1 Data";
y.name = "Column 2 Data";
x.plotsize = 60;
y.plotsize = 20;
x.data = xdata;
y.data = ydata;
x.border = '-';
y.border = '|';
ARGV0;
initial (argc, argv);
checkstdin (Argv0);
readdata ();
dataplot (&x, &y);
exit (0);
}
#define OPTARG (optarg+1)
initial (argc, argv) char **argv;
{
int C;
extern char *optarg;
extern int optind;
int opterr = 0;
while ((C = getopt (argc, argv, "b:m:M:n:s:")) != EOF)
switch (C)
{
case 'b': /* border */
if (*optarg == 'x')
x.border = *OPTARG;
else y.border = *OPTARG;
break;
case 'm': /* minimum for plot */
if (*optarg == 'x')
{
if (!number (OPTARG)) ERRNUM (OPTARG,x-axis plot minimum)
x.plotmin = atof (OPTARG);
x.setmin = 1;
}
else
{
if (!number (OPTARG)) ERRNUM (OPTARG,y-axis plot minimum)
y.plotmin = atof (OPTARG);
y.setmin = 1;
}
break;
case 'M': /* maximum for plot */
if (*optarg == 'x')
{
if (!number (OPTARG)) ERRNUM (OPTARG,x-axis plot maximum)
x.plotmax = atof (OPTARG);
x.setmax = 1;
}
else
{
if (!number (OPTARG)) ERRNUM (OPTARG,y-axis plot maximum)
y.plotmax = atof (OPTARG);
y.setmax = 1;
}
break;
case 'n': /* name for axis */
if (*optarg == 'x') x.name = OPTARG;
else y.name = OPTARG;
break;
case 's': /* size for plot */
if (*optarg == 'x')
{
if (!INTEGER (OPTARG)) ERRNUM (OPTARG,width of plot)
x.plotsize = atoi (OPTARG);
if (x.plotsize < MIN_PLOT || x.plotsize > MAX_WIDTH)
ERRVAL (d,x.plotsize,width of plot)
}
else
{
if (!INTEGER (OPTARG)) ERRNUM (OPTARG,height of plot)
y.plotsize = atoi (OPTARG);
if (y.plotsize < MIN_PLOT || y.plotsize > MAX_WIDTH)
ERRVAL (d,y.plotsize,height of plot)
}
break;
default:
opterr++;
break;
}
if (opterr)
USAGE (-b [xy]char -m [xy]min -M [xy]max -s [xy]size -n [xy]name)
ERROPT (optind)
}
readdata ()
{
char line[BUFSIZ];
char *array[2];
int count = 0;
while (gets (line))
if (parseline (line, array, 2) == 2)
{
if (!number (array[0])) ERRNUM (array[0],x-axis value)
if (!number (array[1])) ERRNUM (array[1],y-axis value)
x.data[count] = atof (array[0]);
y.data[count] = atof (array[1]);
count++;
}
else ERRRAGGED
x.count = y.count = count;
}
#endif