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

feat(a11y): allow user to add optional semantic meaning to goal/gauge charts #1218

Merged
merged 24 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
91bb738
feat: initial commit
rshen91 May 27, 2021
c63b88b
test: update snapshot
rshen91 Jun 10, 2021
a105604
fix: merge changes
rshen91 Jun 22, 2021
6245b05
feat: add goal chart data and labels
rshen91 Jun 22, 2021
0dc89a6
test: add test
rshen91 Jun 22, 2021
ebd8af7
fix: remove min max target value from not goal types
rshen91 Jun 22, 2021
9c90b45
feat: add semantic meaning to goal/gauge charts
rshen91 Jun 23, 2021
37acc83
fix: chart api
rshen91 Jun 23, 2021
503cb9d
fix: api chart extract
rshen91 Jun 23, 2021
91f126b
test: add story snapshot
rshen91 Jun 23, 2021
f63c09c
feat: clean up semantic representation in goals rendering
rshen91 Jun 23, 2021
5620c16
refactor: add goal semantic description component
rshen91 Jun 23, 2021
aba9bae
test: add tests
rshen91 Jun 23, 2021
7c4e432
style: clean up code per code review
rshen91 Jun 23, 2021
0c77eec
Merge remote-tracking branch 'upstream/master' into goal-semantics
rshen91 Jun 30, 2021
3f0681b
style: fix test
rshen91 Jun 30, 2021
250ab2c
fix: update per code review
rshen91 Jun 30, 2021
b5a1364
refactor: change name of prop and data type
rshen91 Jul 1, 2021
4045a9c
fix: update chart api
rshen91 Jul 1, 2021
416e18d
refactor: refactor so bands includes text
rshen91 Jul 1, 2021
50b2991
Merge remote-tracking branch 'upstream/master' into goal-semantics
rshen91 Jul 6, 2021
22ef1a6
fix: update the description so correct band label shown
rshen91 Jul 6, 2021
0bfdec4
refactor: code review
rshen91 Jul 7, 2021
3702ecf
fix: pull lowest value
rshen91 Jul 7, 2021
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/charts/api/charts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,8 @@ export interface GoalSpec extends Spec {
// (undocumented)
labelMinor: string | BandFillColorAccessor;
// (undocumented)
semanticValues?: Array<(number | string)[]>;
monfera marked this conversation as resolved.
Show resolved Hide resolved
// (undocumented)
specType: typeof SpecType.Series;
// Warning: (ae-forgotten-export) The symbol "GoalSubtype" needs to be exported by the entry point index.d.ts
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import { GoalSubtype } from '../../specs/constants';
import { config } from '../config/config';
import { Config } from './config_types';

interface BandViewModel {
/** @internal */
export interface BandViewModel {
value: number;
fillColor: string;
}
Expand All @@ -50,6 +51,7 @@ export interface BulletViewModel {
lowestValue: number;
aboveBaseCount: number;
belowBaseCount: number;
semanticValues?: Array<(string | number)[]>;
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function shapeViewModel(textMeasure: TextMeasure, spec: GoalSpec, config:
labelMinor,
centralMajor,
centralMinor,
semanticValues,
} = spec;

const [lowestValue, highestValue] = [base, target, actual, ...bands, ...ticks].reduce(
Expand Down Expand Up @@ -95,6 +96,7 @@ export function shapeViewModel(textMeasure: TextMeasure, spec: GoalSpec, config:
lowestValue,
aboveBaseCount,
belowBaseCount,
semanticValues,
};

// combined viewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export function renderCanvas2d(
const vertical = subtype === GoalSubtype.VerticalBullet;

const domain = [lowestValue, highestValue];

const data = {
base: { value: base },
...Object.fromEntries(bands.map(({ value }, index) => [`qualitative_${index}`, { value }])),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import React, { MouseEvent, RefObject } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators, Dispatch } from 'redux';

import { ScreenReaderSummary } from '../../../../components/accessibility';
import { GoalSemanticDescription, ScreenReaderSummary } from '../../../../components/accessibility';
import { onChartRendered } from '../../../../state/actions/chart';
import { GlobalChartState } from '../../../../state/chart_state';
import {
Expand All @@ -33,13 +33,15 @@ import { getInternalIsInitializedSelector, InitStatus } from '../../../../state/
import { Dimensions } from '../../../../utils/dimensions';
import { nullShapeViewModel, ShapeViewModel } from '../../layout/types/viewmodel_types';
import { geometries } from '../../state/selectors/geometries';
import { getGoalChartSemanticDataSelector } from '../../state/selectors/get_goal_chart_data';
import { renderCanvas2d } from './canvas_renderers';

interface ReactiveChartStateProps {
initialized: boolean;
geometries: ShapeViewModel;
chartContainerDimensions: Dimensions;
a11ySettings: A11ySettings;
semanticValues?: Array<(string | number)[]>;
}

interface ReactiveChartDispatchProps {
Expand Down Expand Up @@ -112,11 +114,11 @@ class Component extends React.Component<Props> {
chartContainerDimensions: { width, height },
forwardStageRef,
a11ySettings,
semanticValues,
} = this.props;
if (!initialized || width === 0 || height === 0) {
return null;
}

return (
<figure aria-labelledby={a11ySettings.labelId} aria-describedby={a11ySettings.descriptionId}>
<canvas
Expand All @@ -133,6 +135,7 @@ class Component extends React.Component<Props> {
role="presentation"
>
<ScreenReaderSummary />
{semanticValues && semanticValues.length > 1 && <GoalSemanticDescription semanticValues={semanticValues} />}
myasonik marked this conversation as resolved.
Show resolved Hide resolved
</canvas>
</figure>
);
Expand Down Expand Up @@ -183,6 +186,7 @@ const mapStateToProps = (state: GlobalChartState): ReactiveChartStateProps => {
geometries: geometries(state),
chartContainerDimensions: state.parentDimensions,
a11ySettings: getA11ySettingsSelector(state),
semanticValues: getGoalChartSemanticDataSelector(state),
};
};

Expand Down
1 change: 1 addition & 0 deletions packages/charts/src/chart_types/goal_chart/specs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface GoalSpec extends Spec {
centralMajor: string | BandFillColorAccessor;
centralMinor: string | BandFillColorAccessor;
config: RecursivePartial<Config>;
semanticValues?: Array<(number | string)[]>;
}

type SpecRequiredProps = Pick<GoalSpec, 'id' | 'actual'>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { createCustomCachedSelector } from '../../../../state/create_selector';
import { geometries } from './geometries';

/** @internal */
export type GoalChartData = {
maximum: number;
minimum: number;
target: number;
value: number;
};

/** @internal */
export type GoalChartLabels = {
minorLabel: string;
majorLabel: string;
};

/** @internal */
export const getGoalChartDataSelector = createCustomCachedSelector(
[geometries],
(geoms): GoalChartData => {
const goalChartData: GoalChartData = {
maximum: geoms.bulletViewModel.highestValue,
minimum: geoms.bulletViewModel.lowestValue,
target: geoms.bulletViewModel.target,
value: geoms.bulletViewModel.actual,
};
return goalChartData;
},
);

/** @internal */
export const getGoalChartLabelsSelector = createCustomCachedSelector([geometries], (geoms) => {
return { majorLabel: geoms.bulletViewModel.labelMajor, minorLabel: geoms.bulletViewModel.labelMinor };
});

/** @internal */
export const getGoalChartSemanticDataSelector = createCustomCachedSelector([geometries], (geoms) => {
return geoms.bulletViewModel.semanticValues ?? [];
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import React, { MouseEvent, RefObject } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators, Dispatch } from 'redux';

import { ScreenReaderSummary } from '../../../../components/accessibility';
import { ScreenReaderPartitionTable } from '../../../../components/accessibility/partitions_data_table';
import { ScreenReaderSummary, ScreenReaderPartitionTable } from '../../../../components/accessibility';
import { clearCanvas } from '../../../../renderers/canvas';
import { SettingsSpec } from '../../../../specs/settings';
import { onChartRendered } from '../../../../state/actions/chart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ exports[`Chart should render the legend name test 1`] = `
<canvas className=\\"echCanvasRenderer\\" width={150} height={200} style={{...}} role=\\"presentation\\" />
</figure>
<Connect(ScreenReaderSummaryComponent)>
<ScreenReaderSummaryComponent chartTypeDescription=\\"bar chart\\" a11ySettings={{...}} dispatch={[Function: dispatch]}>
<ScreenReaderSummaryComponent chartTypeDescription=\\"bar chart\\" a11ySettings={{...}} goalChartData={{...}} goalChartLabels={{...}} dispatch={[Function: dispatch]}>
<div className=\\"echScreenReaderOnly\\">
<ScreenReaderLabel label={[undefined]} labelId={[undefined]} labelHeadingLevel=\\"p\\" description={[undefined]} descriptionId=\\"chart1--defaultSummary\\" defaultSummaryId=\\"chart1--defaultSummary\\" tableCaption={[undefined]} />
<ScreenReaderLabel label={[undefined]} labelId={[undefined]} labelHeadingLevel=\\"p\\" description={[undefined]} descriptionId=\\"chart1--defaultSummary\\" defaultSummaryId=\\"chart1--defaultSummary\\" tableCaption={[undefined]} goalChartLabels={{...}} />
<ScreenReaderDescription label={[undefined]} labelId={[undefined]} labelHeadingLevel=\\"p\\" description={[undefined]} descriptionId=\\"chart1--defaultSummary\\" defaultSummaryId=\\"chart1--defaultSummary\\" tableCaption={[undefined]} />
<ScreenReaderTypes label={[undefined]} labelId={[undefined]} labelHeadingLevel=\\"p\\" description={[undefined]} descriptionId=\\"chart1--defaultSummary\\" defaultSummaryId=\\"chart1--defaultSummary\\" tableCaption={[undefined]} chartTypeDescription=\\"bar chart\\">
<ScreenReaderTypes label={[undefined]} labelId={[undefined]} labelHeadingLevel=\\"p\\" description={[undefined]} descriptionId=\\"chart1--defaultSummary\\" defaultSummaryId=\\"chart1--defaultSummary\\" tableCaption={[undefined]} chartTypeDescription=\\"bar chart\\" goalChartData={{...}}>
<dl>
<dt>
Chart type:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import { mount } from 'enzyme';
import React from 'react';

import { Goal } from '../../chart_types/goal_chart/specs';
import { GoalSubtype } from '../../chart_types/goal_chart/specs/constants';
import { config } from '../../chart_types/partition_chart/layout/config';
import { PartitionLayout } from '../../chart_types/partition_chart/layout/types/config_types';
import { arrayToLookup } from '../../common/color_calcs';
Expand Down Expand Up @@ -132,4 +134,100 @@ describe('Accessibility', () => {
expect(sunburstLayerWrapper.find('tr').first().text()).toBe('DepthLabelParentValuePercentage');
});
});

describe('Goal chart type accessibility', () => {
const goalChartWrapper = mount(
<Chart className="story-chart">
<Goal
id="spec_1"
subtype={GoalSubtype.Goal}
base={0}
target={260}
actual={170}
bands={[200, 250, 300]}
ticks={[0, 50, 100, 150, 200, 250, 300]}
labelMajor="Revenue 2020 YTD "
labelMinor="(thousand USD) "
centralMajor="170"
centralMinor=""
config={{ angleStart: Math.PI, angleEnd: 0 }}
/>
</Chart>,
);

const bandsWithSemanticsAscending = [
[200, 'freezing'],
[250, 'chilly'],
[300, 'brisk'],
];

const ascendingSemanticValuesGoalChart = mount(
<Chart className="story-chart">
<Goal
id="spec_1"
subtype={GoalSubtype.Goal}
base={0}
target={260}
actual={170}
// doesn't mess with canvas_renderers.ts
// @ts-ignore
bands={bandsWithSemanticsAscending.flat().filter((val) => typeof val === 'number')}
ticks={[0, 50, 100, 150, 200, 250, 300]}
// tickValueFormatter={({ value }: BandFillColorAccessorInput) => String(value)}
// bandFillColor={({ value }: BandFillColorAccessorInput) => semanticBandFillColor(value)}
labelMajor="Revenue 2020 YTD "
labelMinor="(thousand USD) "
centralMajor="170"
centralMinor=""
config={{ angleStart: Math.PI, angleEnd: 0 }}
semanticValues={bandsWithSemanticsAscending}
/>
</Chart>,
);

const bandsWithSemanticsDescending = [
[300, 'brisk'],
[250, 'chilly'],
[200, 'freezing'],
];

const descendingSemanticValuesGoalChart = mount(
<Chart className="story-chart">
<Goal
id="spec_1"
subtype={GoalSubtype.Goal}
base={300}
target={260}
actual={170}
// doesn't mess with canvas_renderers.ts
// @ts-ignore
bands={bandsWithSemanticsDescending.flat().filter((val) => typeof val === 'number')}
ticks={[300, 250, 200, 150, 100, 50, 0]}
// tickValueFormatter={({ value }: BandFillColorAccessorInput) => String(value)}
// bandFillColor={({ value }: BandFillColorAccessorInput) => semanticBandFillColor(value)}
labelMajor="Revenue 2020 YTD "
labelMinor="(thousand USD) "
centralMajor="170"
centralMinor=""
config={{ angleStart: Math.PI, angleEnd: 0 }}
semanticValues={bandsWithSemanticsDescending}
/>
</Chart>,
);
it('should test defaults for goal charts', () => {
expect(goalChartWrapper.find('.echScreenReaderOnly').first().text()).toBe(
'Goal chart label: Revenue 2020 YTD (thousand USD) Chart type:goal chartMinimum: 0Maximum: 300Target: 260Value: 170',
);
});
it('should correctly render ascending semantic values', () => {
expect(ascendingSemanticValuesGoalChart.find('.echGoalDescription').first().text()).toBe(
'values 200 - 250: freezingvalues 250 - 300: chillyvalues above 300: brisk',
);
});
it('should correctly render descending semantic values', () => {
expect(descendingSemanticValuesGoalChart.find('.echGoalDescription').first().text()).toBe(
'values 300 - 250: briskvalues 250 - 200: chillyvalues below 200: freezing',
);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import React from 'react';

interface GoalSemanticDescriptionProps {
semanticValues: Array<(string | number)[]>;
}

/** @internal */
export const GoalSemanticDescription = ({ semanticValues }: GoalSemanticDescriptionProps) => {
return (
<div className="echScreenReaderOnly echGoalDescription">
{semanticValues.map(([value, semantic], index) => {
const nextValue = semanticValues[index + 1];
const prevValue = semanticValues[index - 1];
return nextValue !== undefined ? (
<dd key={index}>{`values ${value} - ${nextValue[0]}: ${semantic}`}</dd>
) : prevValue[0] < value ? (
<dd key={index}>{`values above ${value}: ${semantic}`}</dd>
) : (
<dd key={index}>{`values below ${value}: ${semantic}`}</dd>
);
})}
</div>
myasonik marked this conversation as resolved.
Show resolved Hide resolved
);
};
2 changes: 2 additions & 0 deletions packages/charts/src/components/accessibility/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

/* @internal */
export { ScreenReaderSummary } from './screen_reader_summary';
export { ScreenReaderPartitionTable } from './partitions_data_table';
export { GoalSemanticDescription } from './goal_semantic_description';
27 changes: 23 additions & 4 deletions packages/charts/src/components/accessibility/label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,30 @@

import React from 'react';

import { GoalChartLabels } from '../../chart_types/goal_chart/state/selectors/get_goal_chart_data';
import { A11ySettings } from '../../state/selectors/get_accessibility_config';

interface ScreenReaderLabelProps {
goalChartLabels?: GoalChartLabels;
}

/** @internal */
export function ScreenReaderLabel(props: A11ySettings) {
if (!props.label) return null;
const Heading = props.labelHeadingLevel;
return <Heading id={props.labelId}>{props.label}</Heading>;
export function ScreenReaderLabel({
label,
labelHeadingLevel,
labelId,
goalChartLabels,
}: A11ySettings & ScreenReaderLabelProps) {
if (!label && !goalChartLabels?.majorLabel) return null;
const Heading = labelHeadingLevel;
const goalChartLabelsSection = !goalChartLabels?.majorLabel
? null
: `Goal chart label: ${goalChartLabels.majorLabel} ${goalChartLabels.minorLabel}`;

return (
<Heading id={labelId}>
{label}
{goalChartLabelsSection}
</Heading>
);
}
Loading