-
Notifications
You must be signed in to change notification settings - Fork 352
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(charts): add RTL legend support #9570
Changes from 4 commits
cb601f1
fd66344
794520b
62c1ca0
a4e0be6
df4ddfe
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 |
---|---|---|
|
@@ -31,6 +31,8 @@ import { getPaddingForSide } from '../ChartUtils/chart-padding'; | |
import { getPatternDefs, mergePatternData, useDefaultPatternProps } from '../ChartUtils/chart-patterns'; | ||
import { getChartTheme } from '../ChartUtils/chart-theme-types'; | ||
import { useEffect } from 'react'; | ||
import { ChartLabel } from '../ChartLabel/ChartLabel'; | ||
import { ChartPoint } from '../ChartPoint/ChartPoint'; | ||
|
||
/** | ||
* Chart is a wrapper component that reconciles the domain for all its children, controls the layout of the chart, | ||
|
@@ -284,6 +286,10 @@ export interface ChartProps extends VictoryChartProps { | |
* cases, the legend may not be visible until enough padding is applied. | ||
*/ | ||
legendPosition?: 'bottom' | 'bottom-left' | 'right'; | ||
/** | ||
* @beta Text direction of the legend labels. | ||
*/ | ||
legendDirection?: 'ltr' | 'rtl'; | ||
/** | ||
* The maxDomain prop defines a maximum domain value for a chart. This prop is useful in situations where the maximum | ||
* domain of a chart is static, while the minimum value depends on data or other variable information. If the domain | ||
|
@@ -471,6 +477,7 @@ export const Chart: React.FunctionComponent<ChartProps> = ({ | |
legendComponent = <ChartLegend />, | ||
legendData, | ||
legendPosition = ChartCommonStyles.legend.position, | ||
legendDirection = 'ltr', | ||
name, | ||
padding, | ||
patternScale, | ||
|
@@ -527,6 +534,20 @@ export const Chart: React.FunctionComponent<ChartProps> = ({ | |
...(name && { name: `${name}-${(legendComponent as any).type.displayName}` }), | ||
orientation: legendOrientation, | ||
theme, | ||
...(legendDirection === 'rtl' && { | ||
dataComponent: legendComponent.props.dataComponent ? ( | ||
React.cloneElement(legendComponent.props.dataComponent, { transform: 'translate(40)' }) | ||
) : ( | ||
<ChartPoint transform="translate(40)" /> | ||
) | ||
}), | ||
...(legendDirection === 'rtl' && { | ||
labelComponent: legendComponent.props.labelComponent ? ( | ||
React.cloneElement(legendComponent.props.labelComponent, { direction: 'rtl', dx: '10' }) | ||
) : ( | ||
<ChartLabel direction="rtl" dx="10" /> | ||
) | ||
}), | ||
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. 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. The value of the The text issue shown in the screenshot could be resolved by adjusting the padding, which the 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. @dlabrecq Updated the logic to account for text length for RTL legends, for very long text lengths you'll still need to adjust padding but that is what the position prop refers to being required. I tried both ways of adjusting the legend position and updating the translate ended up being simpler with less piping required for props. The extra 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. For some reason, this was not working in the preview; however, it's working locally. Suspect I may have been viewing an old preview. 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. 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. Oh forgot to update those, will fix that. Perhaps updating the branch again will fix the preview too |
||
...legendComponent.props | ||
}); | ||
|
||
|
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.
rather than a prop, should this auto switch when browser is RTL?
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.
Per conversation, leaving this PR as is and opened #9656 to investigate adding logic to smartly set the default.