Sankey diagram

Sankey visualisation

Updated 6 years ago by Template retirement home

How to use this template

Sankey diagram

A template for creating simple two-stage Sankey diagrams. Ideal for displaying the flow of seats between parties from before to after an election.

Data requirements

The template works with two types of data format:

  1. Flows are already in the data, and each row has a source name, target name and flow value.

  2. Flows are not already in the data. In this model, each row is a entity (e.g. a seat in an election), with a source name and target name (e.g. representing the ruling party before and after the election). With this kind of data the flows are computed automatically by adding up each source–target pair.

Tips

  • If specifying flow values directly, the column must contain positive numbers or zero
  • Names are compared and values summed using case-insensitive comparisons with all non-alphanumeric characters stripped. For example “Cat“ and “cA ?t“ are considered identical.
  • Hovering over a link will defocus all other links and any node not connected to that link. Hovering over a node will defocus all links that are not directly connected to that node and all nodes not connected via a link to the hovered node. You can fix focus on a node or link and all its connections by clicking on the entity. This is particularly useful for guiding viewers through your stories. Click a focused node or link again to remove the focus or click any other node or link to change focus to that entity.

This section documents API usage specific to this template, so for an introduction we suggest you refer to the generic API documentation instead.

template: _627

version: _3

Template data

There are three different formats in which you can supply data to this template. The most convenient for you to use likely depends on the source of your data, as described below.

1. Array of arrays, and a bindings object

You can supply arrays of arrays to opts.data, which might look like:

