Skip to content

Commit

Permalink
Unified the two different approaches of referenceLines.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuznietsov committed May 18, 2022
1 parent 7f06310 commit fd72a67
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@
*/

import React, { FC } from 'react';
import { euiLightVars } from '@kbn/ui-theme';
import { AnnotationDomainType, LineAnnotation, Position, RectAnnotation } from '@elastic/charts';
import { Position } from '@elastic/charts';
import { FieldFormat } from '@kbn/field-formats-plugin/common';
import { ReferenceLineConfig, ReferenceLineYConfig } from '../../../common/types';
import {
getBottomRect,
getGroupId,
getHorizontalRect,
getLineAnnotationProps,
getSharedStyle,
} from './utils';
import { ReferenceLineConfig } from '../../../common/types';
import { getGroupId } from './utils';
import { ReferenceLineAnnotations } from './reference_line_annotations';

interface ReferenceLineProps {
layer: ReferenceLineConfig;
Expand All @@ -27,17 +21,6 @@ interface ReferenceLineProps {
isHorizontal: boolean;
}

const getRectDataValue = (yConfig: ReferenceLineYConfig, formatter: FieldFormat | undefined) => {
const { name, value, fill } = yConfig;
const isFillAbove = fill === 'above';

if (yConfig.axisMode === 'bottom') {
return getBottomRect(name, isFillAbove, formatter, value);
}

return getHorizontalRect(name, isFillAbove, formatter, value);
};

export const ReferenceLine: FC<ReferenceLineProps> = ({
layer,
axesMap,
Expand All @@ -46,78 +29,28 @@ export const ReferenceLine: FC<ReferenceLineProps> = ({
isHorizontal,
}) => {
const {
lineLength,
yConfig: [yConfig],
} = layer;

if (!yConfig) {
return null;
}
const { name, value, axisMode } = yConfig;

const { axisMode, value } = yConfig;

// Find the formatter for the given axis
const groupId = getGroupId(axisMode);

const formatter = formatters[groupId || 'bottom'];
const id = `${layer.layerId}-${value}`;

const defaultColor = euiLightVars.euiColorDarkShade;

const props = getLineAnnotationProps(
yConfig,
yConfig.textVisibility
? {
markerLabel: yConfig.icon ? undefined : name,
markerBodyLabel: yConfig.icon ? name : undefined,
}
: {},
axesMap,
paddingMap,
groupId,
isHorizontal
);

const sharedStyle = getSharedStyle(yConfig);

const dataValuesSample = {
dataValue: value,
header: name,
details: formatter?.convert(value) || value,
};

const dataValues = new Array(lineLength).fill(dataValuesSample);

const line = (
<LineAnnotation
{...props}
id={`${layer.layerId}-${value}-line`}
key={`${layer.layerId}-${value}-line`}
dataValues={dataValues}
domainType={
yConfig.axisMode === 'bottom' ? AnnotationDomainType.XDomain : AnnotationDomainType.YDomain
}
style={{ line: { ...sharedStyle, opacity: 1 } }}
/>
);

let rect;
if (yConfig.fill && yConfig.fill !== 'none') {
const rectDataValuesSample = getRectDataValue(yConfig, formatter);
const rectDataValues = new Array(lineLength).fill(rectDataValuesSample);

rect = (
<RectAnnotation
{...props}
id={`${layer.layerId}-${value}-rect`}
key={`${layer.layerId}-${value}-rect`}
dataValues={rectDataValues}
style={{ ...sharedStyle, fill: yConfig.color || defaultColor, opacity: 0.1 }}
/>
);
}
return (
<>
{line}
{rect}
</>
<ReferenceLineAnnotations
config={{ id, ...yConfig }}
paddingMap={paddingMap}
axesMap={axesMap}
formatter={formatter}
isHorizontal={isHorizontal}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* 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 { AnnotationDomainType, LineAnnotation, Position, RectAnnotation } from '@elastic/charts';
import { euiLightVars } from '@kbn/ui-theme';
import React, { FC } from 'react';
import { FieldFormat } from '@kbn/field-formats-plugin/common';
import { LINES_MARKER_SIZE } from '../../helpers';
import {
AvailableReferenceLineIcon,
FillStyle,
IconPosition,
LineStyle,
YAxisMode,
} from '../../../common/types';
import {
getBaseIconPlacement,
getBottomRect,
getGroupId,
getHorizontalRect,
getLineAnnotationProps,
getSharedStyle,
} from './utils';

export interface ReferenceLineAnnotationConfig {
id: string;
name?: string;
value: number;
nextValue?: number;
icon?: AvailableReferenceLineIcon;
lineWidth?: number;
lineStyle?: LineStyle;
fill?: FillStyle;
iconPosition?: IconPosition;
textVisibility?: boolean;
axisMode?: YAxisMode;
color?: string;
}

interface Props {
config: ReferenceLineAnnotationConfig;
paddingMap: Partial<Record<Position, number>>;
formatter?: FieldFormat;
axesMap: Record<'left' | 'right', boolean>;
isHorizontal: boolean;
}

const getRectDataValue = (
annotationConfig: ReferenceLineAnnotationConfig,
formatter: FieldFormat | undefined
) => {
const { name, value, nextValue, fill, axisMode } = annotationConfig;
const isFillAbove = fill === 'above';

if (axisMode === 'bottom') {
return getBottomRect(name, isFillAbove, formatter, value, nextValue);
}

return getHorizontalRect(name, isFillAbove, formatter, value, nextValue);
};

export const ReferenceLineAnnotations: FC<Props> = ({
config,
axesMap,
formatter,
paddingMap,
isHorizontal,
}) => {
const { id, axisMode, iconPosition, name, textVisibility, value, fill, color } = config;

// Find the formatter for the given axis
const groupId = getGroupId(axisMode);
const defaultColor = euiLightVars.euiColorDarkShade;
// get the position for vertical chart
const markerPositionVertical = getBaseIconPlacement(iconPosition, axesMap, axisMode);
// the padding map is built for vertical chart
const hasReducedPadding = paddingMap[markerPositionVertical] === LINES_MARKER_SIZE;

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

const sharedStyle = getSharedStyle(config);

const dataValues = {
dataValue: value,
header: name,
details: formatter?.convert(value) || value.toString(),
};

const line = (
<LineAnnotation
{...props}
id={`${id}-line`}
key={`${id}-line`}
dataValues={[dataValues]}
domainType={
axisMode === 'bottom' ? AnnotationDomainType.XDomain : AnnotationDomainType.YDomain
}
style={{ line: { ...sharedStyle, opacity: 1 } }}
/>
);

let rect;
if (fill && fill !== 'none') {
const rectDataValues = getRectDataValue(config, formatter);

rect = (
<RectAnnotation
{...props}
id={`${id}-rect`}
key={`${id}-rect`}
dataValues={[rectDataValues]}
style={{ ...sharedStyle, fill: color || defaultColor, opacity: 0.1 }}
/>
);
}
return (
<>
{line}
{rect}
</>
);
};
Loading

0 comments on commit fd72a67

Please sign in to comment.