Parameterizing Variables

To make our QuickApp more interesting and useful we should add additional observations for the QuickApp to display. To do this, we need to give the user a way to select from columns in the table that have numeric values. We can do this by querying the metadata of the base table and then making the column names selectable in a drop-down. This will allow us to select from the available observations and see the results of the initial query.

To start, open the original base table in a new tab. The goal is to query the table and return every column name in it that holds a numeric value. By parameterizing the column in the initial query the QuickApp will have a lot more functionality.

We can retrieve a list of all the columns in the table with a simple metadata query. Here's the query code:

<columns full="0"/>
<sel value="(type='f''i')"/>
<colord cols="name,label"/>
And here's what the results look like when we run the query:
Note: Make sure you opened a new instance of the base table to run the query!

Now that we have a list of the available measures, let's add them to the QuickApp. First, go back to the Drop QA tab where the QuickApp lives.

Now click View > QuickApp Editor... to open the QuickApp Editor again. Then, right-click to open the shortcut menu and click Add a new widget here to add a new widget right next to the first input field we added. From the Class drop-down list, click Dropdown menu. In the Label field on the Class Properties tab, enter "X Variable". Next to the Variable for value drop-down, click the New... link and enter xaxis as the variable name.

Now, click the header for the Query content area below the Properties content area. The Query content area lets you define the query logic that will populate your widgets with values. This area is aware of the metadata query we just ran and anything happening in your current 1010data session. Click the Worksheets tab and then, from the Available worksheets drop-down list, click Observed Daily to select the metadata worksheet we just created.
Note: The worksheet that we just created is the one that appears at the bottom of the drop-down list. The one that appears above it is the original table we started working with at the beginning of this tutorial.

When you've selected the correct worksheet, click the Import button. Then click the Save button to save the new widget we just created.

Finally, remember to right-click on the grid and select Commit changes to this QuickApp... We now have a new drop-down menu populated with all the numeric columns from the base table:

To go further, we're going to have to add some code directly into the Macro Language macro we're working on. Click Actions > Edit Actions (XML).... Here's what the code looks like so far:

<dynamic bucksize="5" xaxis="">
  <widget class_="graphics" height_="400" name="hmnonce__1" width_="600">
    <sel value="between(meantempi;-50;50)"/>
    <sel value="between(snowfalli;0;120)"/>
    <willbe name="xbuck" value="round(meantempi;{@bucksize})"/>
    <tabu label="Tabulation on Observed Daily" breaks="xbuck">
      <break col="xbuck" sort="up"/>
      <tcol source="xbuck" fun="cnt" name="num" label="Number of`Observations"/>
      <tcol source="snowfalli" fun="avg" name="yaxis" label="Average`Snow (in)"/>
    </tabu>
    <graphspec width="600" height="400">
      <chart type="line" samples="100000">
        <data x="xbuck" y="yaxis"/>
      </chart>
    </graphspec>
  </widget>
  <widget class_="field" label_="X Bucket Size" name="hmadded__1" value_="@bucksize"/>
  <widget base_="pub.demo.weather.wunderground.observed_daily" class_="dropdown" label_="X Variable" name="hmadded__2" value_="@xaxis">
    <columns full="0"/>
    <sel value="(type='f''i')"/>
    <colord cols="name,label"/>
  </widget>
</dynamic>
Note: Make sure you remove the relpos attribute from the new drop-down widget in the code.

The QuickApp editor has placed the xaxis variable in the opening <dynamic> tag, just as it did with the initial input field widget. In order to have the QuickApp use the column selected in the drop-down, we have to remove the hard-coded value from the initial query and instead reference this new xaxis variable. Below is the new code with the changes in bold:

<dynamic bucksize="5" xaxis="meantempi">
  <widget class_="graphics" height_="400" name="hmnonce__1" width_="600">
    <sel value="between({@xaxis};-50;50)"/>
    <sel value="between(snowfalli;0;120)"/>
    <willbe name="xbuck" value="round({@xaxis};{@bucksize})"/>
    <tabu label="Tabulation on Observed Daily" breaks="xbuck">
      <break col="xbuck" sort="up"/>
      <tcol source="xbuck" fun="cnt" name="num" label="Number of`Observations"/>
      <tcol source="snowfalli" fun="avg" name="yaxis" label="Average`Snow (in)"/>
    </tabu>
    <graphspec width="600" height="400">
      <chart type="line" samples="100000">
        <data x="xbuck" y="yaxis"/>
      </chart>
    </graphspec>
  </widget>
  <widget class_="field" label_="X Bucket Size" name="hmadded__1" value_="@bucksize"/>
  <widget base_="pub.demo.weather.wunderground.observed_daily" class_="dropdown" label_="X Variable" name="hmadded__2" value_="@xaxis">
    <columns full="0"/>
    <sel value="(type='f''i')"/>
    <colord cols="name,label"/>
  </widget>
</dynamic>

We're close to having our x-axis fully parameterized. However, the minimum and maximum values specified in our original query are still hard-coded. Let's parameterize these variables as well. Of course, we'll also need a way for the user to set those values. So we'll add two more input field widgets to set these values. You can do this in the QuickApp Editor, or in the Macro Language itself. Since we've already looked at how to add input field widgets in the QuickApp Editor, this time we will do it right in the XML code. The code example below shows the new widgets and parameterized variables in bold:

<dynamic bucksize="5" xaxis="meantempi" xmin="-50" xmax="50">
  <widget class_="graphics" height_="400" name="hmnonce__1" width_="600">
    <sel value="between({@xaxis};{@xmin};{@xmax})"/>
    <sel value="between(snowfalli;0;120)"/>
    <willbe name="xbuck" value="round({@xaxis};{@bucksize})"/>
    <tabu label="Tabulation on Observed Daily" breaks="xbuck">
      <break col="xbuck" sort="up"/>
      <tcol source="xbuck" fun="cnt" name="num" label="Number of`Observations"/>
      <tcol source="snowfalli" fun="avg" name="yaxis" label="Average`Snow (in)"/>
    </tabu>
    <graphspec width="600" height="400">
      <chart type="line" samples="100000">
        <data x="xbuck" y="yaxis"/>
      </chart>
    </graphspec>
  </widget>
  <widget class_="field" label_="X Bucket Size" name="hmadded__1" value_="@bucksize"/>
  <widget base_="pub.demo.weather.wunderground.observed_daily" class_="dropdown" label_="X Variable" name="hmadded__2">
    <columns full="0"/>
    <sel value="(type='f''i')"/>
    <colord cols="name,label"/>
  </widget>
  <widget class_="field" label_="X Min:" value_="@xmin"/>
  <widget class_="field" label_="X Max:" value_="@xmax"/>
</dynamic>

Click Apply in the Edit Actions (XML) dialog to see the new widgets we have added to our QuickApp:

Notice that we started building our QuickApp in the QuickApp Editor, but have also made edits to the automatically generated Macro Language code in the Edit Actions (XML) dialog. It is perfectly acceptable to work in this way, and as you get a feel for building QuickApps, you will be able to optimize your workflow by understanding the best times to work in each area. In the next section, we'll jump back into the QuickApp Editor and look at how to clone a widget.