Skip to content

Metric

The Metric object defines an aggregated measure returned by the data provider. Metrics describe which field should be evaluated and which aggregation should be applied, and you pass one or more Metric instances to the .metrics() function when building a query.

Create a metric with cf.Metric(field?, operation?). When you omit both arguments ChartFactor returns the default record count. Supplying a field and operation lets you aggregate that field explicitly, while providing only the field falls back to the default aggregation defined for that field in your provider metadata.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
let provider = cf.provider("Aktiun Elastic");
let source = provider.source('ticket_sales');
// The count metric
let metric0 = cf.Metric();
// commission sum
let metric1 = cf.Metric("commission", "sum").hideFunction();
// price paid sum
let metric2 = cf.Metric("pricepaid", "sum").hideFunction();
// Define attributes to group by
let group = cf.Attribute("eventname.keyword")
    .limit(10)
    .sort("desc", metric1);
// Add metrics and groups to data source
let myData = source.groupby(group)
    .metrics(metric0, metric1, metric2);

The first metric in the example above is the count metric that is returned when no field or operation is specified. The other metrics specify the measurement field and the aggregation function (operation) that the returned value represents.

Aggregation operations

  • count: Number of records in each result group. Returned when no field or operation is supplied.
  • sum: Total sum of a numeric field within the result group.
  • avg: Arithmetic average of a numeric field.
  • min: Smallest value observed in the group.
  • max: Largest value observed in the group.
  • unique: Count of distinct values (count distinct) for the field.
  • unique_approx: Approximate count of distinct values for the field, using a probabilistic algorithm that is more efficient for large datasets. This is supported only by Databricks, Spark and Google BigQuery providers.
  • percentiles: Percentile statistics for the field. Returns multiple values per group, including minimum, first quartile (Q1), median (Q2), third quartile (Q3), and maximum.
  • histogram: Buckets numeric values into intervals to build a distribution. Use helpers such as .fixedBars() or .interval() to control the bucket definition.

Hiding the aggregation function

By default the metric aggregation function is displayed along with the metric label in the chart axis, legend, and tooltips. To only render the metric label and hide the aggregation function we can use the hideFunction() method as shown below:

1
cf.Metric('pricepaid', 'sum').hideFunction()

The picture below shows how metric functions are rendered by default:

image

The picture below shows how different chart elements render when the metric function is hidden:

image

Position

For multimetric trend charts, by default metrics are rendered using the left y-axis. You can specify metric(s) to use the right axis with the position function as shown below:

1
cf.Metric("fare", "sum").position("right")
The picture below shows the fare metric using the right axis:

image