home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / qtawk / graph.exp < prev    next >
Text File  |  1990-04-23  |  3KB  |  132 lines

  1. # greph - processor for a graph-drawing language
  2. #  input: data and specification of a graph
  3. #  output: data plotted in specified area
  4. # AWK page 137
  5.  
  6. BEGIN { #set frame dimensions ...
  7.     ht = 23; wid = 79;    # height and width
  8.     ox = 6; oy = 2;    # offset for x and y axes
  9.     number = /^{_f}({_e})?$/;  # use built-in sub-expression for real numbers
  10. }
  11. $1 == "label" {     # for bottom
  12.     sub(/^ *label */,"");
  13.     botlab = $0;
  14.     next;
  15. }
  16.  
  17. $1 == "bottom" && $2 == "ticks" {   # ticks for x-axis
  18.     for ( i = 3 ; i <= NF ; i++ ) bticks[++nb] = $i;
  19.     next;
  20. }
  21.  
  22. $1 == "left" && $2 == "ticks" { # ticks for y-axis
  23.     for ( i = 3 ; i <= NF ; i++ ) lticks[++nl] = $i;
  24.     next;
  25. }
  26.  
  27. $1 == "range" { # xmin ymin xmax ymax
  28.     xmin = $2; ymin = $3; xmax = $4; ymax = $5;
  29.     next;
  30. }
  31.  
  32. $1 == "height" { ht = $2; next; }
  33. $1 == "width"  { wid = $2; next; }
  34.  
  35. $1 ~~ number && $2 ~~ number {    # pair of numbers
  36.     nd++;   # count number of data points
  37.     x[nd] = $1; y[nd] = $2;
  38.     ch[nd] = $3;    # optional plotting character
  39.     next;
  40. }
  41.  
  42. $1 ~~ number && $2 !~ number {    # single number
  43.     nd++;   # count number of data points
  44.     x[nd] = nd; y[nd] = $1; ch[nd] = $2;
  45.     next;
  46. }
  47.  
  48. END {    # draw graph
  49.     if ( xmin == "" ) {     # no range was given
  50.     xmin = xmax = x[1];
  51.     ymin = ymax = y[1];
  52.     for ( i = 2 ; i <= nd ; i++ ) {
  53.         if ( x[i] < xmin ) xmin = x[i];
  54.         if ( x[i] > xmax ) xmax = x[i];
  55.         if ( y[i] < ymin ) ymin = y[i];
  56.         if ( y[i] > ymax ) ymax = y[i];
  57.     }
  58.     }
  59.     frame; ticks; label; data; draw;
  60. }
  61.  
  62. function frame() {  # create frame for graph
  63.     local i, j;
  64.  
  65.     for ( i = ox+1 ; i < wid ; i++ ) plot(i,oy,"─");      # bottom
  66.     for ( i = ox+1 ; i < wid ; i++ ) plot(i,ht-1,"─");    # top
  67.     for ( i = oy+1 ; i < ht  ; i++ ) plot(ox,i,"│");      # left
  68.     for ( i = oy+1 ; i < ht  ; i++ ) plot(wid-1,i,"│");   # right
  69.     plot(ox,ht-1,"┌");
  70.     plot(wid-1,ht-1,"┐");
  71.     plot(ox,oy,"└");
  72.     plot(wid-1,oy,"┘");
  73. }
  74.  
  75. function ticks() {  # create tick marks for both axes
  76.     local i;
  77.     local xys;
  78.  
  79.     for ( i = 1 ; i <= nb ; i++ ) {
  80.     xys = xscale(bticks[i]);
  81.     plot(xys,oy,"┼");
  82.        splot(xys - 1,1,bticks[i]);
  83.     }
  84.     for ( i = 1 ; i <= nl ; i++ ) {
  85.     xys = yscale(lticks[i]);
  86.     plot(ox,xys,"┼");
  87.     splot(0,xys,lticks[i]);
  88.     }
  89. }
  90.  
  91. function label() {  # center label under x-axis
  92.     splot((wid + ox - length(botlab))/2,0,botlab);
  93. }
  94.  
  95. function data() {   # create data points
  96.     local i;
  97.  
  98.     for ( i = 1 ; i <= nd ; i++ )
  99.       plot(xscale(x[i]),yscale(y[i]), ch[i] ? ch[i] : "*");
  100. #      plot(xscale(x[i]),yscale(y[i]),"*");
  101. }
  102.  
  103. function draw() {   # print graph from array
  104.     local i, j;
  105.  
  106.     for ( i = ht - 1 ; i >= 0 ; i-- ) {
  107.     for ( j = 0 ; j < wid ; j++ )
  108.       printf( j in array && i in array[j] ? array[j][i] : " " );
  109.     printf("\n");
  110.     }
  111. }
  112.  
  113. function xscale(x) {    # scale x-value
  114.     return int((x - xmin + 0.0)/(xmax - xmin) * (wid - 1.0 - ox) + ox + 0.5);
  115. }
  116.  
  117. function yscale(y) {    # scale y-value
  118.     return int((y - ymin + 0.0)/(ymax - ymin) * (ht  - 1.0 - oy) + oy + 0.5);
  119. }
  120.  
  121. function plot(x,y,c) {    # put character c in array
  122.     array[x][y] = c;
  123. }
  124.  
  125. function splot(x,y,s) { # put string s in array
  126.     local i, n = length(s);
  127.     local str = s "";
  128.  
  129.     for ( i = 0 ; i < n ; i++ )
  130.       array[x+i][y] = substr(str,i+1,1);
  131. }
  132.