ILE RPG Google Chart encoders

Beyond sending data series in the clear, the Google Chart API offers two additional encoding schemes: simple and extended. Full information for these formats can be found at http://code.google.com/apis/chart/docs/data_formats.html.

The simple encoding is suitable for integers in the range of 0-61, while the extended encoder can be used for values from 0-4095.

SimpleEncoder() and ExtendedEncoder() procedures


      // -------------
      // SimpleEncoder
      // Encodes a value into Google chart "simple" encoding.

     d SimpleEncoder   pr             1a
     d  invalue                      15s 5 const
     d  inmaxvalue                   15s 5 const

     p SimpleEncoder   b
     d SimpleEncoder   pi             1a
     d  invalue                      15s 5 const
     d  inmaxvalue                   15s 5 const

     d SIMPLEENCODING  c                   'ABCDEFGHIJKLMNOPQRSTUVWXYZ-
     d                                     abcdefghijklmnopqrstuvwxyz-
     d                                     0123456789'
     d MISSING         c                   '_'

     d worksimpleval   s              5i 0 inz

      /free

         worksimpleval = %inth(%len(SIMPLEENCODING) *
                         invalue / inmaxvalue);
         if worksimpleval = 0;
             return MISSING;
         else;
             return %subst(SIMPLEENCODING:worksimpleval:1);
         endif;

      /end-free

     p SimpleEncoder   e



      // ---------------
      // ExtendedEncoder
      // Encodes a value into Google chart "extended" encoding.

     d ExtendedEncoder...
     d                 pr             2a
     d  invalue                      15s 5 const
     d  inmaxvalue                   15s 5 const options(*nopass : *omit)

     p ExtendedEncoder...
     p                 b
     d ExtendedEncoder...
     d                 pi             2a
     d  invalue                      15s 5 const
     d  inmaxvalue                   15s 5 const options(*nopass : *omit)

     d EXTENDEDENCODING...
     d                 c                   'ABCDEFGHIJKLMNOPQRSTUVWXYZ-
     d                                     abcdefghijklmnopqrstuvwxyz-
     d                                     0123456789-.'
     d MISSING         c                   '__'
     d EXTENDEDMAXVAL...
     d                 c                   4095

     d scalingfactor   s              9s 8 inz
     d scaledvalue     s             10i 0 inz
     d col             s              5i 0 inz
     d row             s              5i 0 inz
     d firstchar       s              1a   inz
     d secondchar      s              1a   inz

      /free

         // Determine scaling factor for this encode.
         if %parms() < 2;
            if invalue > EXTENDEDMAXVAL;
               scaledvalue = EXTENDEDMAXVAL;
            else;
               scaledvalue = invalue;
            endif;
         else;
            scaledvalue = invalue / inmaxvalue * EXTENDEDMAXVAL;
         endif;

         col = %int(scaledvalue / %len(EXTENDEDENCODING));
         row = %rem(scaledvalue : %len(EXTENDEDENCODING));

         if col >= 0 and row >= 0;
             firstchar = %subst(EXTENDEDENCODING:col+1:1);
             secondchar = %subst(EXTENDEDENCODING:row+1:1);
             return firstchar+secondchar;
         else;
             return MISSING;
         endif;

      /end-free

     p ExtendedEncoder...
     p                 e