Repository navigation
feat(dashboards): Hatch the background behind low confidence data #131988
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
Open
sentry-junior
wants to merge
5
commits into
master
Choose a base branch
from
feat/low-confidence-background-hatch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f0b801b
feat(dashboards): Hatch the background behind low confidence data
sentry-junior[bot] 21e7553
ref(dashboards): Draw low confidence hatching with a FullHeightAreas …
sentry-junior[bot] 2836071
fix(dashboards): Put low confidence areas on the left y axis
sentry-junior[bot] f5bb096
ref(dashboards): Pass ranges and bounds to FullHeightAreas separately
sentry-junior[bot] c5e883e
ref(dashboards): Rename FullHeightAreas to HatchAreas and trim its specs
sentry-junior[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import {mergeTimeRanges} from './mergeTimeRanges'; | ||
|
|
||
| describe('mergeTimeRanges', () => { | ||
| it('merges ranges that overlap or touch, in any order', () => { | ||
| expect( | ||
| mergeTimeRanges([ | ||
| {start: 50, end: 60}, | ||
| {start: 10, end: 20}, | ||
| {start: 20, end: 30}, | ||
| {start: 25, end: 28}, | ||
| ]) | ||
| ).toEqual([ | ||
| {start: 10, end: 30}, | ||
| {start: 50, end: 60}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('keeps ranges with a gap between them apart', () => { | ||
| expect( | ||
| mergeTimeRanges([ | ||
| {start: 0, end: 10}, | ||
| {start: 11, end: 20}, | ||
| ]) | ||
| ).toEqual([ | ||
| {start: 0, end: 10}, | ||
| {start: 11, end: 20}, | ||
| ]); | ||
| }); | ||
|
|
||
| it('does not change the ranges it was given', () => { | ||
| const ranges = [ | ||
| {start: 0, end: 10}, | ||
| {start: 5, end: 20}, | ||
| ]; | ||
|
|
||
| mergeTimeRanges(ranges); | ||
|
|
||
| expect(ranges).toEqual([ | ||
| {start: 0, end: 10}, | ||
| {start: 5, end: 20}, | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export interface TimeRange { | ||
| end: number; | ||
| start: number; | ||
| } | ||
|
|
||
| /** | ||
| * Sorts ranges by start and merges any that overlap or touch. | ||
| */ | ||
| export function mergeTimeRanges(ranges: readonly TimeRange[]): TimeRange[] { | ||
| const merged: TimeRange[] = []; | ||
|
|
||
| for (const range of ranges.toSorted((a, b) => a.start - b.start)) { | ||
| const previous = merged.at(-1); | ||
| if (previous && range.start <= previous.end) { | ||
| previous.end = Math.max(previous.end, range.end); | ||
| } else { | ||
| merged.push({...range}); | ||
| } | ||
| } | ||
|
|
||
| return merged; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
static/app/views/dashboards/widgets/timeSeriesWidget/plottables/hatchAreas.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import type {CustomSeriesRenderItemAPI, CustomSeriesRenderItemParams} from 'echarts'; | ||
|
|
||
| import {HatchAreas} from './hatchAreas'; | ||
|
|
||
| describe('HatchAreas', () => { | ||
| const config = {name: 'areas', color: '#f0f0f2', dataType: 'number' as const}; | ||
|
|
||
| it('clips areas to the bounds it is plotted with', () => { | ||
| const areas = new HatchAreas( | ||
| [ | ||
| {start: -10, end: 10}, | ||
| {start: 40, end: 120}, | ||
| {start: 150, end: 160}, | ||
| ], | ||
| config | ||
| ); | ||
|
|
||
| const [series] = areas.toSeries({bounds: {start: 0, end: 100}}); | ||
|
|
||
| expect(series?.data).toEqual([ | ||
| [0, 10], | ||
| [40, 100], | ||
| ]); | ||
| }); | ||
|
|
||
| it('is empty without ranges', () => { | ||
| expect(new HatchAreas([], config).isEmpty).toBe(true); | ||
| }); | ||
|
|
||
| it('draws a hatched area across the full plot height behind the data', () => { | ||
| const [series] = new HatchAreas([{start: 1_000, end: 3_000}], config).toSeries(); | ||
|
|
||
| expect(series).toEqual( | ||
| expect.objectContaining({ | ||
| data: [[1_000, 3_000]], | ||
| encode: {x: [0, 1]}, | ||
| silent: true, | ||
| tooltip: {show: false}, | ||
| z: 1, | ||
| }) | ||
| ); | ||
|
|
||
| const values = [1_000, 3_000]; | ||
| const element = series!.renderItem!( | ||
| { | ||
| dataIndex: 0, | ||
| coordSys: {type: 'cartesian2d', x: 0, y: 10, width: 500, height: 200}, | ||
| } as unknown as CustomSeriesRenderItemParams, | ||
| { | ||
| value: (dimension: number) => values[dimension], | ||
| coord: ([time]: number[]) => [(time ?? 0) / 10, 0], | ||
| } as unknown as CustomSeriesRenderItemAPI | ||
| ); | ||
|
|
||
| expect(element).toEqual( | ||
| expect.objectContaining({ | ||
| type: 'rect', | ||
| shape: {x: 100, y: 10, width: 200, height: 200}, | ||
| style: expect.objectContaining({ | ||
| decal: expect.objectContaining({color: '#f0f0f2'}), | ||
| }), | ||
| }) | ||
| ); | ||
| }); | ||
| }); |
146 changes: 146 additions & 0 deletions
146
static/app/views/dashboards/widgets/timeSeriesWidget/plottables/hatchAreas.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import type {CustomSeriesOption, CustomSeriesRenderItem} from 'echarts'; | ||
|
|
||
| import type {CartesianCoordSys} from 'sentry/components/charts/bands/bandShapes'; | ||
| import {defined} from 'sentry/utils/defined'; | ||
| import type {TimeRange} from 'sentry/utils/timeSeries/mergeTimeRanges'; | ||
|
|
||
| import type { | ||
| Plottable, | ||
| PlottableConfidence, | ||
| PlottableTimeSeriesValueType, | ||
| } from './plottable'; | ||
|
|
||
| type HatchAreasConfig = { | ||
| /** | ||
| * Hatch color. Areas are hatched over a transparent fill, so grid lines show | ||
| * through the gaps between stripes | ||
| */ | ||
| color: string; | ||
| /** | ||
| * Type of the Y axis to draw against. The areas don't read Y values, but the | ||
| * chart puts every plottable on an axis by its type, so this should match an | ||
| * axis the chart already has | ||
| */ | ||
| dataType: PlottableTimeSeriesValueType; | ||
| /** | ||
| * Identifies the plottable. It becomes the ECharts series ID and name, so it | ||
| * must be unique among the chart's plottables | ||
| */ | ||
| name: string; | ||
| }; | ||
|
|
||
| type HatchAreasPlottingOptions = { | ||
| /** | ||
| * Areas are clipped to these bounds. The X axis grows to fit any series that | ||
| * runs past the plotted data, so pass the data's first and last timestamps | ||
| */ | ||
| bounds?: TimeRange; | ||
| }; | ||
|
|
||
| /** | ||
| * Hatched areas that span the full height of the plot over stretches of time, drawn | ||
| * behind the data. Unlike time series plottables, it has no data of its own, | ||
| * so it never takes up a Y axis, a legend item, or a tooltip row. | ||
| */ | ||
| export class HatchAreas implements Plottable { | ||
| ranges: readonly TimeRange[]; | ||
| config: Readonly<HatchAreasConfig>; | ||
|
|
||
| // Required by `Plottable`. The areas have no data, so none of it is extrapolated | ||
| confidence: PlottableConfidence = 'full'; | ||
| dataUnit = null; | ||
| needsColor = false; | ||
|
|
||
| /** | ||
| * @param ranges Sorted ranges that don't overlap, such as the output of `mergeTimeRanges` | ||
| */ | ||
| constructor(ranges: readonly TimeRange[], config: HatchAreasConfig) { | ||
| this.ranges = ranges; | ||
| this.config = config; | ||
| } | ||
|
|
||
| get name(): string { | ||
| return this.config.name; | ||
| } | ||
|
|
||
| get dataType(): PlottableTimeSeriesValueType { | ||
| return this.config.dataType; | ||
| } | ||
|
|
||
| get label(): string { | ||
| return this.config.name; | ||
| } | ||
|
|
||
| get isEmpty(): boolean { | ||
| return this.ranges.length === 0; | ||
| } | ||
|
|
||
| get start(): number | null { | ||
| return this.ranges.at(0)?.start ?? null; | ||
| } | ||
|
|
||
| get end(): number | null { | ||
| return this.ranges.at(-1)?.end ?? null; | ||
| } | ||
|
|
||
| toSeries({bounds}: HatchAreasPlottingOptions = {}): CustomSeriesOption[] { | ||
| const ranges = bounds | ||
| ? this.ranges | ||
| .map(({start, end}) => ({ | ||
| start: Math.max(start, bounds.start), | ||
| end: Math.min(end, bounds.end), | ||
| })) | ||
| .filter(({start, end}) => start < end) | ||
| : this.ranges; | ||
|
|
||
| return [ | ||
| { | ||
| id: this.name, | ||
| name: this.name, | ||
| type: 'custom', | ||
| data: ranges.map(({start, end}) => [start, end]), | ||
| // Mapping only `x` keeps the timestamps out of the Y axis extent | ||
| encode: {x: [0, 1]}, | ||
| renderItem: this.renderItem, | ||
| // Behind line, area, and bar series, which default to 2 | ||
| z: 1, | ||
| clip: true, | ||
| silent: true, | ||
| animation: false, | ||
| legendHoverLink: false, | ||
| tooltip: {show: false}, | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| // A custom series rather than a `markArea`, because only custom series render | ||
| // a `decal`, which ECharts redraws at the device pixel ratio so the stripes | ||
| // stay crisp | ||
| private renderItem: CustomSeriesRenderItem = (params, api) => { | ||
| const [left] = api.coord([api.value(0), 0]); | ||
| const [right] = api.coord([api.value(1), 0]); | ||
| if (!defined(left) || !defined(right)) { | ||
| return null; | ||
| } | ||
|
|
||
| const {y, height} = params.coordSys as CartesianCoordSys; | ||
|
|
||
| return { | ||
| type: 'rect', | ||
| silent: true, | ||
| shape: {x: left, y, width: right - left, height}, | ||
| style: { | ||
| // A decal only draws over a fill | ||
| fill: 'transparent', | ||
| decal: { | ||
| symbol: 'rect', | ||
| symbolSize: 1, | ||
| dashArrayX: [1, 0], | ||
| dashArrayY: [2, 2], | ||
| rotation: -Math.PI / 4, | ||
| color: this.config.color, | ||
| }, | ||
| }, | ||
| }; | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.