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(xy): apply the data value formatter to data values over bars #1419

Merged
merged 19 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from 15 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions integration/tests/bar_stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,27 @@ describe('Bar series stories', () => {
});
});

describe('value label positioning and formatting', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, works well and visually is easy to read and check for defects!
I think we can really remove the screenshots and tests that looks similar to this but with a different story (the describe test before this one: value labels positioning.
I think we can remove it, but keeping the additional test in the describe called clip both geometry and chart area values. The reason is the following: that set screenshot is difficult to read, the high decimal values + very thin bars makes it difficult to understand what is the test about (rendering value labels with different configurations)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call - updated vrts in 14182f by deleting the old bar_stories vrts and rerunning.

eachRotation.describe((rotation) => {
describe.each<NonNullable<DisplayValueStyle['alignment']>['vertical']>([
VerticalAlignment.Middle,
VerticalAlignment.Top,
VerticalAlignment.Bottom,
])('Vertical Alignment - %s', (verticalAlignment) => {
describe.each<NonNullable<DisplayValueStyle['alignment']>['horizontal']>([
HorizontalAlignment.Left,
HorizontalAlignment.Center,
HorizontalAlignment.Right,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

])('Horizontal Alignment - %s', (horizontalAlignment) => {
const url = `http://localhost:9001/?path=/story/bar-chart--data-value&args=&globals=theme:light&knob-chartRotation=${rotation}&knob-Horizontal alignment=${horizontalAlignment}&knob-Vertical alignment=${verticalAlignment}`;
it('place the value labels on the correct area', async () => {
await common.expectChartAtUrlToMatchScreenshot(url);
});
});
});
});
});

