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

feat(rect_annotation): add RectAnnotation type #180

Merged
merged 48 commits into from
May 6, 2019

Conversation

emmacunningham
Copy link
Contributor

@emmacunningham emmacunningham commented Apr 20, 2019

Summary

close #105

This PR introduces the functionality to add a RectAnnotation spec which will draw a rectangular annotation in a chart. It also refactors the Annotation specs such that both LineAnnotationSpec and RectAnnotationSpec extend the same BaseAnnotationSpec for shared properties:

BaseAnnotationSpec specifies:

export interface BaseAnnotationSpec {
  /** The id of the annotation */
  annotationId: AnnotationId;
  /** Annotation type: line, rectangle, text */
  annotationType: AnnotationType;
  /** The ID of the axis group, generated via getGroupId method
   * @default __global__
   */
  groupId: GroupId; // defaults to __global__; needed for yDomain position
  /** Data values defined with coordinates and details */
  dataValues: AnnotationDatum[];
  /** Custom annotation style */
  style?: Partial<AnnotationStyle>;
  /** Toggles tooltip annotation visibility */
  hideTooltips?: boolean;
  /** z-index of the annotation relative to other elements in the chart
   * @default 0
   */
  zIndex?: number;
}

The RectAnnotationSpec is defined as follows:

export interface RectAnnotationSpec {
  annotationType: 'rectangle';
  /** Custom rendering function for tooltip */
  renderTooltip?: (
      position: { transform: string; top: number; left: number; }, 
      details?: string
    ) => JSX.Element;
  /** Data values defined with coordinates and details */
  dataValues: RectAnnotationDatum[];
  /** Custom annotation style */
  style?: Partial<RectAnnotationStyle>;
}

where each annotation dataValue (RectAnnotationDatum) has the shape:

export interface RectAnnotationDatum {
  coordinates: {
    x0?: any;
    x1?: any;
    y0?: any;
    y1?: any;
  };
  details?: string;
}

rect_anno_rotations

We validate & coerce the coordinate values such that if no valid coordinates are found, the annotation will be skipped & not render; so long as one of the coordinates is defined, we will draw the annotation by coercing the undefined coordinates to the chart extents such that:

x0 x1 y0 y1 render implications
undefined undefined undefined undefined no rendered annotation
defined undefined (coerced to x domain start) undefined (coerced to y domain end) undefined (coerced to y domain start) rect will be drawn from xDomain start to x1 with full chart height
undefined (coerced to x domain end) defined undefined (coerced to y domain end) undefined (coerced to y domain start) rect will be drawn from x1 to x domain end with full chart height
undefined (coerced to x domain end) undefined (coerced to x domain start) defined undefined (coerced to y domain start) rect will be drawn from y domain start to y0 with full chart width
undefined (coerced to x domain end) undefined (coerced to x domain start) undefined (coerced to y domain end) defined rect will be drawn from y1 to y domain end with full chart width

defined_coord

The annotation may also be styled by passing a custom style, which allows customization of stroke, strokeWidth, opacity, and fill.

rect_anno_style

In the case where an annotation's hover tooltip is visible where there is also a chart element hover tooltip, precedence will go to the annotation tooltip unless the chart element hover has a specific element highlighted in which case the chart element tooltip will be visible instead. (This matches the behavior of the tooltip on the annotations in Discover)

rect_anno_hover

Finally, we added a new prop zIndex which controls the relative z positioning of the annotations:

zIndex

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

  • This was checked for cross-browser compatibility, including a check against IE11
  • Proper documentation or storybook story was added for features that require explanation or tutorials
  • Unit tests were updated or added to match the most common scenarios
  • Each commit follows the convention

@emmacunningham emmacunningham added enhancement New feature or request wip work in progress :annotation Annotation (line, rect, text) related issue labels Apr 20, 2019
@codecov-io
Copy link

codecov-io commented Apr 20, 2019

Codecov Report

Merging #180 into master will increase coverage by 0.2%.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master    #180     +/-   ##
========================================
+ Coverage   97.19%   97.4%   +0.2%     
========================================
  Files          35      35             
  Lines        1962    2120    +158     
  Branches      285     315     +30     
========================================
+ Hits         1907    2065    +158     
  Misses         48      48             
  Partials        7       7
