Skip to main content

Histogram

A histogram groups numerical values into discrete ranges (bins) and uses bars to show how many values fall in each bin. It is a simple yet effective way to grasp the distribution of a numeric variable.

  • Good for: seeing the distribution of a single numeric variable, spotting skew or outliers, checking how values cluster (order value, response time, age).
  • Not great for: comparing distributions across categories (use a ridgeline chart), categorical counts (use a bar chart), or two-metric density (use a density contour plot).
reporting-custom-chart/histogram

Syntax

Use the following AML definition to add the Histogram to your custom chart library.

Legacy syntax
CustomChart {
fields {
field grain {
type: 'dimension'
}
field metric {
type: 'dimension' // this type can be dimenson or measure depends on the type of field that you want to create the histogram on
}
}
template: @vgl
{
"data": {"values": @{values}},
"transform": [
{"bin": {"maxbins": 40}, "field": @{fields.metric.name}, "as": "binned_price"}
],
"mark": {
"type": "bar",
"tooltip": true
},
"encoding": {
"x": {"field": "binned_price", "type": "quantitative", "bin": {"binned": true, "step": 20}},
"x2": {"field": "binned_price_end"},
"y": {"aggregate": "count"}
}
}
;;
}

Required fields

A Histogram expects exactly two fields. Each row of input is one record; the chart bins the numeric value and counts how many records land in each bin.

FieldLabelTypeRole
dimensionDimensiondimensionGrain of the data (one row per record). Sorted ascending (apply_order: 1).
valueValuedimensionNumeric value to bin along the x-axis. Sorted ascending (apply_order: 2).

Data requirements: Feed raw rows (one per record), not pre-aggregated counts, since the template bins value and counts records itself. value must be numeric.

Sample data:

dimensionvalue
order_142
order_217
order_388
order_451
order_523
order_664

Options

Set these options to adjust the chart without editing the Vega template. The CustomChartDef block above declares each option's type and allowed values.

OptionDefaultEffect
max_bins40Maximum number of bins. Vega picks a clean bin width at or below this count; lower it for wider bins.

Known limitations

  • value must be numeric. The bin transform runs on value, so a non-numeric field cannot be binned. Use a bar chart for categorical counts.

  • Needs raw rows, not aggregates. The chart counts records per bin, so pre-summarized input distorts the distribution. Feed one row per record.

  • Bin width is approximate. max_bins is an upper bound, not an exact count; Vega rounds to a readable bin width, so the rendered bar count can be lower.


Open Markdown
Let us know what you think about this document :)