Component

Chart (experimental)

The chart component presents a data chart and a tabular version of the same data

This component takes a set of data and presents it as a line graph and a table with one or more rows and lines. The chart relies upon chartkick and renders using JavaScript, so the table is provided as a fallback for a lack of JavaScript, an accessible view on the data for screenreaders, and a simple view of the raw data for all users.

The chart_overview option can be used to provide an explanation for screen reader users of what the chart shows.

Search for usage of this component on GitHub.

How it looks (preview) (preview all)

Chart showing page views on the website in January 2015

This chart shows page views for January 2015.
This chart is a visual representation of the data available in the table.
Skip to "Chart showing page views on the website in January 2015" data table
View data as a table
Data table for "Chart showing page views on the website in January 2015"
2015-01-01 2015-01-02 2015-01-03 2015-01-04 2015-01-05 2015-01-06 2015-01-07 2015-01-08 2015-01-09 2015-01-10
January 2015 500 1,190 740 820 270 450 110 210 670 430

How to call this component

<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Chart showing page views on the website in January 2015",
  h_axis_title: "Day",
  v_axis_title: "Views",
  chart_overview: "This chart shows page views for January 2015.",
  keys: [
    "2015-01-01",
    "2015-01-02",
    "2015-01-03",
    "2015-01-04",
    "2015-01-05",
    "2015-01-06",
    "2015-01-07",
    "2015-01-08",
    "2015-01-09",
    "2015-01-10"
  ],
  rows: [
    {
      label: "January 2015",
      values: [
        500,
        1190,
        740,
        820,
        270,
        450,
        110,
        210,
        670,
        430
      ]
    }
  ]
} %>

Accessibility acceptance criteria

Charts must:

  • use line colours with a contrast ratio higher than 4.5:1 against the background colour to meet WCAG AA
  • contain a chart_heading option if minimal is not set to true, so that skip links and visually hidden headings are accurate

Links in the component must:

  • accept focus
  • be focusable with a keyboard
  • be usable with a keyboard
  • indicate when they have focus
  • change in appearance when touched (in the touch-down state)
  • change in appearance when hovered
  • be usable with touch
  • be usable with voice commands
  • have visible text
  • have meaningful text

Other examples

Standard options

This component uses the component wrapper helper. It accepts the following options and applies them to the parent element of the component. See the component wrapper helper documentation for more detail.

  • id - accepts a string for the element ID attribute
  • data_attributes - accepts a hash of data attributes
  • aria - accepts a hash of aria attributes
  • classes - accepts a space separated string of classes, these should not be used for styling and must be prefixed with js-
  • margin_bottom - accepts a number from 0 to 9 (0px to 60px) using the GOV.UK Frontend spacing scale (defaults to no margin)
  • role - accepts a space separated string of roles
  • lang - accepts a language attribute value
  • open - accepts an open attribute value (true or false)
  • hidden - accepts an empty string, ‘hidden’, or ‘until-found’
  • tabindex - accepts an integer. The integer can also be passed as a string.
  • dir - accepts ‘rtl’, ‘ltr’, or ‘auto’.

Multiple rows of data (preview)

Where more than one series is shown on a chart, do not hide the legend.

Page views chart

This chart shows page views for January in different years.
This chart is a visual representation of the data available in the table.
Skip to "Page views chart" data table
View data as a table
Data table for "Page views chart"
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
January 2015 5 119 74 82 27 45 11 21 67 43
January 2016 5 8 37 50 43 29 67 61 14 91
January 2017 31 81 12 15 52 61 143 27 18 34
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Page views chart",
  h_axis_title: "Day",
  v_axis_title: "Views",
  chart_overview: "This chart shows page views for January in different years.",
  keys: [
    "1st",
    "2nd",
    "3rd",
    "4th",
    "5th",
    "6th",
    "7th",
    "8th",
    "9th",
    "10th"
  ],
  rows: [
    {
      label: "January 2015",
      values: [
        5,
        119,
        74,
        82,
        27,
        45,
        11,
        21,
        67,
        43
      ]
    },
    {
      label: "January 2016",
      values: [
        5,
        8,
        37,
        50,
        43,
        29,
        67,
        61,
        14,
        91
      ]
    },
    {
      label: "January 2017",
      values: [
        31,
        81,
        12,
        15,
        52,
        61,
        143,
        27,
        18,
        34
      ]
    }
  ]
} %>

Hide legend (preview)

Page views chart

