MC Press Online

Saturday, May 27th

Last updateThu, 25 May 2017 10am

You are here: Home ARTICLES Programming Programming - Other General TechTip: Column, Line, and Area Charts: Generate Professional-Grade Charts in Your Programs, Part 6

Programming / General

TechTip: Column, Line, and Area Charts: Generate Professional-Grade Charts in Your Programs, Part 6

Support MC Press - Visit Our Sponsors


Flexible Input, Dazzling Output with IBM i



Click for this Month's

Bookstore Special Deals

These charts are similar in terms of internal structure, but they produce very different results. See examples of each type, and get tips on the best uses for each.


When I started my research for this TechTip, I realized that the Column API had a very similar structure to the Line API. Curious, I reviewed the Area API, only to find out that its structure was the same as the other two. In the end, what started as a TechTip about only the column API became a TechTip about the column, line, and area APIs.


This three-in-one required some tweaking on the original column template, as well as on the procedure. Let's start with the template:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">

<html xmlns="">


   <meta http-equiv="content-type" content="text/html; charset=utf-8"/>




   <script type="text/javascript" src=""></script>

   <script type="text/javascript">

     google.load("visualization", "1", {packages:["corechart"]});


     function drawChart() {

       var data = new google.visualization.DataTable();


       data.addColumn('string', 'col1');


       data.addColumn('/%ColumnType%/', '/%ColumnTitle%/');


       data.setValue(/%RowNbr%/, 0, '/%RowTitle%/');


       data.setValue(/%RowNbr%/, /%ColNbr%/, /%CellValue%/);



     var options = {


     '/%OptionTitle%/': /%OptionValue%/





       var chart = new google.visualization./%ChartType%/Chart(document.getElementById('chart_div'));

       chart.draw(data, options);






   <div id="chart_div"></div>




The template has the standard Header, Row, and Footer sections, along with the Options sections that I started to include a few Tips back. However, it also includes Column and FirstRow sections that I used on the bar chart TechTip. These last sections are required because the APIs can handle multiple values per row. These values will be grouped in different ways on the three types of chart, but they will be displayed together. (This may sound a bit confusing, but it will be clearer when you see the examples.)


Each value within the row will be a cell, just like on a spreadsheet. The first cell of each row will contain the title of that row. This will be handled by the FirstRow section. After that, multiple Row sections will be written, up to the maximum number of columns defined. This two-dimensional matrix required some special coding on the GenCLAChart procedure (which can be found in the QRPGLESRC/GENCHRT source member of the downloadable code). I had to use a cycle within a cycle to loop through the rows and, within each row, through each cell.


Here's how I did it:


// FirstRow And Row Sections                                  

For W_RowIdx = 1 to C_CLAMaxRows;                              

If P_CLARowDS(W_RowIdx).RowTitle = *Blanks;                  



// Update the Row variables                                  

updHtmlVar('RowNbr'   : %Char(W_RowIdx-1));                  

updHtmlVar('RowTitle' : %Trim(P_CLARowDS(W_RowIdx).RowTitle));

// Write the FirstRow section                                


For W_ColIdx = 1 to C_CLAMaxColumns;                        

   If P_CLARowDS(W_RowIdx).Cell(W_ColIdx).CellValue= *Zeros;  



   updHtmlVar('ColNbr' : %Char(W_ColIdx));                    

   updHtmlVar('CellValue' : %Char(                            


   // Write the Row section                                  





The only problem was that you could decide not to provide values for all the cells in each row or for all the rows (the maximum number of rows is defined in the C_CLAMaxRows constant). This problem was solved by checking whether the data structure fields shown below were filled right in the beginning of each iteration of the respective cycles:





However, this coding assumes that a missing value indicates that the data for that row is over. If you want to allow missing values in the middle of a row, just change the "Leave" statement to an "Iter" statement. Just one more detail: this procedure has a rather unique parameter, the P_ChartType. As the name implies, it's used to determine which type of chart (column, line, or area) will be produced. The rest of the procedure's code is quite simple and similar to the previously presented procedures in this TechTip series.


As you probably guessed by now, this procedure can also be used to produce bar charts, since they are also part of Google's "corechart" package and the bar chart shares the same structure used by these three types of chart. The procedure needs only a few adjustments related to the P_ChartType parameter. If you want to do it, just allow the B value in the Init routine check and translate it into Bar on the Footer section of the Proc routine.


Now let's see what this piece of code can generate. I provided three sample programs (TSTGENCLA1, TSTGENCLA2, and TSTGENCLA3), each of them producing a different type of chart. You can transform the chart produced by the first sample program just by changing the value assigned to the P_ChartType variable to L or A. That will cause Figure 1 to become Figure 2, as shown below:



Figure 1: You had a column chart.



Figure 2: Now your column chart is a line chart.


The values are exactly the same, but their graphical representation lost clarity. Depending on the number of columns and distribution of the values, what seemed a perfectly good choice can produce a rather confusing result.


Another example: let's take the line chart produced by TSTGENCLA2 and change it to a column chart. We'll go from what's shown in Figure 3 to what's shown in Figure 4:



Figure 3: You start with a line chart.



Figure 4: You change the line chart to a column chart.


Again, it loses clarity, and the evolution of the values also loses impact. So, what's the lesson here? As a rule of thumb, I would say that the line and area charts are better suited for fewer columns and more rows than the column chart. This is just my humble opinion, and I'm definitely not the world's leading chart expert, so feel free to use this procedure in any way that you'd like. If you figure out a way to improve it, be sure to let me know!


Naturally, the complete source code is available for download here, and I'm available for any questions or suggestions you may have. The next TechTip of this series will go back to the maps, by presenting the Intensity Map API.

Rafael Victoria-Pereira

Rafael Victória-Pereira has more than 16 years of IBM i experience, as a programmer, analyst, and manager. Over that period, he has been an active voice in the IBM i community, encouraging and helping programmers transition to ILE and free-format RPG. Rafael has written more than 50 technical articles about topics ranging from interfaces—the topic for his first book Flexible Input, Dazzling Output with IBM i—to modern RPG and SQL, in his popular RPG Academy and SQL 101 series on He writes in an easy-to-read, practical style that's highly popular with his audience of IBM technology professionals.

Rafael currently works as an Enterprise Architect at the Luis Simões Group in Portugal. His areas of expertise include programming in the IBM i native languages (RPG, CL, and DB2 SQL) and "modern" programming languages, such as C# and Python, as well as project management and consultancy.


MC Press books written by Rafael Victória-Pereira available now on the MC Press Bookstore.


Flexible Input Dazzling Output with IBM i Flexible Input, Dazzling Output with IBM i
Flexible Input, Dazzling Output (FIDO) is all about interfaces. It shares more with a dog than just its name: it has a few tricks of its own!

List Price $71.95
Now On Sale


Evolve Your RPG Coding Evolve Your RPG Coding
Evolve Your RPG Coding helps you transition to modern RPG programming, guiding you step by step through ILE and free-format RPG, SQL, and modernization techniques.

List Price $79.95
Now On Sale