TechTip: Column, Line, and Area Charts: Generate Professional-Grade Charts in Your Programs, Part 6 PDF Print E-mail
Tips & Techniques - Programming
Written by Rafael Victoria-Pereira   
Friday, 08 June 2012 00:00

Support MC Press - Visit Our Sponsors

Forums Sponsor

POPULAR FORUMS

Forums

 

Search Sponsor

 

Search

 

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:

 

/$Header

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

   <title>

     /%PageTitle%/

   </title>

   <script type="text/javascript" src="https://www.google.com/jsapi"></script>

   <script type="text/javascript">

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

     google.setOnLoadCallback(drawChart);

     function drawChart() {

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

       data.addRows(/%TotalRows%/);

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

/$Column

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

/$FirstRow

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

/$Row

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

/$OptionsBegin

 

     var options = {

/$Option

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

/$OptionsEnd

                   };

/$Footer

 

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

       chart.draw(data, options);

     }

   </script>

</head>

 

<body>

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

</body>

</html>

 

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;                  

   Leave;                                                    

EndIf;                                                      

// Update the Row variables                                  

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

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

// Write the FirstRow section                                

WrtSection('FirstRow');                                      

For W_ColIdx = 1 to C_CLAMaxColumns;                        

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

     Leave;                                                  

   EndIf;                                                    

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

   updHtmlVar('CellValue' : %Char(                            

               P_CLARowDS(W_RowIdx).Cell(W_ColIdx).CellValue));

   // Write the Row section                                  

   WrtSection('Row');                                        

   EndFor;                                      

EndFor;                                          

 

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:

 

P_CLARowDS(W_RowIdx).Cell(W_ColIdx).CellValue

P_CLARowDS(W_RowIdx).RowTitle

 

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:

 

060812Rafaelfigure1

Figure 1: You had a column chart.

 

060812Rafaelfigure2

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:

 

060812Rafaelfigure3

Figure 3: You start with a line chart.

 

060812Rafaelfigure4

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
About the Author:

Rafael Victoria-Pereira is an analyst/developer with 10+ years of System i experience. He is Application Development Manager at LeasePlan in Portugal. His areas of expertise cover System i, Lotus Domino/Notes, and several Windows-based programming languages.

Read More >>
Last Updated on Friday, 08 June 2012 15:12
 
User Rating: / 1
PoorBest 
   MC-STORE.COM