Election results chart

A stacked bar chart ideal for election results. Includes a coalition builder feature

Updated 2 years ago by Template retirement home

How to use this template

Election results chart

This template is ideal for visualising election results. You can show one overall result, or specific regions that can be selected from a dropdown.

Features

  • Toggle parties in the legend to create custom "coalitions"
  • Add historical data to compare the current results to a previous election

Data requirements

Each row after the header should be a region (eg. a state, constituency or just "nationwide"). There should be a column for the name of the region, and a column for each party in the election. In the historical data sheet you can add data for a previous election. This sheet follows the same format as the current results, make sure to use exactly the same region and party names, as this is being used to compare the results.

Tips

  • You can specify custom colors to represent the right parties in the "Chart colors" box
  • It's possible to translate the labels in this chart to your own language in the "Text and translation" box
  • You can change the default region to show in "Dropdown" box, make sure your value is case sensitive
  • You can also show other values than %, in "Label styles" and "Custom scale" boxes you can change the scale and the suffix

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

template: _171

version: _11

Template data

There are two different formats in which you can supply data to this template. Which one will be more convenient for you likely depends on the source of your data, as described below.

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:

    {
        values_now: [
            { "Values_nowHeader1": ..., "Values_nowHeader2": ..., ... },
            { "Values_nowHeader1": ..., "Values_nowHeader2": ..., ... },
            { "Values_nowHeader1": ..., "Values_nowHeader2": ..., ... },
            ...
        ],
        values_historical: [
            { "Values_historicalHeader1": ..., "Values_historicalHeader2": ..., ... },
            { "Values_historicalHeader1": ..., "Values_historicalHeader2": ..., ... },
            { "Values_historicalHeader1": ..., "Values_historicalHeader2": ..., ... },
            ...
        ]
    }

... but with the keys being the column headers from your source data instead. To tell the API how these 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: "_171",
    version: "_11",
    bindings: {
        values_now: {
            name: "Values_nowHeader1"
        },
        values_historical: {
            name: "Values_historicalHeader1"
        }
    },
    data: {
        values_now: [
            { "Values_nowHeader1": ..., "Values_nowHeader2": ..., ... },
            { "Values_nowHeader1": ..., "Values_nowHeader2": ..., ... },
            { "Values_nowHeader1": ..., "Values_nowHeader2": ..., ... },
            ...
        ],
        values_historical: [
            { "Values_historicalHeader1": ..., "Values_historicalHeader2": ..., ... },
            { "Values_historicalHeader1": ..., "Values_historicalHeader2": ..., ... },
            { "Values_historicalHeader1": ..., "Values_historicalHeader2": ..., ... },
            ...
        ]
    }
}

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

{
    template: "_171",
    version: "_11",
    bindings: {
        values_now: {
            name: "Values_nowHeader1",
            values: ["Values_nowHeader2", "Values_nowHeader3", ...]
        },
        values_historical: {
            name: "Values_historicalHeader1",
            values: ["Values_historicalHeader2", "Values_historicalHeader3", ...]
        }
    },
    data: {
        values_now: [
            { "Values_nowHeader1": ..., "Values_nowHeader2": ..., ... },
            { "Values_nowHeader1": ..., "Values_nowHeader2": ..., ... },
            { "Values_nowHeader1": ..., "Values_nowHeader2": ..., ... },
            ...
        ],
        values_historical: [
            { "Values_historicalHeader1": ..., "Values_historicalHeader2": ..., ... },
            { "Values_historicalHeader1": ..., "Values_historicalHeader2": ..., ... },
            { "Values_historicalHeader1": ..., "Values_historicalHeader2": ..., ... },
            ...
        ]
    }
}

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

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: "_171",
version: "_11",
data: {
    values_now: [
        {
            name: ...,
            values: [...]
        },
        ...
    ],
    values_historical: [
        {
            name: ...,
            values: [...]
        },
        ...
    ]
},
...
}

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

{
template: "_171",
version: "_11",
data: {
    values_now: [
        {
            name: ...,
            values: [...]
        },
        ...
    ],
    values_historical: [
        {
            name: ...,
            values: [...]
        },
        ...
    ]
},
...
}

Meanings of the template data keys:

  • values_now.name: The name of the data series (eg. voting district, data mode)
  • values_now.values: One column per party, with party names as header rows.
  • values_historical.name: The name of the data series (eg. voting district, data mode)
  • values_historical.values: One column per party, with party names as header rows.

Template settings

Options for opts.state.

Chart colors

palette string

Color palette.

Allowed values:

  • flourish_default_1 (Flourish categories A)
  • flourish_default_2 (Flourish categories B)
  • carto_pastel (Pastel light)
  • carto_antique (Pastel dark)
  • carto_rainbow (Rainbow)
  • schemeCategory10 (Category 10)
  • schemeCategory20 (Category 20)
  • schemeCategory20b (Category 20b)
  • schemeCategory20c (Category 20c)
  • schemeAccent (Accent)
  • schemeDark2 (Dark 2)
  • schemePaired (Paired)
  • schemePastel1 (Pastel 1)
  • schemePastel2 (Pastel 2)

custom_colors text

Custom Colors. This will overwrite the palette. Write the colors as hex color. Separate the colors by comma. (eg. '#333333,#F15252,#72B846')

Label styles

label_font_size number

Font size.

label_font_color color

Font color.

label_suffix string

Label suffix.

Text and translation

Dropdown translations. Write every translation a new line in this format original_word:translation

Text left of dropdown.

instruction_label string

Instruction text above legend.

placeholder_text string

Placeholder. Placeholder text if there is no data

subtitle_text string

Text below results.

hide_dropdown boolean

Hide dropdown.

current_id string

Default value of dropdown.

hide_empty_rows boolean

Hide empty rows in dropdown.

Margins

margin_top number

Top.

margin_right number

Right.

margin_bottom number

Bottom.

margin_left number

Left.

Custom scale

use_custom_scale boolean

Use custom maximum value for scale. On default, will use the sum of all data points as the max value. Enable this setting to use a custom value

custom_scale_max number

Maximum value for scale. This will set the maximum value of the scale. Eg. the total amount of seats to be won in an election

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.

middle_line_width number

Middle line width.

middle_line_dashoffset string

Middle line dash. eg. 2,2