Skip to content

Commit

Permalink
[8.7] [Lens] Fix reference line label rendering (#151162) (#151259)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.7`:
- [[Lens] Fix reference line label rendering
(#151162)](#151162)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Marco
Liberati","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-02-15T09:18:44Z","message":"[Lens]
Fix reference line label rendering (#151162)\n\n## Summary\r\n\r\nFix
#151101 \r\n\r\nRefactored shared code between reference line and
annotation rendering\r\nto restore label rendering on Reference
line.\r\n\r\nThe root issue was a refactoring of the code who only
considered the\r\nannotation configuration shape, which slightly differs
from the\r\nreference lines one, and in particular a stricter check for
the label\r\nconfiguration together with the assumption that an icon was
always shown\r\nin the annotation marker.\r\n\r\nI've added few new unit
tests to detect changes in the future in some\r\nannotation logic, but
it would be best to extend them in the future.\r\n\r\n<img width=\"444\"
alt=\"Screenshot 2023-02-14 at 15 29
10\"\r\nsrc=\"https://user-images.githubusercontent.com/924948/218792072-a56ece78-07fd-4c0b-ab63-017590281b18.png\">\r\n<img
width=\"451\" alt=\"Screenshot 2023-02-14 at 15 29
17\"\r\nsrc=\"https://user-images.githubusercontent.com/924948/218792082-85630355-b003-4dea-86ca-3e90707c8293.png\">\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [x] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] Any UI
touched in this PR is usable by keyboard only (learn more\r\nabout
[keyboard accessibility](https://webaim.org/techniques/keyboard/))\r\n-
[ ] Any UI touched in this PR does not create any new axe
failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
Risk Matrix\r\n\r\nDelete this section if it is not applicable to this
PR.\r\n\r\nBefore closing this PR, invite QA, stakeholders, and other
developers to\r\nidentify risks that should be tested prior to the
change/feature\r\nrelease.\r\n\r\nWhen forming the risk matrix, consider
some of the following examples\r\nand how they may potentially impact
the change:\r\n\r\n| Risk | Probability | Severity | Mitigation/Notes
|\r\n\r\n|---------------------------|-------------|----------|-------------------------|\r\n|
Multiple Spaces&mdash;unexpected behavior in non-default Kibana
Space.\r\n| Low | High | Integration tests will verify that all features
are still\r\nsupported in non-default Kibana Space and when user
switches between\r\nspaces. |\r\n| Multiple nodes&mdash;Elasticsearch
polling might have race conditions\r\nwhen multiple Kibana nodes are
polling for the same tasks. | High | Low\r\n| Tasks are idempotent, so
executing them multiple times will not result\r\nin logical error, but
will degrade performance. To test for this case we\r\nadd plenty of unit
tests around this logic and document manual testing\r\nprocedure. |\r\n|
Code should gracefully handle cases when feature X or plugin Y
are\r\ndisabled. | Medium | High | Unit tests will verify that any
feature flag\r\nor plugin combination still results in our service
operational. |\r\n| [See more potential
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
|\r\n\r\n\r\n### For maintainers\r\n\r\n- [ ] This was checked for
breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"09033c86dbb9c1b417ea540ac9c03b532f509288","branchLabelMapping":{"^v8.8.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Visualizations","release_note:skip","Feature:Lens","backport:prev-minor","v8.7.0","v8.8.0"],"number":151162,"url":"https://github.com/elastic/kibana/pull/151162","mergeCommit":{"message":"[Lens]
Fix reference line label rendering (#151162)\n\n## Summary\r\n\r\nFix
#151101 \r\n\r\nRefactored shared code between reference line and
annotation rendering\r\nto restore label rendering on Reference
line.\r\n\r\nThe root issue was a refactoring of the code who only
considered the\r\nannotation configuration shape, which slightly differs
from the\r\nreference lines one, and in particular a stricter check for
the label\r\nconfiguration together with the assumption that an icon was
always shown\r\nin the annotation marker.\r\n\r\nI've added few new unit
tests to detect changes in the future in some\r\nannotation logic, but
it would be best to extend them in the future.\r\n\r\n<img width=\"444\"
alt=\"Screenshot 2023-02-14 at 15 29
10\"\r\nsrc=\"https://user-images.githubusercontent.com/924948/218792072-a56ece78-07fd-4c0b-ab63-017590281b18.png\">\r\n<img
width=\"451\" alt=\"Screenshot 2023-02-14 at 15 29
17\"\r\nsrc=\"https://user-images.githubusercontent.com/924948/218792082-85630355-b003-4dea-86ca-3e90707c8293.png\">\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [x] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] Any UI
touched in this PR is usable by keyboard only (learn more\r\nabout
[keyboard accessibility](https://webaim.org/techniques/keyboard/))\r\n-
[ ] Any UI touched in this PR does not create any new axe
failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
Risk Matrix\r\n\r\nDelete this section if it is not applicable to this
PR.\r\n\r\nBefore closing this PR, invite QA, stakeholders, and other
developers to\r\nidentify risks that should be tested prior to the
change/feature\r\nrelease.\r\n\r\nWhen forming the risk matrix, consider
some of the following examples\r\nand how they may potentially impact
the change:\r\n\r\n| Risk | Probability | Severity | Mitigation/Notes
|\r\n\r\n|---------------------------|-------------|----------|-------------------------|\r\n|
Multiple Spaces&mdash;unexpected behavior in non-default Kibana
Space.\r\n| Low | High | Integration tests will verify that all features
are still\r\nsupported in non-default Kibana Space and when user
switches between\r\nspaces. |\r\n| Multiple nodes&mdash;Elasticsearch
polling might have race conditions\r\nwhen multiple Kibana nodes are
polling for the same tasks. | High | Low\r\n| Tasks are idempotent, so
executing them multiple times will not result\r\nin logical error, but
will degrade performance. To test for this case we\r\nadd plenty of unit
tests around this logic and document manual testing\r\nprocedure. |\r\n|
Code should gracefully handle cases when feature X or plugin Y
are\r\ndisabled. | Medium | High | Unit tests will verify that any
feature flag\r\nor plugin combination still results in our service
operational. |\r\n| [See more potential
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
|\r\n\r\n\r\n### For maintainers\r\n\r\n- [ ] This was checked for
breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"09033c86dbb9c1b417ea540ac9c03b532f509288"}},"sourceBranch":"main","suggestedTargetBranches":["8.7"],"targetPullRequestStates":[{"branch":"8.7","label":"v8.7.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.8.0","labelRegex":"^v8.8.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/151162","number":151162,"mergeCommit":{"message":"[Lens]
Fix reference line label rendering (#151162)\n\n## Summary\r\n\r\nFix
#151101 \r\n\r\nRefactored shared code between reference line and
annotation rendering\r\nto restore label rendering on Reference
line.\r\n\r\nThe root issue was a refactoring of the code who only
considered the\r\nannotation configuration shape, which slightly differs
from the\r\nreference lines one, and in particular a stricter check for
the label\r\nconfiguration together with the assumption that an icon was
always shown\r\nin the annotation marker.\r\n\r\nI've added few new unit
tests to detect changes in the future in some\r\nannotation logic, but
it would be best to extend them in the future.\r\n\r\n<img width=\"444\"
alt=\"Screenshot 2023-02-14 at 15 29
10\"\r\nsrc=\"https://user-images.githubusercontent.com/924948/218792072-a56ece78-07fd-4c0b-ab63-017590281b18.png\">\r\n<img
width=\"451\" alt=\"Screenshot 2023-02-14 at 15 29
17\"\r\nsrc=\"https://user-images.githubusercontent.com/924948/218792082-85630355-b003-4dea-86ca-3e90707c8293.png\">\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [x] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] Any UI
touched in this PR is usable by keyboard only (learn more\r\nabout
[keyboard accessibility](https://webaim.org/techniques/keyboard/))\r\n-
[ ] Any UI touched in this PR does not create any new axe
failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
Risk Matrix\r\n\r\nDelete this section if it is not applicable to this
PR.\r\n\r\nBefore closing this PR, invite QA, stakeholders, and other
developers to\r\nidentify risks that should be tested prior to the
change/feature\r\nrelease.\r\n\r\nWhen forming the risk matrix, consider
some of the following examples\r\nand how they may potentially impact
the change:\r\n\r\n| Risk | Probability | Severity | Mitigation/Notes
|\r\n\r\n|---------------------------|-------------|----------|-------------------------|\r\n|
Multiple Spaces&mdash;unexpected behavior in non-default Kibana
Space.\r\n| Low | High | Integration tests will verify that all features
are still\r\nsupported in non-default Kibana Space and when user
switches between\r\nspaces. |\r\n| Multiple nodes&mdash;Elasticsearch
polling might have race conditions\r\nwhen multiple Kibana nodes are
polling for the same tasks. | High | Low\r\n| Tasks are idempotent, so
executing them multiple times will not result\r\nin logical error, but
will degrade performance. To test for this case we\r\nadd plenty of unit
tests around this logic and document manual testing\r\nprocedure. |\r\n|
Code should gracefully handle cases when feature X or plugin Y
are\r\ndisabled. | Medium | High | Unit tests will verify that any
feature flag\r\nor plugin combination still results in our service
operational. |\r\n| [See more potential
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
|\r\n\r\n\r\n### For maintainers\r\n\r\n- [ ] This was checked for
breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"09033c86dbb9c1b417ea540ac9c03b532f509288"}}]}]
BACKPORT-->

Co-authored-by: Marco Liberati <[email protected]>
  • Loading branch information
kibanamachine and dej611 authored Feb 15, 2023
1 parent 360bc05 commit d4d43e8
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('referenceLine', () => {
value: 100,
fill: 'above',
position: 'bottom',
forAccessor: '',
};

const result = referenceLineFunction.fn(null, args, createMockExecutionContext());
Expand Down Expand Up @@ -47,6 +48,7 @@ describe('referenceLine', () => {
color: '#fff',
fill: 'below',
textVisibility: true,
forAccessor: '',
};

const result = referenceLineFunction.fn(null, args, createMockExecutionContext());
Expand All @@ -71,6 +73,7 @@ describe('referenceLine', () => {
value: 100,
fill: 'none',
position: 'bottom',
forAccessor: '',
};

const result = referenceLineFunction.fn(null, args, createMockExecutionContext());
Expand All @@ -96,6 +99,7 @@ describe('referenceLine', () => {
textVisibility: true,
fill: 'none',
position: 'bottom',
forAccessor: '',
};

const result = referenceLineFunction.fn(null, args, createMockExecutionContext());
Expand Down Expand Up @@ -123,6 +127,7 @@ describe('referenceLine', () => {
textVisibility,
fill: 'none',
position: 'bottom',
forAccessor: '',
};

const result = referenceLineFunction.fn(null, args, createMockExecutionContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export const referenceLineFunction: ReferenceLineFn = {
default: FillStyles.NONE,
strict: true,
},
forAccessor: {
types: ['string'],
help: '',
default: '',
},
},
fn(table, args) {
const textVisibility =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@ export type ExtendedAnnotationLayerConfigResult = ExtendedAnnotationLayerArgs &
layerType: typeof LayerTypes.ANNOTATIONS;
};

export interface ReferenceLineArgs
extends Omit<ReferenceLineDecorationConfig, 'forAccessor' | 'fill'> {
export interface ReferenceLineArgs extends Omit<ReferenceLineDecorationConfig, 'fill'> {
name?: string;
value: number;
fill: FillStyle;
Expand Down Expand Up @@ -365,6 +364,7 @@ export interface ReferenceLineConfigResult {
type: typeof REFERENCE_LINE;
layerType: typeof LayerTypes.REFERENCELINE;
lineLength: number;
columnToLabel?: string;
decorations: [ExtendedReferenceLineDecorationConfig];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,27 @@ export const ReferenceLine: FC<ReferenceLineProps> = ({
}) => {
const {
decorations: [decorationConfig],
columnToLabel,
} = layer;

if (!decorationConfig) {
return null;
}

const { value } = decorationConfig;
const columnToLabelMap: Record<string, string> = columnToLabel ? JSON.parse(columnToLabel) : {};

const axisGroup = getAxisGroupForReferenceLine(axesConfiguration, decorationConfig, isHorizontal);

const formatter = axisGroup?.formatter || xAxisFormatter;
const id = `${layer.layerId}-${value}`;
const name = decorationConfig.textVisibility
? columnToLabelMap[decorationConfig.forAccessor]
: undefined;

return (
<ReferenceLineAnnotations
config={{ id, ...decorationConfig, nextValue, axisGroup }}
config={{ id, ...decorationConfig, name, nextValue, axisGroup }}
paddingMap={paddingMap}
axesMap={yAxesMap}
formatter={formatter}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const ReferenceLineAnnotations: FC<Props> = ({
paddingMap,
isHorizontal,
}) => {
const { id, axisGroup, iconPosition, name, textVisibility, value, fill, color } = config;
const { id, axisGroup, iconPosition, name, value, fill, color } = config;

const defaultColor = euiLightVars.euiColorDarkShade;
// get the position for vertical chart
Expand All @@ -85,18 +85,9 @@ export const ReferenceLineAnnotations: FC<Props> = ({
getOriginalAxisPosition(axisGroup?.position ?? Position.Bottom, isHorizontal)
);
// the padding map is built for vertical chart
const hasReducedPadding = paddingMap[markerPositionVertical] === LINES_MARKER_SIZE;
const isTextOnlyMarker = paddingMap[markerPositionVertical] === LINES_MARKER_SIZE;

const props = getLineAnnotationProps(
config,
{
markerLabel: name,
markerBodyLabel: textVisibility && !hasReducedPadding ? name : undefined,
},
axesMap,
paddingMap,
isHorizontal
);
const props = getLineAnnotationProps(config, name, axesMap, isHorizontal, isTextOnlyMarker);

const sharedStyle = getSharedStyle(config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ describe('ReferenceLines', () => {
lineStyle: 'solid',
fill,
value,
forAccessor: '',
}),
]}
/>
Expand Down Expand Up @@ -576,6 +577,7 @@ describe('ReferenceLines', () => {
lineStyle: 'solid',
fill,
value,
forAccessor: '',
}),
]}
/>
Expand Down Expand Up @@ -617,12 +619,14 @@ describe('ReferenceLines', () => {
lineStyle: 'solid',
fill,
value,
forAccessor: '',
}),
createReferenceLine(layerPrefix, 10, {
position,
lineStyle: 'solid',
fill,
value,
forAccessor: '',
}),
]}
/>
Expand Down Expand Up @@ -667,12 +671,14 @@ describe('ReferenceLines', () => {
lineStyle: 'solid',
fill,
value,
forAccessor: '',
}),
createReferenceLine(layerPrefix, 10, {
position: 'bottom',
lineStyle: 'solid',
fill,
value,
forAccessor: '',
}),
]}
/>
Expand Down Expand Up @@ -720,12 +726,14 @@ describe('ReferenceLines', () => {
lineStyle: 'solid',
fill: 'above',
value: value1,
forAccessor: '',
}),
createReferenceLine(layerPrefix, 10, {
position,
lineStyle: 'solid',
fill: 'below',
value: value2,
forAccessor: '',
}),
]}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
*/

import { AxisTypeId, computeInputCombinations, PosType } from './_mocks';
import { computeChartMargins } from './utils';
import { AxisConfiguration } from '../../helpers';
import { computeChartMargins, getLineAnnotationProps } from './utils';
import { AxesMap, AxisConfiguration, Marker, MarkerBody } from '../../helpers';
import { ReferenceLineAnnotationConfig } from './reference_line_annotations';
import { Position } from '@elastic/charts';
import React from 'react';

describe('reference lines helpers', () => {
describe('computeChartMargins', () => {
Expand Down Expand Up @@ -36,4 +39,122 @@ describe('reference lines helpers', () => {
}
);
});

describe('getLineAnnotationProps', () => {
function getAxesMap({ left, right }: Partial<AxesMap> = {}): AxesMap {
return {
left: { groupId: 'yLeft', position: Position.Left, series: [], ...left },
right: { groupId: 'yRight', position: Position.Right, series: [], ...right },
};
}
function getConfig(
customPartialConfig: Partial<ReferenceLineAnnotationConfig> = {}
): ReferenceLineAnnotationConfig {
return {
id: 'id',
value: 3,
lineWidth: 5,
textVisibility: false,
iconPosition: 'auto',
axisGroup: getAxesMap().left,
...customPartialConfig,
};
}

it('should render only the line with no marker', () => {
const config = getConfig();
expect(getLineAnnotationProps(config, 'myLabel', getAxesMap(), false, false)).toEqual(
expect.objectContaining({
groupId: 'yLeft',
markerPosition: Position.Left,
marker: (
<Marker
config={config}
label={'myLabel'}
isHorizontal={false}
hasReducedPadding={false}
/>
),
markerBody: <MarkerBody label={undefined} isHorizontal={false} />,
})
);
});
it('should render an icon marker', () => {
const config = getConfig({ icon: 'triangle' });
expect(getLineAnnotationProps(config, 'myLabel', getAxesMap(), false, false)).toEqual(
expect.objectContaining({
groupId: 'yLeft',
markerPosition: Position.Left,
marker: (
<Marker
config={config}
label={'myLabel'}
isHorizontal={false}
hasReducedPadding={false}
/>
),
markerBody: <MarkerBody label={undefined} isHorizontal={false} />,
})
);
});
it('should render only the label', () => {
const config = getConfig({ textVisibility: true });
expect(getLineAnnotationProps(config, 'myLabel', getAxesMap(), false, true)).toEqual(
expect.objectContaining({
groupId: 'yLeft',
markerPosition: Position.Left,
marker: (
<Marker
config={config}
label={'myLabel'}
isHorizontal={false}
hasReducedPadding={true}
/>
),
markerBody: <MarkerBody label={undefined} isHorizontal={false} />,
})
);
});
it('should render both icon and text', () => {
const config = getConfig({ icon: 'triangle', textVisibility: true });
expect(getLineAnnotationProps(config, 'myLabel', getAxesMap(), false, false)).toEqual(
expect.objectContaining({
groupId: 'yLeft',
markerPosition: Position.Left,
marker: (
<Marker
config={config}
label={'myLabel'}
isHorizontal={false}
hasReducedPadding={false}
/>
),
markerBody: <MarkerBody label={'myLabel'} isHorizontal={false} />,
})
);
});

it('should handle marker rotated label', () => {
const config = getConfig({
icon: 'triangle',
textVisibility: true,
axisGroup: { groupId: 'x', position: Position.Bottom, series: [] },
});
expect(getLineAnnotationProps(config, 'myLabel', getAxesMap(), false, false)).toEqual(
expect.objectContaining({
groupId: 'x',
markerPosition: Position.Top,
marker: (
<Marker
config={config}
label={'myLabel'}
isHorizontal={true}
hasReducedPadding={false}
/>
),
markerBody: <MarkerBody label={'myLabel'} isHorizontal={true} />,
})
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ import {
import { FillStyles } from '../../../common/constants';
import {
GroupsConfiguration,
LINES_MARKER_SIZE,
mapVerticalToHorizontalPlacement,
Marker,
MarkerBody,
getAxisPosition,
getOriginalAxisPosition,
AxesMap,
} from '../../helpers';
import { ReferenceLineAnnotationConfig } from './reference_line_annotations';
import type { ReferenceLineAnnotationConfig } from './reference_line_annotations';

// if there's just one axis, put it on the other one
// otherwise use the same axis
Expand Down Expand Up @@ -84,10 +83,10 @@ export const getSharedStyle = (config: ReferenceLineAnnotationConfig) => ({

export const getLineAnnotationProps = (
config: ReferenceLineAnnotationConfig,
labels: { markerLabel?: string; markerBodyLabel?: string },
label: string | undefined,
axesMap: AxesMap,
paddingMap: Partial<Record<Position, number>>,
isHorizontal: boolean
isHorizontal: boolean,
isTextOnlyMarker: boolean
) => {
// get the position for vertical chart
const markerPositionVertical = getBaseIconPlacement(
Expand All @@ -96,27 +95,27 @@ export const getLineAnnotationProps = (
getOriginalAxisPosition(config.axisGroup?.position ?? Position.Bottom, isHorizontal)
);

// the padding map is built for vertical chart
const hasReducedPadding = paddingMap[markerPositionVertical] === LINES_MARKER_SIZE;

const markerPosition = isHorizontal
? mapVerticalToHorizontalPlacement(markerPositionVertical)
: markerPositionVertical;

const isMarkerLabelHorizontal =
markerPosition === Position.Bottom || markerPosition === Position.Top;

return {
groupId: config.axisGroup?.groupId || 'bottom',
marker: (
<Marker
config={config}
label={labels.markerLabel}
isHorizontal={isHorizontal}
hasReducedPadding={hasReducedPadding}
label={label}
isHorizontal={isMarkerLabelHorizontal}
hasReducedPadding={isTextOnlyMarker}
/>
),
markerBody: (
<MarkerBody
label={labels.markerBodyLabel}
isHorizontal={markerPosition === Position.Bottom || markerPosition === Position.Top}
label={config.textVisibility && !isTextOnlyMarker ? label : undefined}
isHorizontal={isMarkerLabelHorizontal}
/>
),
// rotate the position if required
Expand Down
Loading

0 comments on commit d4d43e8

Please sign in to comment.