-
Notifications
You must be signed in to change notification settings - Fork 28
s2 line hover labels #828
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
Merged
Merged
s2 line hover labels #828
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e9fb733
feat: S2 Line hove value label
7301f4f
fix: update double dip for dimension
tanugarg04 ec7977c
Merge branch AN-450446 (hover labels) into feat/s2-line-hover-labels …
madelineluke c0c69c5
feat: WIP adding cascading formula to the s2 line hover label
madelineluke 5044ca9
feat: updating test values, adding series color to label
madelineluke 9d1c2e6
feat: update label sizing to match font size, slight shift when hover…
madelineluke 3cc23a8
Merge branch 'main' of personal.github.com:adobe/react-spectrum-chart…
madelineluke 56595fc
chore: update documentation
madelineluke fe80bb0
chore: fixing code smells but simplifying logic
madelineluke f28e448
chore: add more test coverage for dimension hover code
madelineluke 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 |
|---|---|---|
|
|
@@ -35,4 +35,5 @@ cursor | |
| # catch all for temporary outputs | ||
| tmp/ | ||
|
|
||
| .agents/ | ||
| .scout/ | ||
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
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
98 changes: 98 additions & 0 deletions
98
...es/react-spectrum-charts-s2/src/stories/Line/Features/HoverLabel/LineHoverLabel.story.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,98 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed 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 REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
| import { ReactElement } from 'react'; | ||
|
|
||
| import { StoryFn } from '@storybook/react'; | ||
|
|
||
| import { Chart } from '../../../../Chart'; | ||
| import { Axis, Legend, Line } from '../../../../components'; | ||
| import useChartProps from '../../../../hooks/useChartProps'; | ||
| import { workspaceTrendsData } from '../../../data/data'; | ||
| import { bindWithProps } from '../../../../test-utils'; | ||
| import { ChartProps } from '../../../../types'; | ||
|
|
||
| const DATETIMES = [1667890800000, 1667977200000, 1668063600000, 1668150000000, 1668236400000, 1668322800000, 1668409200000]; | ||
| const manySeriesData = Array.from({ length: 25 }, (_, i) => | ||
| DATETIMES.map((datetime, j) => ({ | ||
| datetime, | ||
| series: `Series ${i + 1}`, | ||
| value: Math.round(1000 + Math.sin((i + j) * 0.8) * 800 + Math.cos(i * 0.5) * 500 + j * 120), | ||
| })) | ||
| ).flat(); | ||
|
|
||
| export default { | ||
| title: 'React Spectrum Charts 2/Line/Features/HoverLabel', | ||
| component: Line, | ||
| }; | ||
|
|
||
| const defaultChartProps: ChartProps = { data: workspaceTrendsData, minWidth: 400, maxWidth: 800, height: 400 }; | ||
|
|
||
| const HoverLabelStory: StoryFn<typeof Line> = (args): ReactElement => { | ||
| const chartProps = useChartProps(defaultChartProps); | ||
| return ( | ||
| <Chart {...chartProps}> | ||
| <Axis position="left" grid /> | ||
| <Axis position="bottom" labelFormat="time" /> | ||
| <Line {...args} /> | ||
| <Legend lineWidth={{ value: 0 }} /> | ||
| </Chart> | ||
| ); | ||
| }; | ||
|
|
||
| export const WithHoverLabel = bindWithProps(HoverLabelStory); | ||
| WithHoverLabel.args = { | ||
| color: 'series', | ||
| dimension: 'datetime', | ||
| metric: 'value', | ||
| scaleType: 'time', | ||
| showHoverLabel: true, | ||
| }; | ||
|
|
||
| export const WithoutHoverLabel = bindWithProps(HoverLabelStory); | ||
| WithoutHoverLabel.args = { | ||
| color: 'series', | ||
| dimension: 'datetime', | ||
| metric: 'value', | ||
| scaleType: 'time', | ||
| showHoverLabel: false, | ||
| }; | ||
|
|
||
| export const DimensionHover = bindWithProps(HoverLabelStory); | ||
| DimensionHover.args = { | ||
| color: 'series', | ||
| dimension: 'datetime', | ||
| metric: 'value', | ||
| scaleType: 'time', | ||
| showHoverLabel: true, | ||
| dimensionHover: true, | ||
| }; | ||
|
|
||
| const ManySeriesStory: StoryFn<typeof Line> = (args): ReactElement => { | ||
| const chartProps = useChartProps({ data: manySeriesData, minWidth: 400, maxWidth: 800, height: 400 }); | ||
| return ( | ||
| <Chart {...chartProps}> | ||
| <Axis position="left" grid /> | ||
| <Axis position="bottom" labelFormat="time" /> | ||
| <Line {...args} /> | ||
| </Chart> | ||
| ); | ||
| }; | ||
|
|
||
| export const DimensionHoverManySeries = bindWithProps(ManySeriesStory); | ||
| DimensionHoverManySeries.args = { | ||
| color: 'series', | ||
| dimension: 'datetime', | ||
| metric: 'value', | ||
| scaleType: 'time', | ||
| showHoverLabel: true, | ||
| dimensionHover: true, | ||
| }; |
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
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
170 changes: 170 additions & 0 deletions
170
packages/vega-spec-builder-s2/src/line/directLabelUtils.ts
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,170 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed 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 REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
| import { NumericValueRef, ProductionRule, TextMark, Transforms } from 'vega'; | ||
|
|
||
| import { BACKGROUND_COLOR, CHART_SIZE_FONT_SIZE, CHART_SIZE_LABEL_GAP, DIRECT_LABEL_BACKGROUND_STROKE_WIDTH, DIRECT_LABEL_FONT_WEIGHT } from '@spectrum-charts/constants'; | ||
| import { getS2ColorValue } from '@spectrum-charts/themes'; | ||
|
|
||
| import { ColorScheme } from '../types'; | ||
|
|
||
| type PositionRef = NumericValueRef | ProductionRule<NumericValueRef>; | ||
| type FillOverride = { field: string } | { value: string } | { signal: string }; | ||
|
|
||
| export interface DirectLabelUpdateEncode { | ||
| x: PositionRef; | ||
| y: PositionRef; | ||
| additional?: Record<string, unknown>; | ||
| } | ||
|
|
||
| export const MIN_LABEL_GAP = CHART_SIZE_LABEL_GAP; | ||
|
|
||
| /** | ||
| * Builds the five Vega transforms that compute collision-aware vertical positions for a set of | ||
| * co-located labels. Shared by direct labels (namespace='') and hover labels (namespace='hover'). | ||
| * | ||
| * With namespace='': produces fields _seriesCount, _metricRank, _scaledY, _adjustedY, _cumMaxAdjusted | ||
| * With namespace='hover': produces fields _hover_seriesCount, _hover_metricRank, etc. | ||
| */ | ||
| export const getCascadeTransforms = (yScaleName: string, metric: string, namespace: string): Transforms[] => { | ||
| const p = namespace ? `_${namespace}_` : '_'; | ||
| const metricRank = `${p}metricRank`; | ||
| const scaledY = `${p}scaledY`; | ||
| const adjustedY = `${p}adjustedY`; | ||
|
|
||
| return [ | ||
| { | ||
| type: 'joinaggregate' as const, | ||
| fields: [metric], | ||
| ops: ['count' as const], | ||
| as: [`${p}seriesCount`], | ||
| }, | ||
| { | ||
| type: 'window' as const, | ||
| sort: { field: [metric], order: ['descending' as const] }, | ||
| ops: ['rank' as const], | ||
| as: [metricRank], | ||
| }, | ||
| { type: 'formula' as const, as: scaledY, expr: `scale('${yScaleName}', datum["${metric}"])` }, | ||
| { type: 'formula' as const, as: adjustedY, expr: `datum.${scaledY} - datum.${metricRank} * ${MIN_LABEL_GAP}` }, | ||
| { | ||
| type: 'window' as const, | ||
| sort: { field: [metricRank], order: ['ascending' as const] }, | ||
| frame: [null, 0] as [null, number], | ||
| ops: ['max' as const], | ||
| fields: [adjustedY], | ||
| as: [`${p}cumMaxAdjusted`], | ||
| }, | ||
| ]; | ||
| }; | ||
|
|
||
| // Shared style constants used by both mark variants. | ||
| // Any visual change here automatically applies to hover labels AND static labels. | ||
| const directLabelBackgroundStyle = { | ||
| fill: { value: 'transparent' as const }, | ||
| stroke: { signal: BACKGROUND_COLOR }, | ||
| strokeWidth: { value: DIRECT_LABEL_BACKGROUND_STROKE_WIDTH }, | ||
| fontWeight: { value: DIRECT_LABEL_FONT_WEIGHT }, | ||
| fontSize: { signal: CHART_SIZE_FONT_SIZE }, | ||
| }; | ||
|
|
||
| const getDirectLabelForegroundFill = (colorScheme: ColorScheme, override?: FillOverride): FillOverride => | ||
| override ?? { value: getS2ColorValue('gray-900', colorScheme) }; | ||
|
|
||
| /** | ||
| * Builds the two-mark (background halo + foreground text) pattern for hover-style labels | ||
| * where position is encoded directly from data (no label transform). | ||
| * Both marks read from the same dataSource. | ||
| */ | ||
| export const getDirectLabelTextMarks = ( | ||
| backgroundMarkName: string, | ||
| foregroundMarkName: string, | ||
| dataSource: string, | ||
| textSignal: string, | ||
| updateEncode: DirectLabelUpdateEncode, | ||
| colorScheme: ColorScheme, | ||
| foregroundFillOverride?: FillOverride | ||
| ): TextMark[] => { | ||
| const { x, y, additional = {} } = updateEncode; | ||
| return [ | ||
| { | ||
| name: backgroundMarkName, | ||
| type: 'text', | ||
| interactive: false, | ||
| from: { data: dataSource }, | ||
| encode: { | ||
| enter: { text: { signal: textSignal }, ...directLabelBackgroundStyle }, | ||
| update: { x, y, ...additional } as never, | ||
| }, | ||
| }, | ||
| { | ||
| name: foregroundMarkName, | ||
| type: 'text', | ||
| interactive: false, | ||
| from: { data: dataSource }, | ||
| encode: { | ||
| enter: { | ||
| text: { signal: textSignal }, | ||
| fill: getDirectLabelForegroundFill(colorScheme, foregroundFillOverride), | ||
| fontWeight: { value: DIRECT_LABEL_FONT_WEIGHT }, | ||
| fontSize: { signal: CHART_SIZE_FONT_SIZE }, | ||
| }, | ||
| update: { x, y, ...additional } as never, | ||
| }, | ||
| }, | ||
| ]; | ||
| }; | ||
|
|
||
| /** | ||
| * Builds the two-mark (background halo + foreground text) pattern for annotation-style labels | ||
| * that use Vega's label transform for collision-aware placement. | ||
| * The background mark runs the label transform; the foreground reads its computed positions | ||
| * (x, y, align, baseline, opacity) from the background mark. | ||
| */ | ||
| export const getLabelTransformTextMarks = ( | ||
| backgroundMarkName: string, | ||
| foregroundMarkName: string, | ||
| dataSource: string, | ||
| textSignal: string, | ||
| colorScheme: ColorScheme, | ||
| labelTransform: Transforms, | ||
| foregroundFillOverride?: FillOverride | ||
| ): TextMark[] => [ | ||
| { | ||
| name: backgroundMarkName, | ||
| type: 'text', | ||
| interactive: false, | ||
| from: { data: dataSource }, | ||
| encode: { | ||
| enter: { text: { signal: textSignal }, ...directLabelBackgroundStyle }, | ||
| update: { fontWeight: { value: DIRECT_LABEL_FONT_WEIGHT } }, | ||
| }, | ||
| transform: [labelTransform], | ||
| }, | ||
| { | ||
| name: foregroundMarkName, | ||
| type: 'text', | ||
| interactive: false, | ||
| from: { data: backgroundMarkName }, | ||
| encode: { | ||
| enter: { fill: getDirectLabelForegroundFill(colorScheme, foregroundFillOverride) }, | ||
| update: { | ||
| text: { field: 'text' }, | ||
| x: { field: 'x' }, | ||
| y: { field: 'y' }, | ||
| align: { field: 'align' }, | ||
| baseline: { field: 'baseline' }, | ||
| opacity: { field: 'opacity' }, | ||
| fontWeight: { value: DIRECT_LABEL_FONT_WEIGHT }, | ||
| }, | ||
| }, | ||
| }, | ||
| ]; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we make a prior decision to make label gaps scale with chart size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I decided to scale it because with the large size they were overlapping a little too much, I think I showed you when we had that call a few weeks ago. If it looks off though let me know
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good. I just forgot over the break. We set the constants up so it's easy to tweak later. Good stuff!