This chart shows page views for January 2015.
This chart is a visual representation of the data available in the table.
Skip to "Page views chart" data table
View data as a table
Data table for "Page views chart"
2015-01-01 2015-01-02 2015-01-03 2015-01-04 2015-01-05 2015-01-06 2015-01-07 2015-01-08 2015-01-09 2015-01-10
January 2015 500 1,190 740 820 270 450 110 210 670 430
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Page views chart",
  h_axis_title: "Day",
  v_axis_title: "Views",
  chart_overview: "This chart shows page views for January 2015.",
  hide_legend: true,
  keys: [
    "2015-01-01",
    "2015-01-02",
    "2015-01-03",
    "2015-01-04",
    "2015-01-05",
    "2015-01-06",
    "2015-01-07",
    "2015-01-08",
    "2015-01-09",
    "2015-01-10"
  ],
  rows: [
    {
      label: "January 2015",
      values: [
        500,
        1190,
        740,
        820,
        270,
        450,
        110,
        210,
        670,
        430
      ]
    }
  ]
} %>

With line colours and styles (preview)

Note that line styles and colours currently only work on line charts.

Page views chart

This chart shows page views for January in different years.
This chart is a visual representation of the data available in the table.
Skip to "Page views chart" data table
View data as a table
Data table for "Page views chart"
1st 2nd 3rd
January 2015 110 119 105
January 2016 71 68 75
January 2017 21 42 18
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Page views chart",
  h_axis_title: "Day",
  v_axis_title: "Views",
  chart_overview: "This chart shows page views for January in different years.",
  line_colours: [
    "orange",
    "blue",
    "#88AA11"
  ],
  line_styles: [
    "solid",
    "dotted",
    "solid"
  ],
  keys: [
    "1st",
    "2nd",
    "3rd"
  ],
  rows: [
    {
      label: "January 2015",
      values: [
        110,
        119,
        105
      ]
    },
    {
      label: "January 2016",
      values: [
        71,
        68,
        75
      ]
    },
    {
      label: "January 2017",
      values: [
        21,
        42,
        18
      ]
    }
  ]
} %>

Bar chart (preview)

Hats owned

This chart shows page views for January 2015.
This chart is a visual representation of the data available in the table.
Skip to "Hats owned" data table
View data as a table
Data table for "Hats owned"
Jon Ash James Phil
Hats owned 3 1 1 2
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Hats owned",
  h_axis_title: "Day",
  v_axis_title: "Views",
  chart_overview: "This chart shows page views for January 2015.",
  chart_type: "bar",
  hide_legend: true,
  keys: [
    "Jon",
    "Ash",
    "James",
    "Phil"
  ],
  rows: [
    {
      label: "Hats owned",
      values: [
        3,
        1,
        1,
        2
      ]
    }
  ]
} %>

Column chart (preview)

Hats owned

This chart shows page views for January 2015.
This chart is a visual representation of the data available in the table.
Skip to "Hats owned" data table
View data as a table
Data table for "Hats owned"
Jon Ash James Phil
Hats owned 3 1 1 2
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Hats owned",
  h_axis_title: "Day",
  v_axis_title: "Views",
  chart_overview: "This chart shows page views for January 2015.",
  chart_type: "column",
  hide_legend: true,
  keys: [
    "Jon",
    "Ash",
    "James",
    "Phil"
  ],
  rows: [
    {
      label: "Hats owned",
      values: [
        3,
        1,
        1,
        2
      ]
    }
  ]
} %>

Format axis labels (preview)

Axis labels can be forced into date or currency format as shown.

‘currency’ will add a currency symbol (£) at the start of the labels.

‘percent’ will add a percent (%) to the end of the labels.

‘date’ will force a standard date format of YYYY-MM-DD to the labels, where they are in an undesirable date format. This can be useful if there are a lot of labels on the axes because the chart can do strange truncation sometimes.

Cost per day

This chart shows cost per day.
This chart is a visual representation of the data available in the table.
Skip to "Cost per day" data table
View data as a table
Data table for "Cost per day"
2024 Jan 1 2024 Jan 2 2024 Jan 3 2024 Jan 4 2024 Jan 5 2024 Jan 6
Cost 14 29 45 63 54 22
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Cost per day",
  h_axis_title: "Day",
  v_axis_title: "Cost",
  chart_overview: "This chart shows cost per day.",
  v_axis_format: "currency",
  h_axis_format: "date",
  keys: [
    "2024 Jan 1",
    "2024 Jan 2",
    "2024 Jan 3",
    "2024 Jan 4",
    "2024 Jan 5",
    "2024 Jan 6"
  ],
  rows: [
    {
      label: "Cost",
      values: [
        14,
        29,
        45,
        63,
        54,
        22
      ]
    }
  ]
} %>