describe('functional accessors', () => {
it('functional accessors with fieldName', async () => {
await common.expectChartAtUrlToMatchScreenshot(
Expand Down
8 changes: 2 additions & 6 deletions packages/charts/src/chart_types/xy_chart/annotations/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { AnnotationId, AxisId, GroupId } from '../../../utils/ids';
import { Point } from '../../../utils/point';
import { AxisStyle } from '../../../utils/themes/theme';
import { SmallMultipleScales } from '../state/selectors/compute_small_multiple_scales';
import { isHorizontalRotation } from '../state/utils/common';
import { getAxesSpecForSpecId } from '../state/utils/spec';
import { ComputedGeometries } from '../state/utils/types';
import { AnnotationDomainType, AnnotationSpec, AxisSpec, isLineAnnotation } from '../utils/specs';
Expand All @@ -29,12 +28,9 @@ export function getAnnotationAxis(
domainType: AnnotationDomainType,
chartRotation: Rotation,
): Position | undefined {
const { xAxis, yAxis } = getAxesSpecForSpecId(axesSpecs, groupId);
const isHorizontalRotated = isHorizontalRotation(chartRotation);
const { xAxis, yAxis } = getAxesSpecForSpecId(axesSpecs, groupId, chartRotation);
const isXDomainAnnotation = isXDomain(domainType);
const annotationAxis = isXDomainAnnotation ? xAxis : yAxis;
const rotatedAnnotation = isHorizontalRotated ? annotationAxis : isXDomainAnnotation ? yAxis : xAxis;
return rotatedAnnotation ? rotatedAnnotation.position : undefined;
return isXDomainAnnotation ? xAxis?.position : yAxis?.position;
}

/** @internal */
Expand Down
5 changes: 3 additions & 2 deletions packages/charts/src/chart_types/xy_chart/legend/legend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { LegendItem } from '../../../common/legend';
import { SeriesKey, SeriesIdentifier } from '../../../common/series_id';
import { ScaleType } from '../../../scales/constants';
import { TickFormatterOptions } from '../../../specs';
import { mergePartial } from '../../../utils/common';
import { mergePartial, Rotation } from '../../../utils/common';
import { BandedAccessorType } from '../../../utils/geometry';
import { getLegendCompareFn } from '../../../utils/series_sort';
import { PointStyle, Theme } from '../../../utils/themes/theme';
Expand Down Expand Up @@ -104,6 +104,7 @@ export function computeLegend(
serialIdentifierDataSeriesMap: Record<string, DataSeries>,
deselectedDataSeries: SeriesIdentifier[] = [],
theme: Theme,
chartRotation: Rotation,
): LegendItem[] {
const legendItems: LegendItem[] = [];
const defaultColor = theme.colors.defaultVizColor;
Expand Down Expand Up @@ -132,7 +133,7 @@ export function computeLegend(
const labelY1 = banded ? getBandedLegendItemLabel(name, BandedAccessorType.Y1, postFixes) : name;

// Use this to get axis spec w/ tick formatter
const { yAxis } = getAxesSpecForSpecId(axesSpecs, spec.groupId);
const { yAxis } = getAxesSpecForSpecId(axesSpecs, spec.groupId, chartRotation);
const formatter = spec.tickFormat ?? yAxis?.tickFormat ?? defaultTickFormatter;
const { hideInLegend } = spec;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const computeLegendSelector = createCustomCachedSelector(
siDataSeriesMap,
deselectedDataSeries,
chartTheme,
settings.rotation,
);
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function getTooltipAndHighlightFromValue(
if (!spec) {
return acc;
}
const { xAxis, yAxis } = getAxesSpecForSpecId(axesSpecs, spec.groupId);
const { xAxis, yAxis } = getAxesSpecForSpecId(axesSpecs, spec.groupId, chartRotation);

// yScales is ensured by the enclosing if
const yScale = scales.yScales.get(getSpecDomainGroupId(spec));
Expand All @@ -163,21 +163,12 @@ function getTooltipAndHighlightFromValue(
}

// format the tooltip values
const yAxisFormatSpec = [0, 180].includes(chartRotation) ? yAxis : xAxis;
const formattedTooltip = formatTooltip(
indexedGeometry,
spec,
false,
isHighlighted,
hasSingleSeries,
yAxisFormatSpec,
);
const formattedTooltip = formatTooltip(indexedGeometry, spec, false, isHighlighted, hasSingleSeries, yAxis);

// format only one time the x value
if (!header) {
// if we have a tooltipHeaderFormatter, then don't pass in the xAxis as the user will define a formatter
const xAxisFormatSpec = [0, 180].includes(chartRotation) ? xAxis : yAxis;
const formatterAxis = tooltipHeaderFormatter ? undefined : xAxisFormatSpec;
const formatterAxis = tooltipHeaderFormatter ? undefined : xAxis;
header = formatTooltip(indexedGeometry, spec, true, false, hasSingleSeries, formatterAxis);
}

Expand Down
9 changes: 5 additions & 4 deletions packages/charts/src/chart_types/xy_chart/state/utils/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
*/

import { BasicSeriesSpec, DEFAULT_GLOBAL_ID, Spec } from '../../../../specs';
import { Rotation } from '../../../../utils/common';
import { GroupId } from '../../../../utils/ids';
import { isHorizontalAxis, isVerticalAxis } from '../../utils/axis_type_utils';
import { isXDomain } from '../../utils/axis_utils';
import { AxisSpec } from '../../utils/specs';

/** @internal */
Expand All @@ -17,10 +18,10 @@ export function getSpecsById<T extends Spec>(specs: T[], id: string): T | undefi
}

/** @internal */
export function getAxesSpecForSpecId(axesSpecs: AxisSpec[], groupId: GroupId) {
export function getAxesSpecForSpecId(axesSpecs: AxisSpec[], groupId: GroupId, chartRotation: Rotation = 0) {
return axesSpecs.reduce<{ xAxis?: AxisSpec; yAxis?: AxisSpec }>((result, spec) => {
if (spec.groupId === groupId && isHorizontalAxis(spec.position)) result.xAxis = spec;
if (spec.groupId === groupId && isVerticalAxis(spec.position)) result.yAxis = spec;
if (spec.groupId === groupId && isXDomain(spec.position, chartRotation)) result.xAxis = spec;
else if (spec.groupId === groupId && !isXDomain(spec.position, chartRotation)) result.yAxis = spec;
return result;
}, {});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,9 @@ function renderGeometries(
if (shift === -1) continue; // skip bar dataSeries if index is not available

const barSeriesStyle = mergePartial(chartTheme.barSeriesStyle, spec.barSeriesStyle);
const { yAxis } = getAxesSpecForSpecId(axesSpecs, spec.groupId);
const { yAxis } = getAxesSpecForSpecId(axesSpecs, spec.groupId, chartRotation);
const valueFormatter = yAxis?.tickFormat ?? fallBackTickFormatter;

const displayValueSettings = spec.displayValueSettings
? { valueFormatter, ...spec.displayValueSettings }
: undefined;
Expand Down
64 changes: 64 additions & 0 deletions storybook/stories/bar/58_data_values.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { select } from '@storybook/addon-knobs';
import React from 'react';

import { Axis, BarSeries, Chart, ScaleType, Settings, PartialTheme } from '@elastic/charts';

import { BARCHART_1Y0G_LINEAR } from '../../../packages/charts/src/utils/data_samples/test_dataset';
import { useBaseTheme } from '../../use_base_theme';
import { getChartRotationKnob } from '../utils/knobs';

export const Example = () => {
const theme: PartialTheme = {
barSeriesStyle: {
displayValue: {
fontSize: 10,
fill: 'black',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest making this larger because it blends in. For others that view this story I'd be easier to view the change when the value is more pronounced.

alignment: {
horizontal: select(
'Horizontal alignment',
{
Default: undefined,
Left: 'left',
Center: 'center',
Right: 'right',
},
undefined,
),
vertical: select(
'Vertical alignment',
{
Default: undefined,
Top: 'top',
Middle: 'middle',
Bottom: 'bottom',
},
undefined,
),
},
},
},
};

return (
<Chart>
<Settings theme={theme} baseTheme={useBaseTheme()} rotation={getChartRotationKnob()} />
<BarSeries
id="bars"
data={BARCHART_1Y0G_LINEAR}
yAccessors={['y']}
xScaleType={ScaleType.Linear}
displayValueSettings={{ showValueLabel: true }}
/>
<Axis id="bottom-axis" position="bottom" tickFormat={(d) => `${d} H`} />
<Axis id="left-axis" position="left" tickFormat={(d) => `${d} V`} />
</Chart>
);
};
1 change: 1 addition & 0 deletions storybook/stories/bar/bars.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ export { Example as tooltipBoundary } from './55_tooltip_boundary.story';
export { Example as testDualYAxis } from './49_test_dual_axis.story';
export { Example as testUseDefaultGroupDomain } from './56_test_use_dfl_gdomain.story';
export { Example as testRectBorder } from './57_test_rect_border_bars.story';
export { Example as dataValue } from './58_data_values.story';