Skip to content

Commit

Permalink
Turned back extent validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuznietsov committed Apr 27, 2022
1 parent fa77166 commit 5a1ce2a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,23 @@
* Side Public License, v 1.
*/

import { AxisExtentModes, XY_VIS_RENDERER } from '../constants';
import { appendLayerIds, getDataLayers } from '../helpers';
import { AxisExtentConfigResult, LayeredXyVisFn } from '../types';
import { XY_VIS_RENDERER } from '../constants';
import { appendLayerIds } from '../helpers';
import { LayeredXyVisFn } from '../types';
import { logDatatables } from '../utils';
import {
hasAreaLayer,
hasBarLayer,
isValidExtentWithCustomMode,
validateExtentForDataBounds,
} from './validate';

const getCorrectExtent = (extent: AxisExtentConfigResult, hasBarOrArea: boolean) => {
if (
extent.mode === AxisExtentModes.CUSTOM &&
hasBarOrArea &&
!isValidExtentWithCustomMode(extent)
) {
return { ...extent, lowerBound: NaN, upperBound: NaN };
}
return extent;
};

export const layeredXyVisFn: LayeredXyVisFn['fn'] = async (data, args, handlers) => {
const layers = appendLayerIds(args.layers ?? [], 'layers');

logDatatables(layers, handlers);

const dataLayers = getDataLayers(layers);

const hasBar = hasBarLayer(dataLayers);
const hasArea = hasAreaLayer(dataLayers);

const { yLeftExtent, yRightExtent } = args;

validateExtentForDataBounds(yLeftExtent, dataLayers);
validateExtentForDataBounds(yRightExtent, dataLayers);

return {
type: 'render',
as: XY_VIS_RENDERER,
value: {
args: {
...args,
layers,
yLeftExtent: getCorrectExtent(yLeftExtent, hasBar || hasArea),
yRightExtent: getCorrectExtent(yRightExtent, hasBar || hasArea),
ariaLabel:
args.ariaLabel ??
(handlers.variables?.embeddableTitle as string) ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
* Side Public License, v 1.
*/

export { appendLayerIds, getDataLayers } from './layers';
export { appendLayerIds } from './layers';
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { LayerTypes } from '../constants';
import { CommonXYDataLayerConfig, CommonXYLayerConfig, WithLayerId } from '../types';

import { WithLayerId } from '../types';

function isWithLayerId<T>(layer: T): layer is T & WithLayerId {
return (layer as T & WithLayerId).layerId ? true : false;
Expand All @@ -25,9 +25,3 @@ export function appendLayerIds<T>(
layerId: isWithLayerId(l) ? l.layerId : generateLayerId(keyword, index),
}));
}

export const isDataLayer = (layer: CommonXYLayerConfig): layer is CommonXYDataLayerConfig =>
layer.layerType === LayerTypes.DATA || !layer.layerType;

export const getDataLayers = (layers: CommonXYLayerConfig[]) =>
(layers || []).filter((layer): layer is CommonXYDataLayerConfig => isDataLayer(layer));
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,8 @@ describe('XYChart component', () => {
);
expect(component.find(Axis).find('[id="left"]').prop('domain')).toEqual({
fit: false,
min: 123,
max: 456,
min: NaN,
max: NaN,
includeDataFromIds: [],
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
getDataLayers,
Series,
getFormattedTablesByLayers,
validateExtent,
} from '../helpers';
import {
getFilteredLayers,
Expand All @@ -55,7 +56,7 @@ import { ReferenceLineAnnotations, computeChartMargins } from './reference_lines
import { visualizationDefinitions } from '../definitions';
import { CommonXYLayerConfig } from '../../common/types';
import { Annotations, getAnnotationsGroupedByInterval } from './annotations';
import { SeriesTypes, ValueLabelModes } from '../../common/constants';
import { AxisExtentModes, SeriesTypes, ValueLabelModes } from '../../common/constants';
import { DataLayers } from './data_layers';

import './xy_chart.scss';
Expand Down Expand Up @@ -304,12 +305,17 @@ export function XYChart({
return layer.seriesType.includes('bar') || layer.seriesType.includes('area');
})
);
const fit = !hasBarOrArea && extent.mode === 'dataBounds';

const fit = !hasBarOrArea && extent.mode === AxisExtentModes.DATA_BOUNDS;

let min: number = NaN;
let max: number = NaN;
if (extent.mode === 'custom') {
min = extent.lowerBound ?? NaN;
max = extent.upperBound ?? NaN;
const { inclusiveZeroError, boundaryError } = validateExtent(hasBarOrArea, extent);
if (!inclusiveZeroError && !boundaryError) {
min = extent.lowerBound ?? NaN;
max = extent.upperBound ?? NaN;
}
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import type { IFieldFormat, SerializedFieldFormat } from '@kbn/field-formats-plugin/common';
import { FormatFactory } from '../types';
import { CommonXYDataLayerConfig, ExtendedYConfig, YConfig } from '../../common';
import { AxisExtentConfig, CommonXYDataLayerConfig, ExtendedYConfig, YConfig } from '../../common';
import { isDataLayer } from './visualization';

export interface Series {
Expand Down Expand Up @@ -132,3 +132,17 @@ export function getAxesConfiguration(

return axisGroups;
}

export function validateExtent(hasBarOrArea: boolean, extent?: AxisExtentConfig) {
const inclusiveZeroError =
extent &&
hasBarOrArea &&
((extent.lowerBound !== undefined && extent.lowerBound > 0) ||
(extent.upperBound !== undefined && extent.upperBound) < 0);
const boundaryError =
extent &&
extent.lowerBound !== undefined &&
extent.upperBound !== undefined &&
extent.upperBound <= extent.lowerBound;
return { inclusiveZeroError, boundaryError };
}
30 changes: 13 additions & 17 deletions x-pack/plugins/lens/public/xy_visualization/to_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,21 +533,17 @@ const extendedYConfigToExpression = (yConfig: ExtendedYConfig, defaultColor?: st
const axisExtentConfigToExpression = (
extent: AxisExtentConfig | undefined,
layers: ValidXYDataLayerConfig[]
): Ast => {
const hasLine = layers.filter(({ seriesType }) => seriesType.includes('line')).length > 0;
const mode = !extent?.mode || (!hasLine && extent?.mode === 'dataBounds') ? 'full' : extent.mode;
return {
type: 'expression',
chain: [
{
type: 'function',
function: 'axisExtentConfig',
arguments: {
mode: [mode],
lowerBound: extent?.lowerBound !== undefined ? [extent?.lowerBound] : [],
upperBound: extent?.upperBound !== undefined ? [extent?.upperBound] : [],
},
): Ast => ({
type: 'expression',
chain: [
{
type: 'function',
function: 'axisExtentConfig',
arguments: {
mode: [extent?.mode ?? 'full'],
lowerBound: extent?.lowerBound !== undefined ? [extent?.lowerBound] : [],
upperBound: extent?.upperBound !== undefined ? [extent?.upperBound] : [],
},
],
};
};
},
],
});

0 comments on commit 5a1ce2a

Please sign in to comment.