Navigation

Bin, Sort, and Limit Your Data

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

Bin Data

MongoDB Charts supports binning both numeric and date 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.

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.

Bin Date Fields

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. MongoDB 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, MongoDB 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, MongoDB Charts plots the mean value of sales from each individual date of the month:

Charts binning by day example

Compare Periods

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

Bin Numeric Fields

MongoDB Charts can also bin numeric fields from a continuous set into groups of a specified size. This

To use binning for numeric fields:

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

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

  2. Enter the desired bin size in the Bin Size input.

To disable binning, toggle the Binning setting to off. In this case, each individual value from the field is added to the visualization, rather than being grouped into bins.

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 sale document contains a customer object, which contains information on the customer purchasing the item(s).

The following chart applies binning to the customer.age field. The age field contains continuous numeric data and is binned by decade. Each age value is placed into the appropriate bin and Charts counts the number of ages in each bin to provide a high-level overview of the age distrubution of store customers.

Charts numeric binning example

Empty Bins

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

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
Column and Bar Charts Bars with a height of 0. Charts doesn’t display data labels for null bins, even if enabled.
Line and Area Charts Linear interpolation, with no data marker on the null bins. Charts doesn’t display data labels for null bins, even if enabled.

Sort Data

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, MongoDB 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.

Sort Multi-Series Charts by Series Value

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

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

Chart sorted by rotten rating in ascending order

Limit Data

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.

Example

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

Movie ratings by country

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. 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