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

fix(annotations): provide fallback for line annotation markers #1091

Merged
merged 12 commits into from
Apr 2, 2021
Merged
42 changes: 28 additions & 14 deletions .playground/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,41 @@

import React from 'react';

import { Chart, BarSeries, ScaleType, Settings } from '../src';
import { Chart, BarSeries, ScaleType, LineAnnotation, AnnotationDomainTypes, LineAnnotationDatum } from '../src';

function generateAnnotationData(values: any[]): LineAnnotationDatum[] {
return values.map((value, index) => ({ dataValue: value, details: `detail-${index}` }));
}
export class Playground extends React.Component {
render() {
return (
<div className="story-chart story-root root">
<div className="App">
<Chart size={[500, 200]}>
<Settings showLegend showLegendExtra />
<LineAnnotation
domainType={AnnotationDomainTypes.XDomain}
id="ann"
dataValues={[{ dataValue: 'bags' }]}
marker={<div style={{ background: 'red' }}>hello</div>}
// markerPosition="top"
/>
<LineAnnotation
domainType={AnnotationDomainTypes.YDomain}
id="ann1"
dataValues={generateAnnotationData([30])}
marker={<div style={{ background: 'yellow' }}>Horizontal</div>}
// markerPosition="right"
/>
<BarSeries
id="areas"
name="area"
xScaleType={ScaleType.Linear}
yScaleType={ScaleType.Linear}
xAccessor={0}
yAccessors={[1]}
splitSeriesAccessors={[2]}
id="bars"
name="amount"
xScaleType={ScaleType.Ordinal}
xAccessor="x"
yAccessors={['y']}
data={[
[0, 123, 'group0'],
[0, 123, 'group1'],
[0, 123, 'group2'],
[0, 123, 'group3'],
{ x: 'trousers', y: 390, val: 1222 },
{ x: 'watches', y: 23, val: 1222 },
{ x: 'bags', y: 750, val: 1222 },
{ x: 'cocktail dresses', y: 854, val: 1222 },
]}
/>
</Chart>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion src/chart_types/xy_chart/annotations/line/dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,12 @@ function getAnchorPosition(
isXDomain: boolean,
isChartHorizontal: boolean,
specMarkerPosition?: Position,
axisPosition?: Position,
axisPosition?: Position | undefined,
Copy link
Member

Choose a reason for hiding this comment

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

this argument is already optional when using the ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed in e58089a thank you!

): Position {
// catch the case of a chart with no axes nor specMarkerPosition for a vertical line annotation
if (!axisPosition && !specMarkerPosition && isXDomain) {
return Position.Bottom;
}
Copy link
Member

@markov00 markov00 Mar 29, 2021

Choose a reason for hiding this comment

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

Can you please check the getDefaultMarkerPositionFromAxis function? I see that this also covers the case where the axisPosition is undefined but I don't know if we are considering all the cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you! 2b4f5de

const dflPositionFromAxis = getDefaultMarkerPositionFromAxis(isXDomain, isChartHorizontal, axisPosition);

if (specMarkerPosition !== undefined) {
Expand Down
80 changes: 80 additions & 0 deletions stories/test_cases/3_no_axes_annotation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { select } from '@storybook/addon-knobs';
import React from 'react';

import {
LineAnnotationDatum,
Chart,
LineAnnotation,
AnnotationDomainTypes,
BarSeries,
ScaleType,
Position,
} from '../../src';

function generateAnnotationData(values: any[]): LineAnnotationDatum[] {
return values.map((value, index) => ({ dataValue: value, details: `detail-${index}` }));
}

export const Example = () => {
const markerPositionHorizontal = select(
'horizontal marker position',
[Position.Top, Position.Left, Position.Bottom, Position.Right, 'undefined'],
'undefined',
);
const markerPositionVertical = select(
'vertical marker position',
[Position.Top, Position.Left, Position.Bottom, Position.Right, 'undefined'],
'undefined',
);
return (
<Chart className="story-chart">
<LineAnnotation
domainType={AnnotationDomainTypes.XDomain}
id="ann"
dataValues={[{ dataValue: 'bags' }]}
marker={<div style={{ background: 'red' }}>Vertical</div>}
markerPosition={markerPositionVertical === 'undefined' ? undefined : markerPositionVertical}
/>
<LineAnnotation
domainType={AnnotationDomainTypes.YDomain}
id="ann1"
dataValues={generateAnnotationData([30])}
marker={<div style={{ background: 'yellow' }}>Horizontal</div>}
markerPosition={markerPositionHorizontal === 'undefined' ? undefined : markerPositionHorizontal}
/>

<BarSeries
id="bars"
name="amount"
xScaleType={ScaleType.Ordinal}
xAccessor="x"
yAccessors={['y']}
data={[
{ x: 'trousers', y: 390, val: 1222 },
{ x: 'watches', y: 23, val: 1222 },
{ x: 'bags', y: 750, val: 1222 },
{ x: 'cocktail dresses', y: 854, val: 1222 },
]}
/>
</Chart>
);
};
1 change: 1 addition & 0 deletions stories/test_cases/test_cases.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export default {

export { Example as noSeries } from './1_no_series';
export { Example as chromePathBugFix } from './2_chrome_path_bug_fix';
export { Example as noAxesAnnotationBugFix } from './3_no_axes_annotation';