How to Create Tables

A table displays rows and columns of data, showing how your variables change over time. Here is an example of a table:

TIME

Total Sales

Revenue

Profit

2003

115,000

41,400,000

7,114,286

2004

201,400

72,504,000

38,218,286

2005

214,812

77,332,318

43,046,604

 

If you don't care how the FML code works, just copy the code in the text box below and replace ModelVariable1, ModelVariable2, and ModelVariable3 with the variables from your model that you want to include in the table. If you have more or fewer variable, extend or reduce your list. Remember to put double-quotes around your variable names.

Copy-and-paste this code into your HTML page to create a table:

 

## Creating a Table with Forio Macro Language

#set ($TableVarList = ["ModelVariable1", "ModelVariable2", "ModelVariable3"])
#set ($TimeRange = [0..$Run.SimStep])

<TABLE ALIGN="left" BORDER=1 CELLPADDING=5 WIDTH="50%">

## Display the header row
<TR ALIGN="center" VALIGN="middle">
<TH>TIME</TH>

#foreach( $VariableName in $TableVarList)
<TH> $Values.get($VariableName).Label</TH>
#end

</TR>

## Display each data row
#foreach( $step in $TimeRange )
<TR ALIGN="right" VALIGN=middle">
<TD>$Run.convertStepToTime($step)</TD>

#foreach( $VariableName in $TableVarList)
<TD>$Values.get($VariableName).getResultFormatted($step)</TD>
#end
</TR>
#end

</TABLE>
 

 

Let's walk through the code to better understand what each section is doing.

Step 1: Define Variable and Time Lists

First, we need to create two arrays that contain the names of the variables we want to include in the table and the time range, up to the current time.

We do this by using the #set(  ) function. #set sets the value of the FML lists.

The following code creates two lists. One list is called $TableVarList. It's an array of the variables to be included in the table. The second list is called $TimeRange. It's a list of the time steps simulated so far, starting at 0 and going up to the current time step.

 

#set ($TableVarList = ["Total Sales", "Revenue", "Profit"])
#set ($TimeRange = [0..$Run.CurrentStep])
 

 

Step 2: Create the Titles for Each Column

Next, we create a table and generate the header row. The header row contains the names of the variables, not actual variable results. The following code creates the first row of the table:

 

<TABLE ALIGN="left" BORDER=1 CELLPADDING=5 WIDTH="50%">

## Display the header row
<TR ALIGN="center" VALIGN="middle">
<TH>TIME</TH>

#foreach( $VariableName in $TableVarList)
<TH> $Values.get($VariableName).Label</TH>
#end

</TR>
 

 

The looping occurs between the #foreach and #end statements. The #foreach statement uses the array $TableVarList that defined in the code above it. The #foreach takes a list of values in $TableVarList and assigns each one in turn to the variable $VariableName, executing the body of the loop (a block) once for each value.

So, for the three names in $TableVarList, we get three passes through the loop with $VariableName being a different value each time.

The HTML generated by the section of FML code above looks like this:

 

<TABLE ALIGN="left" BORDER=1 CELLPADDING=5 WIDTH="50%">

<TR ALIGN="center" VALIGN="middle">
<TH>TIME</TH>

<TH>Total Sales</TH>
<TH>Revenue</TH>
<TH>Profit</TH>

</TR>
 

 

Notice that the <TH> ...</TH> block has been repeated three times, with the variable name outputted each time.

 

Step 3: Display Variable Data

The third and final step is to display the data in each row. You can do that by using two #foreach loops, one for the variables and one for the time steps, as shown:

 

## Display each data row
#foreach( $step in $TimeRange )
<TR ALIGN="right" VALIGN=middle">
<TD>$Run.convertStepToTime($step)</TD>

#foreach( $VariableName in $TableVarList)
<TD>$Values.get($VariableName).getResultFormatted($step)</TD>
#end
</TR>
#end

</TABLE>
 

 

First, for the six years in $TimeRange, we get three passes through the loop with $step incrementing from 2003 to 2005.

 In the first column, we output the FML statement $Run.convertStepToTime which outputs the year. Then we start a second #foreach loop that outputs the values for the variables for that time.

The HTML generated by this section of FML code looks like this:

 

 <TR ALIGN="right" VALIGN=middle">
<TD>2003</TD>

<TD>115,000</TD>
<TD>41,400,000</TD>
<TD>7,114,286</TD>
</TR>
<TR ALIGN="right" VALIGN=middle">
<TD>2004</TD>

<TD>201,400</TD>
<TD>72,504,000</TD>
<TD>38,218,286</TD>
</TR>
<TR ALIGN="right" VALIGN=middle">
<TD>2005</TD>

<TD>214,812</TD>
<TD>77,332,318</TD>
<TD>43,046,604</TD>
</TR>

</TABLE>
 

 

The commas that you see in the HTML above are just '000s separators. Its the <TD>...</TD> that actually separate each variable value in the table.

 

Displaying Time Horizontally

As an added bonus, below is a table that displays time horizontally, in columns, instead of rows.

TIME

2003

2004

2005

2006

2007

2008

Total Sales

115,000

201,400

214,812

251,624

260,650

268,674

Revenue

41,400,000

72,504,000

77,332,318

90,584,748

93,833,835

96,722,505

Profit

7,114,286

38,218,286

43,046,604

56,299,034

59,548,121

62,436,791

 

Copy-and-paste this code into your HTML page to create a table with time listed in columns. Replace ModelVariable1, ModelVariable2, and ModelVariable3 with the variables from your model that you want to include in the table. If you have more or fewer variable, extend or reduce your list. Remember to put double-quotes around your variable names.

 

## Creating a Horizontal Table with Forio Macro Language
## Time is displayed in columns (the way most spreadsheets display time)

#set ($TableVarList = ["ModelVariable1", "ModelVariable2", "ModelVariable3"])
#set ($TimeRange = [0..$Run.EndStep])

<TABLE ALIGN="left" BORDER=1 CELLPADDING=5 WIDTH="100%">

## Display the header row
<TR ALIGN="center" VALIGN="middle">
<TH>TIME</TH>

#foreach( $step in $TimeRange)
<TD> $Run.convertStepToTime($step)</TD>
#end

</TR>

## Display each data row

#foreach( $VariableName in $TableVarList)
<TR ALIGN="right" VALIGN=middle">
<TH>$Values.get($VariableName).Label</TH>

#foreach( $step in $TimeRange)

#if($step <= $Run.SimStep)

<TD>$Values.get($VariableName).getResultFormatted($step)</TD>

#else

<TD>&nbsp;</TD>

#end

#end

</TR>

#end

</TABLE>