Search code examples
cplotascii

Is there a recommended way to plot data using ascii symbols?


I am writing a bare-metal C language test program that captures some sensor data, analyzes and plots it. The data is pretty simple and I only have a serial console interface. Still, I would like to plot the waveform using only mono-spaced ascii characters.

Is there a standard, recommended way to do something like this?

256  |           *
     |          *
     |            *
     |         *   *
128  |        *     *
     |       *       *       *  
     |   *                 ** *
     | ** ***         * * *    **
  0  _______________________________    

Solution

  • It's simply a pair of nested loops.

    size_t n = 26;
    
    for ( size_t y = 8; y--; ) {
       int miny = ( y + 0 ) * 8;
       int maxy = ( y + 1 ) * 8;
    
       for ( size_t x = 0; x < n; ++x ) {
          putchar( miny <= samples[ x ] && samples[ x ] < maxy ? '*' : ' ' );
       }
    
       putchar( '\n' );
    }