Slope chart

A simple slope chart for comparing two measures across a dataset

Updated 6 years ago by Template retirement home

How to use this template

Slope chart

A simple charting template to plot the differences between two values. Useful to compare the difference in population, income or wealth.

Data requirements

Each row after the header row is one slope. There should be a column for the name and two columns for the values. The values should be of the same data type (eg. votes, income, population).

Credits and feedback

Created by Flourish. Sample data is from the World Bank illustrating the change in urbanization. Want to see additional features? Let us know at support@flourish.studio.

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

template: _555

version: _11

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: {
        data: [
            [ "DataColumn1Value1", "DataColumn2Value1",
            [ "DataColumn1Value2", "DataColumn2Value2",
            [ "DataColumn1Value3", "DataColumn2Value3",
            ...
        ]
    }
}

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: "_555",
    version: "_11",
    bindings: {
        data: {
            name: 0, // index of a column in your data
        }
    },
    data: {
        data: [
            [ "DataColumn1Value1", "DataColumn2Value1",
            [ "DataColumn1Value2", "DataColumn2Value2",
            [ "DataColumn1Value3", "DataColumn2Value3",
            ...
        ]
    }
}

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

{
    template: "_555",
    version: "_11",
    bindings: {
        data: {
            name: 0, // index of a column in your data
            values: [1, 2, ...], // index(es) of column(s) in your data
            line_thickness: 3, // index of a column in your data
            metadata: [4, 5, ...], // index(es) of column(s) in your data
        }
    },
    data: {
        data: [
            [ "DataColumn1Value1", "DataColumn2Value1",
            [ "DataColumn1Value2", "DataColumn2Value2",
            [ "DataColumn1Value3", "DataColumn2Value3",
            ...
        ]
    }
}

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:

{
        data: [
            { "DataHeader1": ..., "DataHeader2": ..., ... },
            { "DataHeader1": ..., "DataHeader2": ..., ... },
            { "DataHeader1": ..., "DataHeader2": ..., ... },
            ...
        ]
    }

... 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: "_555",
    version: "_11",
    bindings: {
        data: {
            name: "DataHeader1",
        }
    },
    data: {
        data: [
            { "DataHeader1": ..., "DataHeader2": ..., ... },
            { "DataHeader1": ..., "DataHeader2": ..., ... },
            { "DataHeader1": ..., "DataHeader2": ..., ... },
            ...
        ]
    }
}

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

{
    template: "_555",
    version: "_11",
    bindings: {
        data: {
            name: "DataHeader1",
            values: ["DataHeader2", "DataHeader3", ...],
            line_thickness: "DataHeader4",
            metadata: ["DataHeader5", "DataHeader6", ...],
        }
    },
    data: {
        data: [
            { "DataHeader1": ..., "DataHeader2": ..., ... },
            { "DataHeader1": ..., "DataHeader2": ..., ... },
            { "DataHeader1": ..., "DataHeader2": ..., ... },
            ...
        ]
    }
}

(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: "_555",
    version: "_11",
    data: {
    data: [
        {
            name: ...,
            values: [...],
            metadata: [...]
        },
        ...
    ]
},
    ...
}

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

{
    template: "_555",
    version: "_11",
    data: {
    data: [
        {
            name: ...,
            values: [...],
            line_thickness: ...,
            metadata: [...]
        },
        ...
    ]
},
    ...
}

Meanings of the template data keys:

  • data.name: The name of the data series (eg. voting district, data mode)
  • data.values: Values for that data series (eg. % of votes). It should be two columns only.
  • data.line_thickness: Values for the thickness of line
  • data.metadata: Makes additional columns (text, emoji, image URLs, etc) avaiable in custom popups

Template settings

Options for opts.state.

Slopes

slope_color color

Default colour.

slope_color_highlight color

Highlight colour.

circle_radius number

Circle radius.

slope_width number

Slope width. Ignored if a line thickness is set for the line via column selection

slope_width_max number

Max slope width.

selected_slopes text

Highlight slopes. To highlight slopes, write the name of each slope on a line. You can also create a a story and highlight/unhighlight by clicking.

value_type string

Visualise.

Allowed values:

  • ranks (Ranks)
  • scores (Scores)

sort_mode string

Sort mode.

Allowed values:

  • descending (Descending)
  • ascending (Ascending)

Title and subtitle

title string

Title.

subtitle string

Subitle.

title_padding number

Margin (pixels).

title_color color

Colour.

Labels

label_font_size number

Font size.

label_font_color color

Font colour.

label_font_highlight_color color

Highlight colour.

label_suffix string

Label suffix. Suffix for labels (eg. %)

labels_position string

Labels position.

Allowed values:

  • left (Left)
  • right (Right)
  • both (Both)

only_show_highlights boolean

Only show highlighted labels.

label_max_width number

Max label width. as % of total width

Max: 100

show_popups boolean

Enable popups.

Text colour.

Font size.

Min: 1

Backgro… colour.

Backgro… opacity.

Max: 1

Custom popup contents.

Popup content. The text to appear in the popup. You can use {{column_name}} to add a value from your data. It must be in a selected column, but you can add columns to “Metadata” if you just want to include them for use in the popup. Advanced used can include HTML to apply layouts, formatting, images, etc.

Height & margins

user_set_height boolean

Set specific height.

user_height number

Height of chart (% of width). fill in 100 for a square chart

margin_top number

Top.

margin_right number

Right.

margin_bottom number

Bottom.

margin_left number

Left.

Y axis

axis_font_size number

Font size.

axis_text_color color

Font colour.

axis_color color

Axis colour.

axis_width number

Axis width.

axis_dashoffset string

Axis dash. eg. 2,2

axis_labels_position string

Position of axis labels.

Allowed values:

  • top (Top)
  • bottom (Bottom)
  • hidden (Hidden)

Number formatting

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

  • . (.)
  • , (,)

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

numbers.prefix string

Prefix. Text to place in front of labels

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

numbers.suffix string

Suffix. Text to place after labels

Animations

animation_duration number

Animation duration. The duration of the animation (in milliseconds) when changing data or mode – for example between slides in a story