Vertical table (preview)

Reorient the table to better suit the output of some data sets.

Page views chart

This chart shows page views for January in different years.
This chart is a visual representation of the data available in the table.
Skip to "Page views chart" data table
View data as a table
Data table for "Page views chart"
January 2015 January 2016
1st 5 3
2nd 119 8
3rd 74 37
4th 117 82
5th 33 118
6th 89 85
7th 79 80
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Page views chart",
  table_direction: "vertical",
  h_axis_title: "Day",
  v_axis_title: "Views",
  chart_overview: "This chart shows page views for January in different years.",
  keys: [
    "1st",
    "2nd",
    "3rd",
    "4th",
    "5th",
    "6th",
    "7th"
  ],
  rows: [
    {
      label: "January 2015",
      values: [
        5,
        119,
        74,
        117,
        33,
        89,
        79
      ]
    },
    {
      label: "January 2016",
      values: [
        3,
        8,
        37,
        82,
        118,
        85,
        80
      ]
    }
  ]
} %>

With a different point size (preview)

For some charts it may be better to reduce the point size so that the line is clearer. However it is recommended that the point size is larger to make it easier for users to move the mouse over them.

Page views chart

This chart shows page views for January 2015.
This chart is a visual representation of the data available in the table.
Skip to "Page views chart" data table
View data as a table
Data table for "Page views chart"
2015-01-01 2015-01-02 2015-01-03 2015-01-04 2015-01-05 2015-01-06 2015-01-07 2015-01-08 2015-01-09 2015-01-10
January 2015 600 690 740 820 570 950 810 610 770 530
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Page views chart",
  h_axis_title: "Day",
  v_axis_title: "Views",
  chart_overview: "This chart shows page views for January 2015.",
  point_size: 0,
  keys: [
    "2015-01-01",
    "2015-01-02",
    "2015-01-03",
    "2015-01-04",
    "2015-01-05",
    "2015-01-06",
    "2015-01-07",
    "2015-01-08",
    "2015-01-09",
    "2015-01-10"
  ],
  rows: [
    {
      label: "January 2015",
      values: [
        600,
        690,
        740,
        820,
        570,
        950,
        810,
        610,
        770,
        530
      ]
    }
  ]
} %>

Scale y axis based on data (preview)

By default the Y-axis will always start at zero, but this may not always be required.

If there are empty items in the data the chart line will either discontinue or leave a gap, as shown.

Page views chart

This chart shows page views for January 2015.
This chart is a visual representation of the data available in the table.
Skip to "Page views chart" data table
View data as a table
Data table for "Page views chart"
2015-01-01 2015-01-02 2015-01-03 2015-01-04 2015-01-05 2015-01-06 2015-01-07 2015-01-08 2015-01-09 2015-01-10 2015-01-11 2015-01-12 2015-01-13
January 2015 1,500 1,190 1,740 1,820 1,270 1,450 1,110 1,210 1,670 1,430 1,352
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Page views chart",
  h_axis_title: "Day",
  v_axis_title: "Views",
  chart_overview: "This chart shows page views for January 2015.",
  hide_legend: true,
  y_axis_auto_adjust: true,
  keys: [
    "2015-01-01",
    "2015-01-02",
    "2015-01-03",
    "2015-01-04",
    "2015-01-05",
    "2015-01-06",
    "2015-01-07",
    "2015-01-08",
    "2015-01-09",
    "2015-01-10",
    "2015-01-11",
    "2015-01-12",
    "2015-01-13"
  ],
  rows: [
    {
      label: "January 2015",
      values: [
        1500,
        1190,
        1740,
        1820,
        1270,
        null,
        null,
        1450,
        1110,
        1210,
        1670,
        1430,
        1352
      ]
    }
  ]
} %>

With a different heading level (preview)

Use a different heading level for the chart heading. It defaults to a H2.

Page views chart

This chart shows page views for January in different years.
This chart is a visual representation of the data available in the table.
Skip to "Page views chart" data table
View data as a table
Data table for "Page views chart"
1st 2nd 3rd 4th 5th 6th 7th
January 2015 5 119 74 117 33 89 79
January 2016 3 8 37 82 118 85 80
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Page views chart",
  chart_heading_level: 4,
  h_axis_title: "Day",
  v_axis_title: "Views",
  chart_overview: "This chart shows page views for January in different years.",
  keys: [
    "1st",
    "2nd",
    "3rd",
    "4th",
    "5th",
    "6th",
    "7th"
  ],
  rows: [
    {
      label: "January 2015",
      values: [
        5,
        119,
        74,
        117,
        33,
        89,
        79
      ]
    },
    {
      label: "January 2016",
      values: [
        3,
        8,
        37,
        82,
        118,
        85,
        80
      ]
    }
  ]
} %>

