From ba806b1a8c40f1afdaba3d2d61f2c0cf0a95c17e Mon Sep 17 00:00:00 2001 From: Emma Cunningham Date: Mon, 8 Apr 2019 07:32:10 -0700 Subject: [PATCH] feat(line_annotation): add hideLines and hideTooltips props to spec (#154) --- src/lib/series/specs.ts | 4 +++ src/specs/line_annotation.tsx | 2 ++ src/state/annotation_utils.test.ts | 58 ++++++++++++++++++++++++++++-- src/state/annotation_utils.ts | 8 +++-- stories/annotations.tsx | 5 +++ 5 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/lib/series/specs.ts b/src/lib/series/specs.ts index d8c1c659de..2db8acae91 100644 --- a/src/lib/series/specs.ts +++ b/src/lib/series/specs.ts @@ -206,6 +206,10 @@ export interface LineAnnotationSpec { width: number; height: number; }; + /** Annotation lines are hidden */ + hideLines?: boolean; + /** Annotation tooltips are hidden */ + hideTooltips?: boolean; } // TODO: RectangleAnnotationSpec & TextAnnotationSpec diff --git a/src/specs/line_annotation.tsx b/src/specs/line_annotation.tsx index 61b36c66a1..5527d1fc7c 100644 --- a/src/specs/line_annotation.tsx +++ b/src/specs/line_annotation.tsx @@ -12,6 +12,8 @@ export class LineAnnotationSpecComponent extends PureComponent(); diff --git a/src/state/annotation_utils.test.ts b/src/state/annotation_utils.test.ts index 8855f59866..716e7f08d1 100644 --- a/src/state/annotation_utils.test.ts +++ b/src/state/annotation_utils.test.ts @@ -525,7 +525,7 @@ describe('annotation utils', () => { expect(dimensions).toEqual(expectedDimensions); }); - test('should not compute annotation line values for values outside of domain', () => { + test('should not compute annotation line values for values outside of domain or AnnotationSpec.hideLines', () => { const chartRotation: Rotation = 0; const yScales: Map = new Map(); yScales.set(groupId, continuousScale); @@ -652,6 +652,27 @@ describe('annotation utils', () => { ); expect(invalidStringYDimensions).toEqual([]); + + const validHiddenAnnotation: AnnotationSpec = { + annotationType: AnnotationTypes.Line, + annotationId, + domainType: AnnotationDomainTypes.XDomain, + dataValues: [{ dataValue: 2, details: 'foo' }], + groupId, + style: DEFAULT_ANNOTATION_LINE_STYLE, + hideLines: true, + }; + + const hiddenAnnotationDimensions = computeLineAnnotationDimensions( + validHiddenAnnotation, + chartDimensions, + chartRotation, + yScales, + continuousScale, + Position.Right, + ); + + expect(hiddenAnnotationDimensions).toEqual(null); }); test('should compute if a point is within an annotation line bounds (xDomain annotation)', () => { @@ -967,7 +988,7 @@ describe('annotation utils', () => { annotationDimensions.set(annotationId, annotationLines); // missing annotations - const missingTooltipState = computeAnnotationTooltipState( + const missingSpecTooltipState = computeAnnotationTooltipState( cursorPosition, annotationDimensions, annotations, @@ -975,12 +996,43 @@ describe('annotation utils', () => { localAxesSpecs, ); - expect(missingTooltipState).toBe(null); + expect(missingSpecTooltipState).toBe(null); // add valid annotation axis annotations.set(annotationId, lineAnnotation); localAxesSpecs.set(verticalAxisSpec.id, verticalAxisSpec); + // hide tooltipState + lineAnnotation.hideTooltips = true; + + const hideTooltipState = computeAnnotationTooltipState( + cursorPosition, + annotationDimensions, + annotations, + chartRotation, + localAxesSpecs, + ); + + expect(hideTooltipState).toBe(null); + + // show tooltipState, hide lines + lineAnnotation.hideTooltips = false; + lineAnnotation.hideLines = true; + + const hideLinesTooltipState = computeAnnotationTooltipState( + cursorPosition, + annotationDimensions, + annotations, + chartRotation, + localAxesSpecs, + ); + + expect(hideLinesTooltipState).toBe(null); + + // show tooltipState & lines + lineAnnotation.hideTooltips = false; + lineAnnotation.hideLines = false; + const tooltipState = computeAnnotationTooltipState( cursorPosition, annotationDimensions, diff --git a/src/state/annotation_utils.ts b/src/state/annotation_utils.ts index 435f76c2e8..bfe86e811d 100644 --- a/src/state/annotation_utils.ts +++ b/src/state/annotation_utils.ts @@ -249,7 +249,11 @@ export function computeLineAnnotationDimensions( xScale: Scale, axisPosition: Position, ): AnnotationLineProps[] | null { - const { domainType, dataValues, marker, markerDimensions } = annotationSpec; + const { domainType, dataValues, marker, markerDimensions, hideLines } = annotationSpec; + + if (hideLines) { + return null; + } // TODO : make line overflow configurable via prop const lineOverflow = DEFAULT_LINE_OVERFLOW; @@ -579,7 +583,7 @@ export function computeAnnotationTooltipState( ): AnnotationTooltipState | null { for (const [annotationId, annotationDimension] of annotationDimensions) { const spec = annotationSpecs.get(annotationId); - if (!spec) { + if (!spec || spec.hideTooltips || spec.hideLines) { continue; } diff --git a/stories/annotations.tsx b/stories/annotations.tsx index 8c280df806..97b7da40c8 100644 --- a/stories/annotations.tsx +++ b/stories/annotations.tsx @@ -258,6 +258,9 @@ storiesOf('Annotations', module) questionInCircle: 'questionInCircle', }, 'alert'); + const hideLines = boolean('annotation lines hidden', false); + const hideTooltips = boolean('annotation tooltips hidden', false); + return ( @@ -267,6 +270,8 @@ storiesOf('Annotations', module) dataValues={dataValues} style={style} marker={()} + hideLines={hideLines} + hideTooltips={hideTooltips} />