Election results bar

A stacked bar chart to show categorical data

Updated 7 years ago by Template retirement home

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

template: _163

version: _10

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: {
        election: [
            [ "ElectionColumn1Value1", "ElectionColumn2Value1",
            [ "ElectionColumn1Value2", "ElectionColumn2Value2",
            [ "ElectionColumn1Value3", "ElectionColumn2Value3",
            ...
        ],
        election_historical: [
            [ "Election_historicalColumn1Value1", "Election_historicalColumn2Value1",
            [ "Election_historicalColumn1Value2", "Election_historicalColumn2Value2",
            [ "Election_historicalColumn1Value3", "Election_historicalColumn2Value3",
            ...
        ]
    }
}

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: "_163",
    version: "_10",
    bindings: {
        election: {
            province: 0, // index of a column in your data
        },
        election_historical: {
            province: 0, // index of a column in your data
        }
    },
    data: {
        election: [
            [ "ElectionColumn1Value1", "ElectionColumn2Value1",
            [ "ElectionColumn1Value2", "ElectionColumn2Value2",
            [ "ElectionColumn1Value3", "ElectionColumn2Value3",
            ...
        ],
        election_historical: [
            [ "Election_historicalColumn1Value1", "Election_historicalColumn2Value1",
            [ "Election_historicalColumn1Value2", "Election_historicalColumn2Value2",
            [ "Election_historicalColumn1Value3", "Election_historicalColumn2Value3",
            ...
        ]
    }
}

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

{
    template: "_163",
    version: "_10",
    bindings: {
        election: {
            province: 0, // index of a column in your data
            votes: [1, 2, ...], // index(es) of column(s) in your data
        },
        election_historical: {
            province: 0, // index of a column in your data
            votes: [1, 2, ...], // index(es) of column(s) in your data
        }
    },
    data: {
        election: [
            [ "ElectionColumn1Value1", "ElectionColumn2Value1",
            [ "ElectionColumn1Value2", "ElectionColumn2Value2",
            [ "ElectionColumn1Value3", "ElectionColumn2Value3",
            ...
        ],
        election_historical: [
            [ "Election_historicalColumn1Value1", "Election_historicalColumn2Value1",
            [ "Election_historicalColumn1Value2", "Election_historicalColumn2Value2",
            [ "Election_historicalColumn1Value3", "Election_historicalColumn2Value3",
            ...
        ]
    }
}

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:

{
        election: [
            { "ElectionHeader1": ..., "ElectionHeader2": ..., ... },
            { "ElectionHeader1": ..., "ElectionHeader2": ..., ... },
            { "ElectionHeader1": ..., "ElectionHeader2": ..., ... },
            ...
        ],
        election_historical: [
            { "Election_historicalHeader1": ..., "Election_historicalHeader2": ..., ... },
            { "Election_historicalHeader1": ..., "Election_historicalHeader2": ..., ... },
            { "Election_historicalHeader1": ..., "Election_historicalHeader2": ..., ... },
            ...
        ]
    }

... 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: "_163",
    version: "_10",
    bindings: {
        election: {
            province: "ElectionHeader1",
        },
        election_historical: {
            province: "Election_historicalHeader1",
        }
    },
    data: {
        election: [
            { "ElectionHeader1": ..., "ElectionHeader2": ..., ... },
            { "ElectionHeader1": ..., "ElectionHeader2": ..., ... },
            { "ElectionHeader1": ..., "ElectionHeader2": ..., ... },
            ...
        ],
        election_historical: [
            { "Election_historicalHeader1": ..., "Election_historicalHeader2": ..., ... },
            { "Election_historicalHeader1": ..., "Election_historicalHeader2": ..., ... },
            { "Election_historicalHeader1": ..., "Election_historicalHeader2": ..., ... },
            ...
        ]
    }
}

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

{
    template: "_163",
    version: "_10",
    bindings: {
        election: {
            province: "ElectionHeader1",
            votes: ["ElectionHeader2", "ElectionHeader3", ...],
        },
        election_historical: {
            province: "Election_historicalHeader1",
            votes: ["Election_historicalHeader2", "Election_historicalHeader3", ...],
        }
    },
    data: {
        election: [
            { "ElectionHeader1": ..., "ElectionHeader2": ..., ... },
            { "ElectionHeader1": ..., "ElectionHeader2": ..., ... },
            { "ElectionHeader1": ..., "ElectionHeader2": ..., ... },
            ...
        ],
        election_historical: [
            { "Election_historicalHeader1": ..., "Election_historicalHeader2": ..., ... },
            { "Election_historicalHeader1": ..., "Election_historicalHeader2": ..., ... },
            { "Election_historicalHeader1": ..., "Election_historicalHeader2": ..., ... },
            ...
        ]
    }
}

(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: "_163",
    version: "_10",
    data: {
    election: [
        {
            province: ...,
            votes: [...]
        },
        ...
    ],
    election_historical: [
        {
            province: ...,
            votes: [...]
        },
        ...
    ]
},
    ...
}

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

{
    template: "_163",
    version: "_10",
    data: {
    election: [
        {
            province: ...,
            votes: [...]
        },
        ...
    ],
    election_historical: [
        {
            province: ...,
            votes: [...]
        },
        ...
    ]
},
    ...
}

Meanings of the template data keys:

  • election.province: province
  • election.votes: votes
  • election_historical.province: province
  • election_historical.votes: votes

Template settings

Options for opts.state.

Margins

margin_top number

Top.

margin_right number

Right.

margin_bottom number

Bottom.

margin_left number

Left.

Colors

custom_colors string

Chart colors.

Labels

label_font_size number

Font size.

label_font_color color

Font color.

label_suffix string

Label suffix.

Middle line

middle_line_content string

Label.

middle_line_position number

Position (%).

Max: 100

middle_line_text_color color

Text color.

middle_line_color color

Line color.

Default location

current_id string

Name of location.

hide_dropdown boolean

Hide dropdown.

Custom text

Dropdown translations.

Text left to dropdown.

instruction_label string

Text above filter bar.

placeholder_text string

Placeholder. Placeholder text if there is no data

subtitle_text string

Text below results.

External data source

external_data string

URL to svg.