-
Notifications
You must be signed in to change notification settings - Fork 121
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
fix: outside rect annotation placement and group relations #2471
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d00d4a1
fix: outside rect annotation when used without groupId
nickofthyme f16e4f2
chore: render inside if both directions are provided
nickofthyme c9c9740
chore: update story defaults
nickofthyme 186055d
Merge branch 'main' into fix-outside-rect-anno
nickofthyme d8b80ee
fix: eslint errors
nickofthyme 3bf8f06
fix: type errors
nickofthyme 2584aa8
fix: eslint errors
nickofthyme 5e0deeb
Merge branch 'main' into fix-outside-rect-anno
nickofthyme f2e14c1
test(vrt): update screenshots [skip ci]
elastic-datavis[bot] 15397b6
chore: update e2e test options
nickofthyme c549003
test(vrt): update screenshots [skip ci]
elastic-datavis[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file added
BIN
+21.1 KB
...nnotations/should-render-outside-annotations-with-no-group-ids-chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,17 @@ | |
|
||
import { AnnotationRectProps } from './types'; | ||
import { getPanelSize, SmallMultipleScales } from '../../../../common/panel_utils'; | ||
import { Rect } from '../../../../geoms/types'; | ||
import { ScaleBand, ScaleContinuous } from '../../../../scales'; | ||
import { isBandScale, isContinuousScale } from '../../../../scales/types'; | ||
import { isDefined, isNil, Position, Rotation } from '../../../../utils/common'; | ||
import { Size } from '../../../../utils/dimensions'; | ||
import { AxisId, GroupId } from '../../../../utils/ids'; | ||
import { Logger } from '../../../../utils/logger'; | ||
import { Point } from '../../../../utils/point'; | ||
import { AxisStyle } from '../../../../utils/themes/theme'; | ||
import { PrimitiveValue } from '../../../partition_chart/layout/utils/group_by_rollup'; | ||
import { isHorizontalRotation, isVerticalRotation } from '../../state/utils/common'; | ||
import { isHorizontalRotation } from '../../state/utils/common'; | ||
import { getAxesSpecForSpecId } from '../../state/utils/spec'; | ||
import { AxisSpec, RectAnnotationDatum, RectAnnotationSpec } from '../../utils/specs'; | ||
import { Bounds } from '../types'; | ||
|
@@ -40,7 +43,7 @@ export function computeRectAnnotationDimensions( | |
isHistogram: boolean = false, | ||
): AnnotationRectProps[] | null { | ||
const { dataValues, groupId, outside, id: annotationSpecId } = annotationSpec; | ||
const { xAxis, yAxis } = getAxesSpecForSpecId(axesSpecs, groupId); | ||
const { xAxis, yAxis } = getAxesSpecForSpecId(axesSpecs, groupId, chartRotation); | ||
const yScale = yScales.get(groupId); | ||
const rectsProps: Omit<AnnotationRectProps, 'id' | 'panel'>[] = []; | ||
const panelSize = getPanelSize(smallMultiplesScales); | ||
|
@@ -72,25 +75,19 @@ export function computeRectAnnotationDimensions( | |
return; | ||
} | ||
|
||
const hasYValues = isDefined(initialY0) || isDefined(initialY1); | ||
const outsideDim = annotationSpec.outsideDimension ?? getOutsideDimension(getAxisStyle(xAxis?.id ?? yAxis?.id)); | ||
|
||
if (!yScale) { | ||
if (!isDefined(initialY0) && !isDefined(initialY1)) { | ||
const isLeftSide = | ||
(chartRotation === 0 && xAxis?.position === Position.Bottom) || | ||
(chartRotation === 180 && xAxis?.position === Position.Top) || | ||
(chartRotation === -90 && yAxis?.position === Position.Right) || | ||
(chartRotation === 90 && yAxis?.position === Position.Left); | ||
const orthoDimension = isHorizontalRotation(chartRotation) ? panelSize.height : panelSize.width; | ||
const outsideDim = annotationSpec.outsideDimension ?? getOutsideDimension(getAxisStyle(xAxis?.id ?? yAxis?.id)); | ||
const rectDimensions = { | ||
if (!hasYValues) { | ||
// only x values present, just fill full height of chart space | ||
const rectDimensions: Rect = { | ||
...xAndWidth, | ||
...(outside | ||
? { | ||
y: isLeftSide ? orthoDimension : -outsideDim, | ||
height: outsideDim, | ||
} | ||
? getXOutsideAnnotationDimensions(panelSize, chartRotation, xAxis?.position ?? 'bottom', outsideDim) | ||
: { | ||
y: 0, | ||
height: orthoDimension, | ||
height: isHorizontalRotation(chartRotation) ? panelSize.height : panelSize.width, | ||
}), | ||
}; | ||
rectsProps.push({ | ||
|
@@ -99,10 +96,33 @@ export function computeRectAnnotationDimensions( | |
datum, | ||
}); | ||
} | ||
return; | ||
return; // cannot scale y values without a scale | ||
} | ||
|
||
const hasXValues = isDefined(initialX0) || isDefined(initialX1); | ||
|
||
if (outside) { | ||
if (hasXValues && hasYValues) { | ||
Logger.warn( | ||
`The RectAnnotation (${annotationSpecId}) was defined as outside but has both x and y values defined.`, | ||
); | ||
Comment on lines
+104
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Warns when attempting to render annotation as |
||
} else if (hasXValues) { | ||
const rectDimensions: Rect = { | ||
...xAndWidth, | ||
...getXOutsideAnnotationDimensions(panelSize, chartRotation, xAxis?.position ?? 'bottom', outsideDim), | ||
}; | ||
|
||
rectsProps.push({ | ||
specId: annotationSpecId, | ||
rect: rectDimensions, | ||
datum, | ||
}); | ||
return; | ||
} | ||
} | ||
|
||
const [y0, y1] = limitValueToDomainRange(yScale, initialY0, initialY1); | ||
|
||
// something is wrong with the data types, don't draw this annotation | ||
if (!Number.isFinite(y0) || !Number.isFinite(y1)) return; | ||
|
||
|
@@ -111,29 +131,21 @@ export function computeRectAnnotationDimensions( | |
if (Number.isNaN(scaledY1) || Number.isNaN(scaledY0)) return; | ||
|
||
height = Math.abs(scaledY0 - scaledY1); | ||
// if the annotation height is 0 override it with the height from chart dimension and if the values in the domain are the same | ||
|
||
// if the annotation height is 0, override it with the height from chart dimension and if the values in the domain are the same | ||
if (height === 0 && yScale.domain.length === 2 && yScale.domain[0] === yScale.domain[1]) { | ||
// eslint-disable-next-line prefer-destructuring | ||
height = panelSize.height; | ||
scaledY1 = 0; | ||
} | ||
|
||
const orthoDimension = isVerticalRotation(chartRotation) ? panelSize.height : panelSize.width; | ||
const isLeftSide = | ||
(chartRotation === 0 && yAxis?.position === Position.Left) || | ||
(chartRotation === 180 && yAxis?.position === Position.Right) || | ||
(chartRotation === -90 && xAxis?.position === Position.Bottom) || | ||
(chartRotation === 90 && xAxis?.position === Position.Top); | ||
const outsideDim = annotationSpec.outsideDimension ?? getOutsideDimension(getAxisStyle(xAxis?.id ?? yAxis?.id)); | ||
const rectDimensions = { | ||
...(!isDefined(initialX0) && !isDefined(initialX1) && outside | ||
? { | ||
x: isLeftSide ? -outsideDim : orthoDimension, | ||
width: outsideDim, | ||
} | ||
: xAndWidth), | ||
const rectDimensions: Rect = { | ||
...xAndWidth, | ||
y: scaledY1, | ||
height, | ||
...(outside && | ||
!(hasXValues && hasYValues) && | ||
getYOutsideAnnotationDimensions(panelSize, chartRotation, yAxis?.position ?? 'left', outsideDim)), | ||
}; | ||
|
||
rectsProps.push({ | ||
|
@@ -244,7 +256,7 @@ function maxOf(base: number, value: number | string | null | undefined): number | |
} | ||
|
||
function getOutsideDimension({ tickLine: { visible, size } }: AxisStyle): number { | ||
return visible ? size : 0; | ||
return visible ? size : 1; | ||
} | ||
|
||
/** | ||
|
@@ -259,3 +271,69 @@ export function getAnnotationRectPropsId( | |
) { | ||
return [specId, verticalValue, horizontalValue, ...Object.values(datum.coordinates), datum.details, index].join('__'); | ||
} | ||
|
||
function getXOutsideAnnotationDimensions( | ||
panelSize: Size, | ||
rotation: Rotation, | ||
axisPosition: Position, | ||
thickness: number, | ||
): Pick<Rect, 'y' | 'height'> { | ||
const { height, width } = panelSize; | ||
|
||
switch (axisPosition) { | ||
case Position.Top: | ||
return { | ||
y: rotation === 180 ? height : rotation === 90 ? width : -thickness, | ||
height: thickness, | ||
}; | ||
case Position.Bottom: | ||
return { | ||
y: rotation === 0 ? height : rotation === 90 ? width : -thickness, | ||
height: thickness, | ||
}; | ||
case Position.Left: | ||
return { | ||
y: rotation === -90 ? -thickness : width, | ||
height: thickness, | ||
}; | ||
case Position.Right: | ||
default: | ||
return { | ||
y: rotation === -90 ? width : -thickness, | ||
height: thickness, | ||
}; | ||
} | ||
} | ||
|
||
function getYOutsideAnnotationDimensions( | ||
panelSize: Size, | ||
rotation: Rotation, | ||
axisPosition: Position, | ||
thickness: number, | ||
): Pick<Rect, 'x' | 'width'> { | ||
const { height, width } = panelSize; | ||
|
||
switch (axisPosition) { | ||
case Position.Left: | ||
return { | ||
x: rotation === 180 ? width : rotation === 90 ? height : -thickness, | ||
width: thickness, | ||
}; | ||
case Position.Right: | ||
return { | ||
x: rotation === 0 ? width : rotation === 90 ? height : -thickness, | ||
width: thickness, | ||
}; | ||
case Position.Top: | ||
return { | ||
x: rotation === -90 ? height : -thickness, | ||
width: thickness, | ||
}; | ||
case Position.Bottom: | ||
default: | ||
return { | ||
x: rotation === -90 ? -thickness : height, | ||
width: thickness, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needed to match to correct axis