Slope chart

A simple slope chart for comparing two measures across a dataset

Updated 7 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: _184

version: _33

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: {
        lines: [
            [ "LinesColumn1Value1", "LinesColumn2Value1",
            [ "LinesColumn1Value2", "LinesColumn2Value2",
            [ "LinesColumn1Value3", "LinesColumn2Value3",
            ...
        ]
    }
}

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: "_184",
    version: "_33",
    bindings: {
        lines: {
            name: 0, // index of a column in your data
        }
    },
    data: {
        lines: [
            [ "LinesColumn1Value1", "LinesColumn2Value1",
            [ "LinesColumn1Value2", "LinesColumn2Value2",
            [ "LinesColumn1Value3", "LinesColumn2Value3",
            ...
        ]
    }
}

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

{
    template: "_184",
    version: "_33",
    bindings: {
        lines: {
            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
            group: 4, // index of a column in your data
            metadata: [5, 6, ...], // index(es) of column(s) in your data
        }
    },
    data: {
        lines: [
            [ "LinesColumn1Value1", "LinesColumn2Value1",
            [ "LinesColumn1Value2", "LinesColumn2Value2",
            [ "LinesColumn1Value3", "LinesColumn2Value3",
            ...
        ]
    }
}

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:

{
        lines: [
            { "LinesHeader1": ..., "LinesHeader2": ..., ... },
            { "LinesHeader1": ..., "LinesHeader2": ..., ... },
            { "LinesHeader1": ..., "LinesHeader2": ..., ... },
            ...
        ]
    }

... 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: "_184",
    version: "_33",
    bindings: {
        lines: {
            name: "LinesHeader1",
        }
    },
    data: {
        lines: [
            { "LinesHeader1": ..., "LinesHeader2": ..., ... },
            { "LinesHeader1": ..., "LinesHeader2": ..., ... },
            { "LinesHeader1": ..., "LinesHeader2": ..., ... },
            ...
        ]
    }
}

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

{
    template: "_184",
    version: "_33",
    bindings: {
        lines: {
            name: "LinesHeader1",
            values: ["LinesHeader2", "LinesHeader3", ...],
            line_thickness: "LinesHeader4",
            group: "LinesHeader5",
            metadata: ["LinesHeader6", "LinesHeader7", ...],
        }
    },
    data: {
        lines: [
            { "LinesHeader1": ..., "LinesHeader2": ..., ... },
            { "LinesHeader1": ..., "LinesHeader2": ..., ... },
            { "LinesHeader1": ..., "LinesHeader2": ..., ... },
            ...
        ]
    }
}

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

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

{
    template: "_184",
    version: "_33",
    data: {
    lines: [
        {
            name: ...,
            values: [...],
            line_thickness: ...,
            group: ...,
            metadata: [...]
        },
        ...
    ]
},
    ...
}

Meanings of the template data keys:

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

Template settings

Options for opts.state.

Slopes

slope_color color

Default color.

slope_color_highlight color

Highlight color.

circle_radius number

Circle radius.

slope_width number

Stroke width.

slope_width_max number

Max stroke width. This is only relevant if you've added data to set the thickness of the slope

selected_slope text

Highlight slopes. To highlight slopes, write the name of each slope separated by comma. 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 color.

label_font_highlight_color color

Highlight color.

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

axis_color color

Axis color.

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)