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

[XY] Add xExtent validation within expression #134546

Merged
merged 3 commits into from
Jun 17, 2022
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 @@ -115,10 +115,6 @@ export const createArgsWithLayers = (
yLeft: false,
yRight: false,
},
xExtent: {
mode: 'dataBounds',
type: 'axisExtentConfig',
},
yLeftExtent: {
mode: 'full',
type: 'axisExtentConfig',
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ export const errors = {
i18n.translate('expressionXY.reusable.function.xyVis.errors.dataBoundsForNotLineChartError', {
defaultMessage: 'Only line charts can be fit to the data bounds',
}),
extentFullModeIsInvalidError: () =>
i18n.translate('expressionXY.reusable.function.xyVis.errors.extentFullModeIsInvalid', {
defaultMessage: 'For x axis extent, the full mode is not supported.',
}),
extentModeNotSupportedError: () =>
i18n.translate('expressionXY.reusable.function.xyVis.errors.extentModeNotSupportedError', {
defaultMessage: 'X axis extent is only supported for numeric histograms.',
}),
timeMarkerForNotTimeChartsError: () =>
i18n.translate('expressionXY.reusable.function.xyVis.errors.timeMarkerForNotTimeChartsError', {
defaultMessage: 'Only time charts can have current time marker',
Expand Down Expand Up @@ -136,6 +144,20 @@ export const validateExtentForDataBounds = (
}
};

export const validateXExtent = (
extent: AxisExtentConfigResult | undefined,
Copy link
Contributor

@VladLasitsa VladLasitsa Jun 17, 2022

Choose a reason for hiding this comment

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

nit: extent?: AxisExtentConfigResult

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have to invert the arguments order to set the optional argument

export const validateXExtent = (
  dataLayers: Array<DataLayerConfigResult | CommonXYDataLayerConfig>,
  extent?: AxisExtentConfigResult
) => {
...

Usually I prefer to have as first argument what it needs to be validate, and as subsequent arguments supporting utilities/objects.

dataLayers: Array<DataLayerConfigResult | CommonXYDataLayerConfig>
) => {
if (extent) {
if (extent.mode === AxisExtentModes.FULL) {
throw new Error(errors.extentFullModeIsInvalidError());
}
if (isTimeChart(dataLayers) || dataLayers.every(({ isHistogram }) => !isHistogram)) {
throw new Error(errors.extentModeNotSupportedError());
}
}
};

export const validateExtent = (
extent: AxisExtentConfigResult,
hasBarOrArea: boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,129 @@ describe('xyVis', () => {
)
).rejects.toThrowErrorMatchingSnapshot();
});

test('throws the error if the x axis extent is enabled for a date histogram', async () => {
const {
data,
args: { layers, ...rest },
} = sampleArgs();
const { layerId, layerType, table, type, ...restLayerArgs } = sampleLayer;

expect(
xyVisFunction.fn(
data,
{
...rest,
...restLayerArgs,
referenceLines: [],
annotationLayers: [],
isHistogram: true,
xScaleType: 'time',
xExtent: { type: 'axisExtentConfig', mode: 'dataBounds' },
},
createMockExecutionContext()
)
).rejects.toThrowErrorMatchingSnapshot();
});

test('throws the error if the x axis extent is enabled with the full mode', async () => {
const {
data,
args: { layers, ...rest },
} = sampleArgs();
const { layerId, layerType, table, type, ...restLayerArgs } = sampleLayer;

expect(
xyVisFunction.fn(
data,
{
...rest,
...restLayerArgs,
referenceLines: [],
annotationLayers: [],
xExtent: {
type: 'axisExtentConfig',
mode: 'full',
lowerBound: undefined,
upperBound: undefined,
},
},
createMockExecutionContext()
)
).rejects.toThrowErrorMatchingSnapshot();
});

test('throws the error if the x axis extent is enabled without a histogram defined', async () => {
const {
data,
args: { layers, ...rest },
} = sampleArgs();
const { layerId, layerType, table, type, ...restLayerArgs } = sampleLayer;

expect(
xyVisFunction.fn(
data,
{
...rest,
...restLayerArgs,
referenceLines: [],
annotationLayers: [],
xExtent: {
type: 'axisExtentConfig',
mode: 'dataBounds',
},
},
createMockExecutionContext()
)
).rejects.toThrowErrorMatchingSnapshot();
});

test('it renders with custom xExtent for a numeric histogram', async () => {
const { data, args } = sampleArgs();
const { layers, ...rest } = args;
const { layerId, layerType, table, type, ...restLayerArgs } = sampleLayer;
const result = await xyVisFunction.fn(
data,
{
...rest,
...restLayerArgs,
referenceLines: [],
annotationLayers: [],
isHistogram: true,
xExtent: {
type: 'axisExtentConfig',
mode: 'custom',
lowerBound: 0,
upperBound: 10,
},
},
createMockExecutionContext()
);

expect(result).toEqual({
type: 'render',
as: XY_VIS,
value: {
args: {
...rest,
xExtent: {
type: 'axisExtentConfig',
mode: 'custom',
lowerBound: 0,
upperBound: 10,
},
layers: [
{
layerType,
table: data,
layerId: 'dataLayers-0',
type,
...restLayerArgs,
isHistogram: true,
},
],
},
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
validateLineWidthForChartType,
validatePointsRadiusForChartType,
validateLinesVisibilityForChartType,
validateXExtent,
} from './validate';

const createDataLayer = (args: XYArgs, table: Datatable): DataLayerConfigResult => {
Expand Down Expand Up @@ -120,6 +121,7 @@ export const xyVisFn: XyVisFn['fn'] = async (data, args, handlers) => {
const hasBar = hasBarLayer(dataLayers);
const hasArea = hasAreaLayer(dataLayers);

validateXExtent(args.xExtent, dataLayers);
validateExtent(args.yLeftExtent, hasBar || hasArea, dataLayers);
validateExtent(args.yRightExtent, hasBar || hasArea, dataLayers);
validateFillOpacity(args.fillOpacity, hasArea);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export interface XYArgs extends DataLayerArgs {
xTitle: string;
yTitle: string;
yRightTitle: string;
xExtent: AxisExtentConfigResult;
xExtent?: AxisExtentConfigResult;
yLeftExtent: AxisExtentConfigResult;
yRightExtent: AxisExtentConfigResult;
yLeftScale: YScaleType;
Expand Down Expand Up @@ -231,7 +231,7 @@ export interface LayeredXYArgs {
xTitle: string;
yTitle: string;
yRightTitle: string;
xExtent: AxisExtentConfigResult;
xExtent?: AxisExtentConfigResult;
yLeftExtent: AxisExtentConfigResult;
yRightExtent: AxisExtentConfigResult;
yLeftScale: YScaleType;
Expand Down Expand Up @@ -263,7 +263,7 @@ export interface XYProps {
xTitle: string;
yTitle: string;
yRightTitle: string;
xExtent: AxisExtentConfigResult;
xExtent?: AxisExtentConfigResult;
yLeftExtent: AxisExtentConfigResult;
yRightExtent: AxisExtentConfigResult;
yLeftScale: YScaleType;
Expand Down