Repository navigation
chore(timeseries charts): adjust legend width by padding #32030
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ import { | |
| TimeFormatter, | ||
| ValueFormatter, | ||
| } from '@superset-ui/core'; | ||
| import { SortSeriesType } from '@superset-ui/chart-controls'; | ||
| import { SortSeriesType, LegendPaddingType } from '@superset-ui/chart-controls'; | ||
| import { format } from 'echarts/core'; | ||
| import type { LegendComponentOption } from 'echarts/components'; | ||
| import type { SeriesOption } from 'echarts'; | ||
|
|
@@ -425,6 +425,7 @@ export function getLegendProps( | |
| theme: SupersetTheme, | ||
| zoomable = false, | ||
| legendState?: LegendState, | ||
| padding?: LegendPaddingType, | ||
| ): LegendComponentOption | LegendComponentOption[] { | ||
| const legend: LegendComponentOption | LegendComponentOption[] = { | ||
| orient: [LegendOrientation.Top, LegendOrientation.Bottom].includes( | ||
|
|
@@ -443,13 +444,30 @@ export function getLegendProps( | |
| borderColor: theme.colors.grayscale.base, | ||
| }, | ||
| }; | ||
| const MIN_LEGEND_WIDTH = 0; | ||
| const MARGIN_GUTTER = 45; | ||
| const getLegendWidth = (paddingWidth: number) => | ||
| Math.max(paddingWidth - MARGIN_GUTTER, MIN_LEGEND_WIDTH); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kinda agree with bot's comment above - maybe we should set a minimal width value where the legend is still readable?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I tried out a few different min widths, but the problem is that when applying a min width, then we have the same overlap issue with the chart. So either way, there will be a UI issue, and a larger padding will need to be set. Someone needs to explicitly set a 0 padding in order for this to apply. If you think having the overlap is better than a readable legend, we can def do that. |
||
|
|
||
| switch (orientation) { | ||
| case LegendOrientation.Left: | ||
| legend.left = 0; | ||
| if (padding?.left) { | ||
| legend.textStyle = { | ||
| overflow: 'truncate', | ||
| width: getLegendWidth(padding.left), | ||
| }; | ||
| } | ||
| break; | ||
| case LegendOrientation.Right: | ||
| legend.right = 0; | ||
| legend.top = zoomable ? TIMESERIES_CONSTANTS.legendRightTopOffset : 0; | ||
| if (padding?.right) { | ||
| legend.textStyle = { | ||
| overflow: 'truncate', | ||
| width: getLegendWidth(padding.right), | ||
| }; | ||
| } | ||
| break; | ||
| case LegendOrientation.Bottom: | ||
| legend.bottom = 0; | ||
|
|
@@ -467,7 +485,7 @@ export function getChartPadding( | |
| show: boolean, | ||
| orientation: LegendOrientation, | ||
| margin?: string | number | null, | ||
| padding?: { top?: number; bottom?: number; left?: number; right?: number }, | ||
| padding?: LegendPaddingType, | ||
| isHorizontal?: boolean, | ||
| ): { | ||
| bottom: number; | ||
|
|
||
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.
Zero-width legend risk
Tell me more
What is the issue?
Setting MIN_LEGEND_WIDTH to 0 could cause legends to disappear completely when padding minus MARGIN_GUTTER is 0 or negative.
Why this matters
When the padding is less than or equal to MARGIN_GUTTER (45px), the legend width will be 0, making the legend text invisible and breaking the layout functionality intended by the developer.
Suggested change ∙ Feature Preview
Set a reasonable minimum width to ensure legends remain visible and functional:
💬 Chat with Korbit by mentioning @korbit-ai.