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.
How it looks (preview) (preview all)
Page views chart
How to call this component
<%= 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
]
}
]
} %>
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 ifminimal
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 attributedata_attributes
- accepts a hash of data attributesaria
- accepts a hash of aria attributesclasses
- accepts a space separated string of classes, these should not be used for styling and must be prefixed withjs-
role
- accepts a space separated string of roleslang
- accepts a language attribute valueopen
- 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
<%= 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
]
}
]
} %>
Vertical table (preview)
Reorient the table to better suit the output of some data sets.
Page views chart
<%= 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
<%= 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
<%= 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
<%= 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 a download link (preview)
Add a download link to the original data. Doesn’t do anything clever, just creates a link to the URL passed.
Page views chart
<%= render "govuk_publishing_components/components/chart", {
chart_heading: "Page views chart",
h_axis_title: "Day",
v_axis_title: "Views",
link: "https://www.gov.uk",
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 padding (preview)
Moves the heading and items beneath the chart inwards. Useful where the chart is contained in an element where these items would otherwise touch the sides.
Page views
<%= render "govuk_publishing_components/components/chart", {
chart_heading: "Page views",
h_axis_title: "Day",
v_axis_title: "Views",
padding: true,
link: "https://www.gov.uk",
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 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
<%= 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
]
}
]
} %>