Aggregate Data from a Data Store Entity using a Filter and Display in a Chart

SAIL Recipes give you an opportunity to explore different interface design patterns. To learn how to directly use SAIL recipes within your interfaces, see Adapt a SAIL Recipe to Work with My Applications.

Goal

Aggregate data from a data store entity, specifically the total number of employees for each title in the Engineering department, to display in a bar chart.

image:SAIL_Recipe_Bar_Chart_Post_Filter.png

This recipe uses an employee data structure and objects created through the Use the Write to Data Store Entity Smart Service Function on an Interface recipe. Make sure that recipes has been built first in order to see data in this recipe.

This scenario demonstrates:

  • How to use the report builder to aggregate data from a data store entity and display in a bar chart
  • How to modify the generated expression to add a default filter to the query

Using The Report Builder

image:SAIL_Recipes_report_builder_bar_chart.png

  1. Open the Interface Designer and select Report Builder from the list of templates.
  2. In the Source Constant field, select the EMPLOYEE_ENTITYconstant.
  3. Delete the firstName, lastName, and department columns so that only the id column remains.
  4. In the Add a field... dropdown, select title and click Add Field.
  5. Select the Group records by common fields option and select Count for the id column.
  6. Set the display name for the id column as Total.
  7. Select Bar Chart as the visualization.
  8. In the Chart Labels dropdown, select title.
  9. Click Generate.

You should see a chart that looks similar to this:

images:SAIL_Recipe_Bar_Chart_Pre_Filter.png

Expression

To add a filter, enter the following expression after the aggregation parameter of a!query():

filter: a!queryFilter(field: "department", operator: "=", value: "Engineering"),

The comma at the end of this expression is included because pagingInfo is defined later. Your final expression should look like this:

load(
  local!pagingInfo: a!pagingInfo(
    startIndex: 1,
    batchSize: -1,
    sort: a!sortInfo(
      field: "id",
      ascending: true
    )
  ),
  with(
    local!datasubset: a!queryEntity(
      entity: cons!EMPLOYEE_ENTITY,
      query: a!query(
        aggregation: a!queryAggregation(aggregationColumns: {
          a!queryAggregationColumn(field: "id", aggregationFunction: "COUNT"),
          a!queryAggregationColumn(field: "title", isGrouping: true),
        }),
        filter: a!queryFilter(field: "department", operator: "=", value: "Engineering"),
        pagingInfo: local!pagingInfo
      )
    ),
    a!barChartField(
      categories: {
        index(local!datasubset.data, "title", null)
      },
      series: {
        a!chartSeries(
          label: "id",
          data: index(local!datasubset.data, "id", null)
        )
      }
    )
  )
)

Notable implementation details

  • The expression for the filter is being passed into the filter parameter of a!query(). If you would like to filter using multiple constraints, use a!queryLogicalExpression() in the logicalExpression parameter instead.
FEEDBACK