{
    data: {
        links: [
            [ "LinksColumn1Value1", "LinksColumn2Value1",
            [ "LinksColumn1Value2", "LinksColumn2Value2",
            [ "LinksColumn1Value3", "LinksColumn2Value3",
            ...
        ]
    }
}

where each array of arrays represents the rows in a data sheet.

To tell the API how the values from each column should be associated with the keys that the template is expecting, you must also supply an object attached to opts.bindings. (The meanings of the keys in the bindings object are documented below.) The minimal bindings you can supply for this template are as shown in this example:

{
    template: "_627",
    version: "_3",
    bindings: {
        links: {
            source: 0, // index of a column in your data
            target: 1, // index of a column in your data
        }
    },
    data: {
        links: [
            [ "LinksColumn1Value1", "LinksColumn2Value1",
            [ "LinksColumn1Value2", "LinksColumn2Value2",
            [ "LinksColumn1Value3", "LinksColumn2Value3",
            ...
        ]
    }
}

All possible bindings that you can supply are shown in this example:

{
    template: "_627",
    version: "_3",
    bindings: {
        links: {
            source: 0, // index of a column in your data
            target: 1, // index of a column in your data
            value: 2, // index of a column in your data
            step_from: 3, // index of a column in your data
            step_to: 4, // index of a column in your data
        }
    },
    data: {
        links: [
            [ "LinksColumn1Value1", "LinksColumn2Value1",
            [ "LinksColumn1Value2", "LinksColumn2Value2",
            [ "LinksColumn1Value3", "LinksColumn2Value3",
            ...
        ]
    }
}

2. Array of objects with arbitrary keys, and a bindings object

This format is most likely useful when you have data from an external source, such as CSV data loaded from d3-dsv. You should supply this attached to the opts.data, which might look like:

{
        links: [
            { "LinksHeader1": ..., "LinksHeader2": ..., ... },
            { "LinksHeader1": ..., "LinksHeader2": ..., ... },
            { "LinksHeader1": ..., "LinksHeader2": ..., ... },
            ...
        ]
    }

... but with the keys being the column headers from your source data instead. You must also supply an object attached to opts.bindings. The minimal bindings you can supply for this template are as shown in this example:

{
    template: "_627",
    version: "_3",
    bindings: {
        links: {
            source: "LinksHeader1",
            target: "LinksHeader2",
        }
    },
    data: {
        links: [
            { "LinksHeader1": ..., "LinksHeader2": ..., ... },
            { "LinksHeader1": ..., "LinksHeader2": ..., ... },
            { "LinksHeader1": ..., "LinksHeader2": ..., ... },
            ...
        ]
    }
}

All possible bindings that you can supply are shown in this example:

{
    template: "_627",
    version: "_3",
    bindings: {
        links: {
            source: "LinksHeader1",
            target: "LinksHeader2",
            value: "LinksHeader3",
            step_from: "LinksHeader4",
            step_to: "LinksHeader5",
        }
    },
    data: {
        links: [
            { "LinksHeader1": ..., "LinksHeader2": ..., ... },
            { "LinksHeader1": ..., "LinksHeader2": ..., ... },
            { "LinksHeader1": ..., "LinksHeader2": ..., ... },
            ...
        ]
    }
}

(As before, the keys containing "Header" would be replaced by column names from your data source.)

3. Array of objects with template-defined keys

There is an alternative format you can use, which is likely to be easier to use if your data is not from a spreadsheet source. With this alternative format you supply your data to the template as an array of objects, attached to opts.data, where the keys must be those used by the template, as documented below. In this case there is no need to supply a bindings object, since the key names are already those expected by the template. The required properties in the data object are as follows (scroll down for a description of what each property is):

{
    template: "_627",
    version: "_3",
    data: {
    links: [
        {
            source: ...,
            target: ...
        },
        ...
    ]
},
    ...
}

And the full list of all possible properties is as follows:

{
    template: "_627",
    version: "_3",
    data: {
    links: [
        {
            source: ...,
            target: ...,
            value: ...,
            step_from: ...,
            step_to: ...
        },
        ...
    ]
},
    ...
}

Meanings of the template data keys:

  • links.source: source
  • links.target: target
  • links.value: A numerical value used to set the thickness of the links.
  • links.step_from: An integer used to indicate where a link starts from (used only in Alluvial Mode).
  • links.step_to: An integer value used to indicate where a link goes to (used only in Alluvial Mode).

Template settings

Options for opts.state.

Layout

mode string

Mode.

Allowed values:

  • alluvial (Alluvial)
  • traditional (Traditional)

iterations number

Iterations.

Max: 100

spread string

Spread.

Allowed values:

  • step (Based on step values)
  • column (Evenly)

end_step number

End step. Will be ignored if less than the maximum step value

alignment string

Allignment.

Allowed values:

  • justify (Justify)
  • center (Centre)
  • left (Left)
  • right (Right)

node_width number

Node width.

Min: 2

Max: 50

node_padding number

Vertical node separation.

Max: 20

Title and subtitle

title string

Title.

subtitle string

Subtitle.

title_padding number

Margin above/below.

title_text_color color

Colour.

Appearance

bg_color color

Background colour.

max_margin number

Margin. Maximum margin, may be less on small screens

Max: 50

color_mode string

Palette.

Allowed values:

  • single (Single colour)
  • generated (Generated sequence)

custom_palette text

Custom overrides. Associate nodes with colours using the format <name>:<colour>, one entry per line.

base_color color

Base color.

Link opacity.

Max: 1

background_opacity number

Defocused opacity. When a node or link is in focus, this sets the opacity of the others

Max: 1

label_color color

Colour.

font_size number

Size. In pixel

Min: 6

Max: 50

show_values boolean

Show values.

Show link values.

Allowed values:

  • always (Always)
  • focused (When focused)

value_outline string

Value outline colour.

Allowed values:

  • none (None)
  • label_color (Label)
  • bg_color (Background)

Number formatting

decimal_separator_in_data string

Decimal separator in data sheet. Used for interpreting your data. Only change if data is not displaying on the chart as expected.

Allowed values:

  • . (.)
  • , (,)

display_format string

Number format to display. How the numbers should appear on chart labels

Allowed values:

  • ,. (12,235.67)
  • ., (12.345,67)
  • . (12235.67)
  • , (12345,67)
  • . (12 235.67)
  • , (12 345,67)

prefix string

Prefix. Text to place in front of labels

n_dec number

Decimal places. Use negative integers to round to positive powers of ten (eg -2 rounds to the nearest 100)

Min: -10

Max: 10

suffix string

Suffix. Text to place after labels

Animation

transition_duration number

Transition duration. Measured in seconds