How to embed a dynamic graph in a WEB page
Source code of program EMBEDGRAPH
 *=============================================================================================
 * This program demonstrates how you can insert a graph in your HTML WEB page,                 
 * provided that your program uses CGIDEV2 procedures to create the WEB page.                  
 *                                                                                             
 * 1- Start creating your WEB page as usual                                                    
 *    (in this example we start by listing the source code of this program and its external    
 *     html)                                                                                   
 * 2- When you reach the point where the graph should appear                                   
 *    a- Create the graph as an external IFS stream file                                       
 *       (call program GRAPHIT/CHARTIT, see local procedure CrtGraphStmf() in this example)    
 *    b- Load in memory this graph stream file an add the memory to the output buffer          
 *       (see local procedure EmbedStmf() in this example)                                     
 * 3- Complete your output HTML and send the output buffer.                                    
 *                                                                                             
 * CREATE THE PROGRAM with ACTGRP(*CALLER) !!!                                                 
 *                                                                                             
 *=============================================================================================
 /copy CGIDEV2/qrpglesrc,hspecs                                                                
 /copy CGIDEV2/qrpglesrc,hspecsbnd                                                             
FPOPULATIONif   e           k disk    extfile('GRAPHIT/POPULATION')                            
FQRPGLESRC if   e             disk    extfile('GRAPHIT/QRPGLESRC')                             
F                                     extmbr('EMBEDGRAPH')                                     
F                                     rename(qrpglesrc:rpgrcd)                                 
FQTXTSRC   if   e             disk    extfile('QTEMP/QTXTSRC')                                 
F                                     extmbr('EMBEDGRAPH')                                     
F                                     rename(qtxtsrc:txtrcd)                                   
F                                     prefix('TXT')                                            
F                                     usropn                                                   
 /copy CGIDEV2/qrpglesrc,prototypeb                                                            
 /copy CGIDEV2/qrpglesrc,usec                                                                  
 /copy CGIDEV2/qrpglesrc,variables3                                                            
                                                                                               
D extHtml         s           2000    inz('/graphit/html/embedgraph.txt')                      
 * Indicators for GetHtmlIfsMult subprocedure                                                  
D IfsMultIndicators...                                                                         
D                 ds                                                                           
D  NoErrors                       n                                                            
D  NameTooLong                    n                                                            
D  NotAccessible                  n                                                            
D  NoFilesUsable                  n                                                            
D  DupSections                    n                                                            
D  FileIsEmpty                    n                                                            
                                                                                               
D                 ds                                                                           
D srcdta                       100                                                             
D srcdta1                        1    overlay(srcdta:1)                                        
D srcdta2                        1    overlay(srcdta:2)                                        
D srcdta6                       95    overlay(srcdta:6)                                        
                                                                                               
D xlabels         s          10000                                                             
D xdata           s           5000                                                             
D stmf            c                   '/graphit/work/populations.html'                         
                                                                                               
 * Local procedures                                                                            
D FillData        pr                                                                           
D DspRpgSrc       pr                                                                           
D DspExtHtml      pr                                                                           
D ReplAcute       pr                                                                           
D  char                          1    const                                                    
D CrtGraphStmf    pr                                                                           
D GoChartit       pr                  extpgm('GRAPHIT/CHARTIT')                                
D EmbedStmf       pr                                                                           
                                                                                               
 *=====================================================================                        
 * Main line                                                                                   
 *=====================================================================                        
 /free                                                                                         
                                                                                               
      // Read database file "population", fill in variables "xlabels" and "xdata"              
      FillData();                                                                              
      // Load the external HTML script                                                         
      IfsMultIndicators = getHtmlIfsMult(%trim(exthtml):'');                            
                                                                                               
      wrtsection('top');          // start the output HTML                                     
      wrtsection('part1');        // start HTML leg number 1                                   
      dspRpgSrc();                // display RPG source                                        
      wrtsection('part2');        // start HTML leg number 2                                   
      dspExtHtml();               // display external HTML                                     
      wrtsection('part3');        // start HTML leg number 3                                   
                                                                                               
      crtGraphStmf();             // create the HTML stmf of the graph                         
      embedStmf();                // embed the graph stmf                                      
                                                                                               
      wrtsection('part3end');     // end HTML                                                  
      wrtsection('*fini');        // send the output buffer to the client browser              
                                                                                               
      *inlr=*on;                                                                               
      return;                                                                                  
                                                                                               
 /end-free                                                                                     
 *=====================================================================                        
 * Read database file "population", fill in variables                                          
 *   - "xlabels" (example: China,India,U.S.A, ... ) and                                        
 *   - "xdata"   (example: 1357,1252,320, ... )                                                
 * that will be later used in procedure GoChartit()                                            
 *=====================================================================                        