With margin bottom (preview)

The component accepts a number for margin bottom from 0 to 9 (0px to 60px) using the GOV.UK Frontend spacing scale. It defaults to having a bottom margin of 15px.

Page views chart

This chart shows page views for January in different years.
This chart is a visual representation of the data available in the table.
Skip to "Page views chart" data table
View data as a table
Data table for "Page views chart"
1st 2nd 3rd 4th 5th 6th 7th
January 2015 5 119 74 117 33 89 79
January 2016 3 8 37 82 118 85 80
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Page views chart",
  h_axis_title: "Day",
  v_axis_title: "Views",
  margin_bottom: 9,
  chart_overview: "This chart shows page views for January in different years.",
  keys: [
    "1st",
    "2nd",
    "3rd",
    "4th",
    "5th",
    "6th",
    "7th"
  ],
  rows: [
    {
      label: "January 2015",
      values: [
        5,
        119,
        74,
        117,
        33,
        89,
        79
      ]
    },
    {
      label: "January 2016",
      values: [
        3,
        8,
        37,
        82,
        118,
        85,
        80
      ]
    }
  ]
} %>

Minimal version (preview)

The minimal version presents a simplified version of the chart. This should only be used where there is not enough space to display a full chart, and the user is then given an option to see more information about the chart, for example by including a link next to the chart component. This must be part of the page, as this is not provided by the component itself.

Specifically, minimal mode:

  • hides the chart heading
  • removes the legend and X and Y axis values
  • removes the data table and link to the data (if supplied) beneath the chart
  • removes the part of the visually hidden accessibility message that links to the table
  • removes the ability to interact with the chart (no hover popups or clicks)
  • sets the size of the points on the chart to zero
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Page views",
  h_axis_title: "Day",
  v_axis_title: "Views",
  minimal: true,
  chart_overview: "This is a graph of views per day",
  keys: [
    "1st",
    "2nd",
    "3rd",
    "4th",
    "5th",
    "6th",
    "7th"
  ],
  rows: [
    {
      label: "January 2015",
      values: [
        5,
        119,
        74,
        117,
        33,
        89,
        79
      ]
    },
    {
      label: "January 2016",
      values: [
        3,
        8,
        37,
        82,
        118,
        85,
        80
      ]
    }
  ]
} %>

With a different height (preview)

Sets a height in pixels for the chart, defaulting to 400. This is useful where the chart might appear in a narrow column and the default height would be too tall.

The component is not currently responsive.

<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Page views",
  h_axis_title: "Day",
  v_axis_title: "Views",
  minimal: true,
  chart_overview: "This is a graph of views per day",
  height: 200,
  keys: [
    "1st",
    "2nd",
    "3rd",
    "4th",
    "5th",
    "6th",
    "7th"
  ],
  rows: [
    {
      label: "January 2015",
      values: [
        5,
        119,
        74,
        117,
        33,
        89,
        79
      ]
    },
    {
      label: "January 2016",
      values: [
        3,
        8,
        37,
        82,
        118,
        85,
        80
      ]
    }
  ]
} %>

With hidden heading (preview)

The heading element is optional, allowing a heading to be set outside the component. Hiding the heading doesn’t remove the need to populate chart_heading with text, as chart_heading is needed for accessibility.

This is a graph of views per day
This chart is a visual representation of the data available in the table.
Skip to "Page views" data table
View data as a table
Data table for "Page views"
1st 2nd 3rd 4th 5th 6th 7th
January 2015 5 119 74 117 33 89 79
January 2016 3 8 37 82 118 85 80
<%= render "govuk_publishing_components/components/chart", {
  chart_heading: "Page views",
  hide_heading: true,
  h_axis_title: "Day",
  v_axis_title: "Views",
  chart_overview: "This is a graph of views per day",
  keys: [
    "1st",
    "2nd",
    "3rd",
    "4th",
    "5th",
    "6th",
    "7th"
  ],
  rows: [
    {
      label: "January 2015",
      values: [
        5,
        119,
        74,
        117,
        33,
        89,
        79
      ]
    },
    {
      label: "January 2016",
      values: [
        3,
        8,
        37,
        82,
        118,
        85,
        80
      ]
    }
  ]
} %>