Impacted Files Coverage Δ
src/lib/themes/theme.ts 100% <100%> (ø) ⬆️
src/lib/series/specs.ts 100% <100%> (ø) ⬆️
src/state/chart_state.ts 96.41% <100%> (+0.14%) ⬆️
src/state/annotation_utils.ts 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0897ff1...a56c2af. Read the comment docs.

@emmacunningham
Copy link
Contributor Author

jenkins test this

@emmacunningham emmacunningham removed the wip work in progress label Apr 22, 2019
@emmacunningham emmacunningham requested review from markov00 and removed request for markov00 April 29, 2019 15:24
Copy link
Member

@markov00 markov00 left a comment

Choose a reason for hiding this comment

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

Code LGTM. Tested locally. I've a minor/major feature that I think we need to add before release this:
I think we should include a prop to specify if the annotation is on the foreground or on the background.
For example, on the current vislib the gray band that specify partial buckets are behind the bars, but the line annotations are on the foreground.
We can introduce here the concept of z-index, and use this to render annotation above or below the chart elements.
We can than move this concept also to chart elements

@emmacunningham
Copy link
Contributor Author

@markov00 Added the zIndex prop to BaseAnnotationSpec, which will now control the ordering of how the elements. Because of how react-konva doesn't handle zIndex (and instead relies on rendering order for this), this required a change in the implementation of the annotations; they now are rendered in the same layer as the chart elements. I will leave the remaining work of ordering the chart elements to a separate PR as that will require grouping the chart elements by series & rendering the groups per zIndex on the series.

zIndex

@emmacunningham emmacunningham requested a review from markov00 May 1, 2019 16:34
@emmacunningham
Copy link
Contributor Author

jenkins test this

Copy link
Member

@markov00 markov00 left a comment

Choose a reason for hiding this comment

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

I've added few comments that I think we should address before merging. Everything else is amazing, Thanks!

@@ -10,6 +10,7 @@ export class RectAnnotationSpecComponent extends PureComponent<RectAnnotationPro
static defaultProps: Partial<RectAnnotationProps> = {
groupId: getGroupId('__global__'),
annotationType: 'rectangle',
zIndex: 0,
Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to move the rect annotation on the background as default with a zIndex of -1 or -10 (so that on our current Kibana use cases we don't have to specify the zIndex for each RectAnnotation)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 added here d9aa483

@@ -14,6 +14,7 @@ export class LineAnnotationSpecComponent extends PureComponent<LineAnnotationPro
style: DEFAULT_ANNOTATION_LINE_STYLE,
hideLines: false,
hideTooltips: false,
zIndex: 0,
Copy link
Member

Choose a reason for hiding this comment

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

We should already specify a default zIndex of 1 or 10 for lines so that our current use-cases are already satisfied bu our defaults

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added default here d9aa483

@@ -255,6 +255,10 @@ export interface BaseAnnotationSpec {
style?: Partial<AnnotationStyle>;
/** Toggles tooltip annotation visibility */
hideTooltips?: boolean;
/** z-index of the annotation relative to other elements in the chart
* @default 0
Copy link
Member

Choose a reason for hiding this comment

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

I think we should specify the default in a different way, as per my comment below we should use two different defaults, one for Line and one for Rect

Copy link
Contributor Author

Choose a reason for hiding this comment

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

have added a comment to the individual specs for RectAnnotation and LineAnnotation the different default values d9aa483

@emmacunningham emmacunningham merged commit b339318 into elastic:master May 6, 2019
markov00 pushed a commit that referenced this pull request May 6, 2019
# [4.2.0](v4.1.0...v4.2.0) (2019-05-06)

### Features

* **rect_annotation:** add RectAnnotation type ([#180](#180)) ([b339318](b339318))
@markov00
Copy link
Member

markov00 commented May 6, 2019

🎉 This PR is included in version 4.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@markov00 markov00 added the released Issue released publicly label May 6, 2019
AMoo-Miki pushed a commit to AMoo-Miki/OpenSearch-Dashboards that referenced this pull request Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:annotation Annotation (line, rect, text) related issue enhancement New feature or request released Issue released publicly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add rectangular annotation & tooltip
3 participants