Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EES-5759 handle empty charts #5603

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const CustomTooltip = ({
{tooltipLabel}
</p>

{!!payload.length && (
{payload && !!payload.length && (
<ul className={styles.itemList} data-testid="chartTooltip-items">
{getPayloadOrder()?.map((item, index) => {
const dataKey =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ const HorizontalBarBlock = ({
const chartHasNegativeValues =
(parseNumber(minorDomainTicks.domain?.[0]) ?? 0) < 0;

if (!chartData.length) {
return <p className="govuk-!-margin-top-5">No data to display.</p>;
}

return (
<ChartContainer
height={height || 300}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ const LineChartBlock = ({
const chartHasNegativeValues =
(parseNumber(minorDomainTicks.domain?.[0]) ?? 0) < 0;

if (!chartData.length) {
return <p className="govuk-!-margin-top-5">No data to display.</p>;
}

return (
<ChartContainer
height={height || 300}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ const VerticalBarBlock = ({
resizeTicks(containerWidth);
}, 300);

if (!chartData.length) {
return <p className="govuk-!-margin-top-5">No data to display.</p>;
}

return (
<ChartContainer
height={height || 300}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
testChartConfiguration,
testChartTableData,
testEmptyChartConfiguration,
testEmptyChartTableData,
} from '@common/modules/charts/components/__tests__/__data__/testChartData';
import { expectTicks } from '@common/modules/charts/components/__tests__/testUtils';
import HorizontalBarBlock from '@common/modules/charts/components/HorizontalBarBlock';
Expand Down Expand Up @@ -65,6 +67,27 @@ describe('HorizontalBarBlock', () => {
});
});

test('does not render the chart if there is no chart data', async () => {
const emptyFullTable = mapFullTable(testEmptyChartTableData);
const emptyChartProps: VerticalBarProps = {
...testEmptyChartConfiguration,
legend: testEmptyChartConfiguration.legend as LegendConfiguration,
axes: testEmptyChartConfiguration.axes as VerticalBarProps['axes'],
meta: emptyFullTable.subjectMeta,
data: emptyFullTable.results,
dataLabelPosition: 'inside',
};
const { container } = render(<HorizontalBarBlock {...emptyChartProps} />);

await waitFor(() => {
expect(screen.getByText('No data to display.')).toBeInTheDocument();
});

expect(
container.querySelector('.recharts-wrapper'),
).not.toBeInTheDocument();
});

test('major axis can be hidden', () => {
const { container } = render(
<HorizontalBarBlock
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
testChartConfiguration,
testChartTableData,
testEmptyChartConfiguration,
testEmptyChartTableData,
} from '@common/modules/charts/components/__tests__/__data__/testChartData';
import { expectTicks } from '@common/modules/charts/components/__tests__/testUtils';
import LineChartBlock, {
Expand Down Expand Up @@ -64,6 +66,27 @@ describe('LineChartBlock', () => {
});
});

test('does not render the chart if there is no chart data', async () => {
const emptyFullTable = mapFullTable(testEmptyChartTableData);
const emptyChartProps: LineChartProps = {
...testEmptyChartConfiguration,
legend: testEmptyChartConfiguration.legend as LegendConfiguration,
axes: testEmptyChartConfiguration.axes as LineChartProps['axes'],
meta: emptyFullTable.subjectMeta,
data: emptyFullTable.results,
dataLabelPosition: 'above',
};
const { container } = render(<LineChartBlock {...emptyChartProps} />);

await waitFor(() => {
expect(screen.getByText('No data to display.')).toBeInTheDocument();
});

expect(
container.querySelector('.recharts-wrapper'),
).not.toBeInTheDocument();
});

test('major axis can be hidden', () => {
const { container } = render(
<LineChartBlock
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
testChartConfiguration,
testChartTableData,
testEmptyChartConfiguration,
testEmptyChartTableData,
} from '@common/modules/charts/components/__tests__/__data__/testChartData';
import { expectTicks } from '@common/modules/charts/components/__tests__/testUtils';
import VerticalBarBlock, {
Expand Down Expand Up @@ -66,6 +68,27 @@ describe('VerticalBarBlock', () => {
});
});

test('does not render the chart if there is no chart data', async () => {
const emptyFullTable = mapFullTable(testEmptyChartTableData);
const emptyChartProps: VerticalBarProps = {
...testEmptyChartConfiguration,
legend: testEmptyChartConfiguration.legend as LegendConfiguration,
axes: testEmptyChartConfiguration.axes as VerticalBarProps['axes'],
meta: emptyFullTable.subjectMeta,
data: emptyFullTable.results,
dataLabelPosition: 'outside',
};
const { container } = render(<VerticalBarBlock {...emptyChartProps} />);

await waitFor(() => {
expect(screen.getByText('No data to display.')).toBeInTheDocument();
});

expect(
container.querySelector('.recharts-wrapper'),
).not.toBeInTheDocument();
});

test('major axis can be hidden', () => {
const { container } = render(
<VerticalBarBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,113 @@ export const testChartTableData: TableDataResponse = {
},
],
};

export const testEmptyChartConfiguration: Chart = {
legend: {
position: 'top',
items: [
{
dataSet: {
indicator: 'indicator-1-id',
filters: [],
},
label: 'Indicator 1',
colour: '#4763a5',
},
],
},
axes: {
major: {
type: 'major',
groupBy: 'timePeriod',
sortAsc: true,
referenceLines: [],
dataSets: [
{
indicator: 'indicator-1-id',
filters: [],
location: {
level: 'localAuthority',
value: 'la-1',
},
},
],
visible: true,
size: 50,
showGrid: true,
tickConfig: 'default',
},
minor: {
type: 'major',
groupBy: 'timePeriod',
referenceLines: [],
dataSets: [],
sortAsc: true,
visible: true,
size: 50,
showGrid: true,
min: 0,
tickConfig: 'default',
},
},
type: 'line',
title: 'Emptychart',
alt: 'Some alt text',
height: 300,
width: 600,
};

export const testEmptyChartTableData: TableDataResponse = {
subjectMeta: {
filters: {},
footnotes: [],
indicators: [
{
label: 'Indicator 1',
unit: '',
value: 'indicator-1-id',
name: 'indicator-1',
},
],
locations: {
localAuthority: [
{ id: 'la-1-id', label: 'LA 1', value: 'la-1' },
{ id: 'la-2-id', label: 'LA 2', value: 'la-2' },
],
},
boundaryLevels: [
{
id: 1,
label:
'Countries December 2017 Ultra Generalised Clipped Boundaries in UK',
},
],
publicationName: 'Pupil absence in schools in England',
subjectName: 'Absence by characteristic',
timePeriodRange: [
{ code: 'AY', label: '2020/21', year: 2020 },
{ code: 'AY', label: '2021/22', year: 2021 },
],
geoJsonAvailable: true,
},
results: [
{
filters: [],
geographicLevel: 'localAuthority',
locationId: 'la-1-id',
measures: {
'indicator-1-id': '1',
},
timePeriod: '2020_AY',
},
{
filters: [],
geographicLevel: 'localAuthority',
locationId: 'la-2-id',
measures: {
'indicator-1-id': '2',
},
timePeriod: '2021_AY',
},
],
};