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(heatmap): move onBrushEnd from config to Settings #1369

Merged
merged 9 commits into from
Sep 15, 2021

Conversation

rshen91
Copy link
Contributor

@rshen91 rshen91 commented Sep 9, 2021

Summary

BREAKING CHANGE

The onBrushEnd prop is removed from the heatmap config. It can now be pulled off the Setting to match the other chart types.

Details

In settings.tsx I created a new type BrushArea:

/** @public */
export type BrushArea = XYBrushArea | HeatmapBrushEvent;

/** @public */
export type BrushEndListener = (brushArea: BrushArea) => void;

This satisfies chart_state.interactions.test.tsx and on_brushed_highlighted_shapes.test.ts

Issues

Closes #1214

Checklist

  • The proper chart type label has been added (e.g. :xy, :partition)
  • All related issues have been linked (i.e. closes #123, fixes #123)
  • New public API exports have been added to packages/charts/src/index.ts
  • Unit tests have been added or updated to match the most common scenarios
  • The proper documentation and/or storybook story has been added or updated

@rshen91 rshen91 marked this pull request as ready for review September 10, 2021 14:47
@rshen91 rshen91 added the :heatmap Heatmap/Swimlane chart related issue label Sep 10, 2021
@nickofthyme nickofthyme force-pushed the master branch 7 times, most recently from 7148acf to 604b153 Compare September 13, 2021 04:06
Comment on lines 145 to 148
export type BrushArea = XYBrushArea | HeatmapBrushEvent;

/** @public */
export type BrushEndListener = (brushArea: XYBrushArea) => void;
export type BrushEndListener = (brushArea: BrushArea) => void;
Copy link
Member

Choose a reason for hiding this comment

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

@nickofthyme @monfera @rshen91
I'd like to have your opinion here: this is not the only place where we have an union type as an argument of a chart callback.
It happens on the onClick event, but also in other situations.
Most chart consumers automatically cast to the right type depending on their needs: e.g. I'm using a bar chart so I'm cast with as XYBrushArea the brushArea.
Right now we don't offer a Generic type for the chart and I also don't fully know if and how this can work.
I'm wondering if we can, in the meantime, find a simpler and cleaner solution to the problem.
Tagging the callback argument type with the chart type could be a solution:

type HeatmapBrushEvent = {
  type: 'heatmap';
  cells: Cell[];
  x: (string | number)[];
  y: (string | number)[];
};

interface XYBrushArea {
  type: 'cartesian';
  x?: [number, number];
  y?: Array<GroupBrushExtent>;
}

what do you think?

Copy link
Collaborator

@nickofthyme nickofthyme Sep 14, 2021

Choose a reason for hiding this comment

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

So I was thinking one way to do this would be in the function somehow like this...

<Settings
  onBrushEnd={('heatmap', event: HeatmapBrushEvent) => null}
  // or with multiple
  onBrushEnd={(['line', 'area'], event: LineBrushEvent | AreaBrushEvent) => null}
/>

The issue with this is that they could pass 'heatmap when there is no HeatmapSpec defined.

<Settings
  onBrushEnd={{
    types: ['heatmap'],
    callback: (event: HeatmapBrushEvent) => null
  }}
/>

The idea behind the types is something like below... (see TS Playground)

Screen.Recording.2021-09-14.at.08.38.04.AM.mp4

Copy link
Collaborator

Choose a reason for hiding this comment

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

I found a slightly simpler way to avoid the type recursion. See demo

interface HeatmapEvent {
    type: "heatmap"
    getHeatmap: () => {}
}

interface XYEvent {
    type: "xy"
    getXY: () => {}
}
interface EventMap {
    'heatmap': HeatmapEvent;
    'xy': XYEvent;
}

// There may be a less verbose way of doing this...
type Callback<Type> = Type extends keyof EventMap ?
    EventMap[Type] : never

type HeatmapCb = Callback<'heatmap'>; // HeatmapEvent
type XYCb = Callback<'xy'>; // XYEvent
type AnyCb = Callback<'xy'|'heatmap'>; // HeatmapEvent | XYEvent

Copy link
Collaborator

@nickofthyme nickofthyme Sep 14, 2021

Choose a reason for hiding this comment

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

After looking at how we could implement this in the brush end listener, it is not currently easy to use a generic via Settings props. @rshen91 is opening an issue (#1380) to discuss a future enhancement.

Comment on lines 145 to 148
export type BrushArea = XYBrushArea | HeatmapBrushEvent;

/** @public */
export type BrushEndListener = (brushArea: XYBrushArea) => void;
export type BrushEndListener = (brushArea: BrushArea) => void;
Copy link
Collaborator

@nickofthyme nickofthyme Sep 14, 2021

Choose a reason for hiding this comment

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

So I was thinking one way to do this would be in the function somehow like this...

<Settings
  onBrushEnd={('heatmap', event: HeatmapBrushEvent) => null}
  // or with multiple
  onBrushEnd={(['line', 'area'], event: LineBrushEvent | AreaBrushEvent) => null}
/>

The issue with this is that they could pass 'heatmap when there is no HeatmapSpec defined.

<Settings
  onBrushEnd={{
    types: ['heatmap'],
    callback: (event: HeatmapBrushEvent) => null
  }}
/>

The idea behind the types is something like below... (see TS Playground)

Screen.Recording.2021-09-14.at.08.38.04.AM.mp4

CHANGELOG.md Outdated
@@ -1,31 +1,3 @@
# [35.0.0](https://github.com/elastic/elastic-charts/compare/v34.2.1...v35.0.0) (2021-09-13)
Copy link
Collaborator

@nickofthyme nickofthyme Sep 14, 2021

Choose a reason for hiding this comment

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

Ugh sorry for got about git checkout would also wipe any auto changes in between. I just git reset to the good pr with the unit test fix on top 🍒 and force pushed 🙊

@markov00
Copy link
Member

jenkins test this please

Copy link
Collaborator

@nickofthyme nickofthyme left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉

@rshen91 rshen91 merged commit 5910f7a into elastic:master Sep 15, 2021
@rshen91 rshen91 deleted the heatmap-settings branch September 15, 2021 13:08
rshen91 added a commit that referenced this pull request Sep 15, 2021
BREAKING CHANGE: remove onBrushEnd from heatmap config and merge onBrushEnd in Settings with cartesian onBrushEnd
nickofthyme pushed a commit that referenced this pull request Sep 15, 2021
# [36.0.0](v35.0.0...v36.0.0) (2021-09-15)

### Features

* **heatmap:** move onBrushEnd from config to Settings ([#1369](#1369)) ([409a0c4](409a0c4))

### BREAKING CHANGES

* **heatmap:** remove onBrushEnd from heatmap config and merge onBrushEnd in Settings with cartesian onBrushEnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:heatmap Heatmap/Swimlane chart related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move heatmap onBrushEnd callback to Settings
3 participants