For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Bin, Sort, and Limit Your Data

You can bin, sort, and limit data in your charts to highlight key aspects in your data.

Atlas Charts supports binning date, numeric, and string fields in your data. Binning breaks continuous data into discrete groups called bins, with each bin containing a contiguous subset of the original values. For example, you might group users into bins by the decade they were born, or group timestamped calendar events by the month of their start date.

Example

Continuous vs Discrete Data

Continuous data can occupy any value over a continuous range. Some examples of continuous data include height, temperature, or the time a person was born.

Alternatively, discrete data is data which can only take certain values, categorized into a classification. Examples of discrete data include eye color and the number of students in a class.

When handling dates, this data often comes in a continuous form. It can be useful to split this data into a specific windows of time to uncover additional trends and patterns. Atlas Charts supports the following bins for dates:

  • Year

  • Month

  • Week of Year

  • Date of the Month

  • Day of the Week

  • Hour

  • Minute

  • Second

To use binning for dates:

  1. Drag a date field (signified by a calendar icon in the Fields section of the Chart Builder) to a category encoding channel.

    By default, binning is enabled as indicated by the Binning On toggle.

  2. Use the dropdown to select the bin size for your dates.

  1. Toggle the Periodic setting as desired.

    • If enabled, Atlas Charts creates bins relative to the next highest time period and repeats each bin to cover the timeframe spanned in the data field.

    • If disabled, the bins Charts creates do not repeat.

    Example

    Consider a date field containing data spanning 5 years, and a bin selection of Month. If Periodic is disabled, Charts adds the 60 months in the dataset to the visualization.

    Alternatively, if Periodic is enabled, Charts only plots the 12 calendar months, and visualizes the total aggregated results from each calendar month.

    Note

    • The Periodic setting is always enabled when selecting a bin size of Day of the Week.

    • The Periodic setting is always disabled when selecting a bin size of Year, because year is the largest time period available in Charts.

To disable binning, toggle the Binning setting to off. In this case, each date from the field is added to the visualization with no grouping performed.

Example

The following chart visualizes historical sales data for an office supply store. Every sale is represented by a document in the supplySales collection. Each document in the collection includes the date of the sale as salesDate and a list of items included in the sale as the items array.

The saleDate field contains continuous timestamp data. The field is binned by Date of the Month with the Periodic setting enabled. This means that over the course of the several years the data spans, Atlas Charts plots the mean value of sales from each individual date of the month:

Charts binning by day example
click to enlarge

When you use binned date data, you have the option to compare data across periods of time. You can display cumulative totals or changes in value.

To use the compare periods feature, a chart must have:

If the conditions are met, the aggregation card has a toggle switch labeled Compare Periods. When toggled on, a dropdown menu appears with options for Cumulative Total and Change in Value.

On a Cumulative Total chart, each successive data point is calculated by adding its own value added to the preceding chronological data point. You can also specify a baseline value to serve as the initial value of the cumulative total. The default baseline value is 0.

On a Change in Value chart, each successive data point shows the difference between it and the preceding chronological data point. The first data point in the chart will always be blank, because there is no previous period to compare against.

The following types of charts can use the compare periods feature:

Note

The cumulative data option is not available with multi-series charts.

Example

The following column chart visualizes yearly sales for an office supply store as a cumulative total across five years. Each document in the collection includes the date of the sale as salesDate and a quantity field with a count of items included in the sale.

The saleDate field is binned by year. The items field is an array in which each item has a quantity field, which is added to the cumulative total with a SUM aggregation operation.

Charts cumulative total example
click to enlarge

When binning is enabled, Charts displays entries for empty bins within the minimum and maximum data range a chart displays.

Note

Exception

Charts doesn't display empty bins if including them results in more than 5000 unique bins on a chart.

The value Charts uses for empty bins depends on the aggregation function you choose:

Aggregation Function
Inferred Value

count or distinct

0

All Other Functions

null

Charts displays bins with null values differently based on chart type:

Chart Type
How Charts Displays null Bins

Bars with a height of 0. Charts doesn't display data labels for null bins, even if enabled.

Linear interpolation, with no data marker on the null bins. Charts doesn't display data labels for null bins, even if enabled.

Use the Sort By dropdown in the Chart Builder to sort chart data by either:

If you sort a multi-series chart generically by Value without sorting by a specific series, Atlas Charts sorts your data based on the sum of all values in your series.

To toggle between ascending or descending sort order, click the a-z button to the right of the Sort By dropdown.

By default, Charts sorts data based on Value in descending order.

If you create a multi-series chart using a different field per series, you can sort the chart by a specific series field.

Example

The following charts use the Sample Data: Movies data source to compare the mean number of fresh and rotten Rotten Tomato ratings for movies in each genre.

This chart is sorted by the mean fresh value in descending order:

Chart sorted by fresh rating in descending order
click to enlarge

This chart is sorted by the mean rotten value in ascending order:

Chart sorted by rotten rating in ascending order
click to enlarge

You can apply a limit to the Category encoding channel to only include a specified number of categories in your visualization. The categories included are the first matching categories based on the sort order specified. Limiting data can be useful when visualizing data with so many categories it becomes difficult to create a meaningful chart.

When you limit your data, you may additionally enable Show "All Others" to create a new category called "All others" that combines the values of categories omitted by your limit.

Example

The following chart shows the average IMDb rating of movies from a particular country:

Movie ratings by country
click to enlarge

The dataset contains movies from many different countries, but it would be most interesting to see which countries produce the highest-rated movies. We can accomplish this by applying a limit to only show countries with the 10 highest average ratings for movies.

Switch the Limit Results toggle to On and leave the Show input at the default value of 10.

Check Show "All Others" to create an 11th column representing the average rating of movies from countries that aren't in the top 10.

The chart is now much easier to understand, and we have a clear view of the countries with the highest-rated movies:

Movie ratings by country limited
click to enlarge
Rate this page