P FillData        b                                                                            
D FillData        pr                                                                           
 /free                                                                                         
                                                                                               
      clear xdata;                                                                             
      clear xlabels;                                                                           
      read poprcd;                                                                             
      dow not %eof;                                                                            
          xlabels = %trim(xlabels) + %trim(popcountry) + ',';   //country names                
          xdata = %trim(xdata) + %trim(%char(poppeople)) + ','; //country populations          
          read poprcd;                                                                         
      enddo;                                                                                   
      xdata = %subst(xdata:1:%len(%trim(xdata))-1);       //trim off the last comma            
      xdata = '[' + %trim(xdata) + ']';                                                        
      xlabels = %subst(xlabels:1:%len(%trim(xlabels))-1); //trim off the last comma            
                                                                                               
 /end-free                                                                                     
P FillData        e                                                                            
 *========================================================================                     
 * Display this RPG source                                                                     
 *========================================================================                     
P DspRpgSrc       b                                                                            
D DspRpgSrc       pi                                                                           
 /free                                                                                         
                                                                                               
      read rpgrcd;                                                                             
      dow not %eof;                                                                            
          updhtmlvar('srcdta':srcdta6:'1':'%trim0');                                           
          wrtsection('RPGrow');                                                                
          read rpgrcd;                                                                         
      enddo;                                                                                   
      wrtsection('RPGrowend');                                                                 
                                                                                               
 /end-free                                                                                     
P DspRpgSrc       e                                                                            
 *========================================================================                     
 * Display the external HTML script                                                            
 *========================================================================                     
