US counties choropleth map

Updated 7 years ago by Duncan Clark

How to use this template

US counties choropleth template

To do

Fix legend:

  • Add option to make bucketing scale boxes proportional to bucket size;
  • Optional: add marker to show position along scale when hovering over a county. (Implemented for continuous scale).

After this I think we have something usable so we could look at how to generate different templates for different maps.

Other features (in no particular order) not currently implemented:

  • Switching between multiple datasets (including how to cope with different value scales, units, etc and navigation).
  • Proper support for diverging colour scales (not just a colour option). Could add option for boxplot-style scaling (ie low outlier, low, middle, high, high outlier).
  • Categorical scales.
  • Custom colours. Useful for categorical scales, tricky/less useful (?) for other scales.
  • Optional: hover/click on colour scale to highlight associated areas (or hide other regions).

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

template: _111

version: _1

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: "_111",
    version: "_1",
    bindings: {
        data: {
            id: 0, // index of a column in your data
            name: 1, // index of a column in your data
            value: 2, // 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: "_111",
    version: "_1",
    bindings: {
        data: {
            id: 0, // index of a column in your data
            name: 1, // index of a column in your data
            value: 2, // index of a column 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: "_111",
    version: "_1",
    bindings: {
        data: {
            id: "DataHeader1",
            name: "DataHeader2",
            value: "DataHeader3",
        }
    },
    data: {
        data: [
            { "DataHeader1": ..., "DataHeader2": ..., ... },
            { "DataHeader1": ..., "DataHeader2": ..., ... },
            { "DataHeader1": ..., "DataHeader2": ..., ... },
            ...
        ]
    }
}

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

{
    template: "_111",
    version: "_1",
    bindings: {
        data: {
            id: "DataHeader1",
            name: "DataHeader2",
            value: "DataHeader3",
        }
    },
    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: "_111",
    version: "_1",
    data: {
    data: [
        {
            id: ...,
            name: ...,
            value: ...
        },
        ...
    ]
},
    ...
}

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

{
    template: "_111",
    version: "_1",
    data: {
    data: [
        {
            id: ...,
            name: ...,
            value: ...
        },
        ...
    ]
},
    ...
}

Meanings of the template data keys:

  • data.id: The column containing the numerical id of the county
  • data.name: The column containing the name of the county
  • data.value: A column of values

Template settings

Options for opts.state.

Title

title string

Title.

Colour scale

palette string

Palette. Specify a d3 sequential scheme (such as [[schemeOranges]], [[schemeReds]], [[schemeBlues]] and [[schemeGreens]]) or diverging scheme (such as [[schemeRdYlGn]] or [[schemePiYG]]). See github.com/d3/d3-scale-chromatic for a full list.

reverse_scale_colours boolean

Reverse colours.

scale_min string

Colour-scale minimum value. Any values falling below this take on the colour at the bottom of the scale.

scale_max string

Colour-scale maximum value. Any values falling above this take on the colour at the top of the scale.

bucket boolean

Bucket scale.

n_buckets number

Number of buckets. From 3 to 9; ignored if using custom thresholds

Min: 3

Max: 9

custom_thresholds string

Intermediate thresholds. A comma-separated list of (up to 6) additional thresholds. Will only be used if one or more entries in the list falls between Colour-scale minimum and Colour-scale maximum, in which case Number of buckets option will be ignored

no_data_fill color

Missing data colour.

hatch boolean

Show missing data as pattern.

Number styles

prefix string

Number prefix. Optional prefix for numbers; useful for currency symbols

suffix string

Number suffix. Optional suffix for numbers; useful for units

multiplier number

Number multiplier. Optional multiplier for numbers

decimals number

Number of decimal places. Decimal places for numbers. Negative values round to positive powers of 10 (eg -2 rounds to nearest 100)

Legend

legend_text_size number

Legend text size.

legend_width number

Legend width.

State borders

state_border_color color

Colour.

state_border_opacity number

Opacity.

Max: 1

state_border_width number

Width.