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 overview option can be used to provide an explanation for screen reader users of what the graph shows.

Search for usage of this component on GitHub.

How it looks (preview) (preview all)

Page views chart

This chart is a visual representation of the data available in the table.
Data table
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: "Page views chart",
  h_axis_title: "Day",
  v_axis_title: "Views",
  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
      ]
    }
  ]
} %>

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

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-
  • 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’

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 is a visual representation of the data available in the table.
Data table
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",
  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
      ]
    }
  ]
} %>

Vertical table (preview)

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

Page views chart

This chart is a visual representation of the data available in the table.
Data table
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",
  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 heading level (preview)

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

Page views chart

This chart is a visual representation of the data available in the table.
Data table
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",
  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 is a visual representation of the data available in the table.
Data table
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,
  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
      ]
    }
  ]
} %>