P DspExtHtml      b                                                                            
D DspExtHtml      pi                                                                           
D rc              s             10i 0                                                          
D cmd             s            200                                                             
 /free                                                                                         
                                                                                               
      rc=docmd('chkobj qtemp/qtxtsrc *file');                                                  
      if rc<>0;                                                                                
         rc=docmd('crtdupobj qtxtsrc graphit *file qtemp');                                    
      endif;                                                                                   
                                                                                               
      rc=docmd('chkobj qtemp/qtxtsrc *file embedgraph');                                       
      if rc<>0;                                                                                
         rc=docmd('addpfm qtemp/qtxtsrc embedgraph');                                          
      endif;                                                                                   
                                                                                               
      rc=docmd('CPYFRMSTMF FROMSTMF(''' + %trim(exthtml) + ''') +                              
           TOMBR(''/qsys.lib/qtemp.lib/qtxtsrc.file/embedgraph.mbr'') +                        
           MBROPT(*REPLACE)');                                                                 
      if rc<>0;                                                                                
         wrtsection('*fini');                                                                  
         *inlr=*on;                                                                            
         return;                                                                               
      endif;                                                                                   
                                                                                               
      open QTXTSRC;                                                                            
                                                                                               
      read txtrcd;                                                                             
      dow not %eof;                                                                            
          replAcute('<');  //replace acute bracket <                                           
          replAcute('>');  //replace acute bracket >                                           
          updhtmlvar('srcdta':txtsrcdta);                                                      
          wrtsection('HTMLrow');                                                               
          read txtrcd;                                                                         
      enddo;                                                                                   
      wrtsection('HTMLrowEnd');                                                                
                                                                                               
      close QTXTSRC;                                                                           
                                                                                               
 /end-free                                                                                     
P DspExtHtml      e                                                                            
 *========================================================================                     
 * Replace acute brackets ( < and > ) in "txtsrcdta"                                           
 *========================================================================                     
P ReplAcute       b                                                                            
D ReplAcute       pi                                                                           
D  char                          1    const                                                    
D  repl           s              4                                                             
D r               s             10i 0                                                          
 /free                                                                                         
                                                                                               
      select;                                                                                  
      when char='<';                                                                           
           repl='<';                                                                        
      when char='>';                                                                           
           repl='>';                                                                        
      other;                                                                                   
           wrtsection('*fini');                                                                
           *inlr=*on;                                                                          
           return;                                                                             
      endsl;                                                                                   
                                                                                               
      // Replace all characters '<' or '>'                                                     
      r=1;                                                                                     
      dou r=0;                                                                                 
          r=%scan(char:txtsrcdta);                                                             
          select;                                                                              
          when r=1;                                                                            
             txtsrcdta=repl+%subst(txtsrcdta:r+1);                                             
          when r>1;                                                                            
             txtsrcdta=%subst(txtsrcdta:1:r-1) +                                               
                       repl+%subst(txtsrcdta:r+1);                                             
          endsl;                                                                               
      enddo;                                                                                   
                                                                                               
 /end-free                                                                                     
P ReplAcute       e                                                                            
 *==========================================                        
 * Create the HTML stream file of the graph                                                    
 *=====================================================================                        
P CrtGraphStmf    b                                                                            
D CrtGraphStmf    pr                                                                           
 /free                                                                                         
                                                                                               
      putenv('CHARTIT_XTYPE='+'pie':qusec);                                                    
      putenv('CHARTIT_XTITLE=World population distribution': qusec);                           
      putenv('CHARTIT_XWIDTH=300': qusec);                                                     
      putenv('CHARTIT_XHEIGHT=300': qusec);                                                    
      putenv('CHARTIT_XLABELS=' + %trim(xlabels): qusec);                                      
      putenv('CHARTIT_XDATA=' + %trim(xdata): qusec);                                          
      putenv('CHARTIT_XLISTDATA=yes': qusec);                                                  
      putenv('CHARTIT_XLEGENDA=[Country populations (millions)]':qusec);                       
      putenv('CHARTIT_XTOSTMF=' + stmf: qusec);                                                
                                                                                               
      GoChartit();     //call pgm GRAPHIT/CHARTIT                                              
                                                                                               
 /end-free                                                                                     
P CrtGraphStmf    e                                                                            
 *========================================================================                     
 * Embed the stream file into the html for the browser                                         
 *========================================================================                     
P Embedstmf       b                                                                            
D Embedstmf       pi                                                                           
D dataPointer     s               *                                                            
D dataLength      s             10i 0                                                          
D rc              s             10i 0                                                          
 /free                                                                                         
                                                                                               
      // Load the stream file into memory                                                      
      rc=LoadStreamFile(Stmf:'TEXT':DataLength:DataPointer);                                   
      if rc=0;                                                                                 
         // Write to the output buffer the memory contain the graph stream file                
         wrtNoSection(DataPointer:DataLength);                                                 
         // Deallocate memory                                                                  
         dealloc(n) DataPointer;                                                               
      endif;                                                                                   
                                                                                               
 /end-free                                                                                     
P Embedstmf       e                                                                            
                
External HTML script
<as400>top
Content-type: text/html
<html>
<head><title>How to embed a dynamic graph in a WEB page</title>
<link rel="SHORTCUT ICON" href="http://www.easy400.net/easy400/favicon.ico">
<link href="/easy400/css/css.css" rel="stylesheet" type="text/css">
<style TYPE="text/css">
.title {color: Blue; font-weight: Bold; font-size: 18pt;
font-family: souvenir lt bt, verdana, serif; text-align: center}
td.leg {padding-left: 5px; border: #000000 1px solid;}
td.ehtml {background-color: #77AA99;}
td.summary {font-family: "Arial Narrow"; font-size: 16px; color: #000099;
font-style: italic; text-align: center; border-bottom: #000000 1px solid;}
th,td {font-size: 12px; font-family: Helvetica, Helv, Arial;}
</style>
</head>
<body>
<table border="0">
<tr><td colspan="3">
<table cellspacing="0" cellpadding="0" width="100%">
<tr><td class="title" align="center">
How to embed a dynamic graph in a WEB page</td></tr>
</table>
</td></tr>
<tr>
<as400>part1
<!-- **** 1ST LEG **** -->
<td class="leg" valign="top">
<table cellspacing="0" cellpadding="0">
<tr><td class="summary">Source code of program EMBEDGRAPH</td></tr>
<tr><td><pre style="font-size: 10px; background-color: #B9AACC">
<as400>RPGrow
/%srcdta%/
<as400>RPGrowEnd
</td></tr>
</table>
</td>
<as400>part2
<!-- **** 2ND LEG **** -->
<td class="leg" valign="top">
<table cellspacing="0" cellpadding="0">
<tr><td class="summary">External HTML script</td></tr>
<tr>
<as400>HTMLrow
<td class="ehtml">/%srcdta%/</td></tr>
<as400>HTMLrowEnd
</table>
</td>
<as400>part3
<!-- **** 3RD LEG **** -->
<td class="leg" valign="top">
<table cellspacing="0" cellpadding="0" width="100%">
<tr><td class="summary">Embedded graph</td></tr>
<tr><td> </td></tr>
<!-- The Graph is embedded just after this comment -->
<as400>part3end
<!-- End of the embedded Graph ---------------------->
</table>
</td>
</tr>
</table>
</body>
</html>
World population distribution
Embedded graph
 
World population distribution
Country populations (millions)
Other 173 countries 1711
China 1357
India 1252
United States 320
Indonesia 250
Brazil 200
Pakistan 191
Nigeria 174
Bangladesh 157
Russia 144
Japan 127
Mexico 122
Philippines 99
Ethiopia 94
Vietnam 90
Egypt 88
Germany 81
Iran 80
Turkey 77
Thailand 70
Congo 68
France 66
United Kingdom 65
Italy 61
South Africa 51
Canada 34
Australia 24