Vega Lite
Data Transformation
How Can I Use the Flatten Transform in Vega-Lite?

How Can I Use the Flatten Transform in Vega-Lite?

Are you dealing with nested arrays in your data and need a way to visualize each element individually? The flatten transform in Vega-Lite is your go-to tool! This transform takes array-valued fields and maps them to individual data objects, making it much easier to create detailed and expressive visualizations.

Why Use the Flatten Transform?

The flatten transform is useful when you want to treat each element in an array as its own data row. This way, you can generate more granular visual insights from nested data structures.

How Does It Work?

The flatten transform generates a new data stream where each data object consists of an extracted array value, along with the original fields of the corresponding input data object.

Here's the basic structure:

// Any View Specification
{
  ...
  "transform": [
    {"flatten": ...} // Flatten Transform
  ],
  ...
}

Define Your Flatten Transform

The key properties for the flatten transform are:

  • flatten: An array of field names you want to flatten.
  • as: (Optional) The output field names for the flattened fields.

Quick Example

Let's see it in action with a simple JSON example:

{
  "flatten": ["foo", "bar"]
}

Input Data

Imagine your data looks like this:

[
  {"key": "alpha", "foo": [1, 2], "bar": ["A", "B"]},
  {"key": "beta", "foo": [3, 4, 5], "bar": ["C", "D"]}
]

Output Data

After applying the flatten transform, your data will look like this:

[
  {"key": "alpha", "foo": 1, "bar": "A"},
  {"key": "alpha", "foo": 2, "bar": "B"},
  {"key": "beta", "foo": 3, "bar": "C"},
  {"key": "beta", "foo": 4, "bar": "D"},
  {"key": "beta", "foo": 5, "bar": null}
]

More Examples

Basic Example

This example uses the flatten transform on two fields, adding null values where shorter arrays are extended to match longer ones.

{
  "transform": [
    {"flatten": ["foo", "bar"]}
  ]
}

Advanced Example: Coordinated Views with Nested Time Series

In this scenario, a single field is flattened and then used to plot a line chart that coordinates with a circle chart.

{
  "vconcat": [
    {
      "transform": [
        {"flatten": ["date"]}
      ],
      "mark": "line",
      "encoding": {
        "x": {"field": "date", "type": "temporal"},
        "y": {"field": "value", "type": "quantitative"}
      }
    },
    {
      "mark": "circle",
      "encoding": {
        "x": {"field": "date", "type": "temporal"},
        "y": {"field": "value", "type": "quantitative"},
        "color": {"field": "category", "type": "nominal"}
      }
    }
  ]
}

FAQ

What happens if the arrays have different lengths?

When arrays have different lengths, the shorter array will be filled with null values to match the longest array.

Can I specify custom output field names?

Yes, you can use the as property to specify custom names for the fields in the output data stream.

Can flatten transform handle multiple arrays simultaneously?

Absolutely! You can list multiple fields in the flatten array to flatten them all at once.

There you have it! The flatten transform is a powerful tool for breaking down nested arrays in your data, making each element individually accessible for visualization. Happy visualizing!