-
Notifications
You must be signed in to change notification settings - Fork 64
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
LG-4659: Replace axes unit
prop with formatter
#2536
Changes from all commits
4d7d8c8
f02eee9
3902ebe
cf3a4ed
3734ae7
ac6258a
5447e1b
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@lg-charts/core': minor | ||
--- | ||
|
||
Replaces `unit` prop of `XAxis` and `YAxis` with `formatter` prop |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,13 +14,13 @@ import { | |
import { ChartOptions } from '../Chart/Chart.types'; | ||
import { useChartContext } from '../ChartContext'; | ||
|
||
import { XAxisProps, XAxisType } from './XAxis.types'; | ||
import { XAxisProps } from './XAxis.types'; | ||
|
||
const getOptions = ({ | ||
theme, | ||
type, | ||
label, | ||
unit, | ||
formatter, | ||
}: XAxisProps & { theme: Theme }): Partial<ChartOptions> => { | ||
const options: Partial<ChartOptions> = { | ||
xAxis: { | ||
|
@@ -42,10 +42,7 @@ const getOptions = ({ | |
color: color[theme].text[Variant.Secondary][InteractionState.Default], | ||
align: 'center', | ||
margin: spacing[400], | ||
formatter: | ||
unit && type === XAxisType.Value | ||
? (value: string) => `${value}${unit}` | ||
: undefined, | ||
formatter, | ||
}, | ||
axisTick: { | ||
show: false, | ||
|
@@ -96,16 +93,16 @@ const unsetAxisOptions = { | |
* <XAxis | ||
* type="time", | ||
* label="My X-Axis Data", | ||
* unit="GB" | ||
* formatter="{value}GB" | ||
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. Yup, exactly. Specifically for rendering the value. There are other formatters such as |
||
* /> | ||
* </Chart> | ||
*/ | ||
export function XAxis({ type, label, unit }: XAxisProps) { | ||
export function XAxis({ type, label, formatter }: XAxisProps) { | ||
const { updateChartOptions } = useChartContext(); | ||
const { theme } = useDarkMode(); | ||
|
||
useEffect(() => { | ||
updateChartOptions(getOptions({ type, label, unit, theme })); | ||
updateChartOptions(getOptions({ type, label, formatter, theme })); | ||
|
||
return () => { | ||
/** | ||
|
@@ -115,7 +112,7 @@ export function XAxis({ type, label, unit }: XAxisProps) { | |
xAxis: unsetAxisOptions, | ||
}); | ||
}; | ||
}, [type, label, unit, theme, updateChartOptions]); | ||
}, [type, label, formatter, theme, updateChartOptions]); | ||
|
||
return null; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,13 +14,13 @@ import { | |
import { ChartOptions } from '../Chart/Chart.types'; | ||
import { useChartContext } from '../ChartContext'; | ||
|
||
import { YAxisProps, YAxisType } from './YAxis.types'; | ||
import { YAxisProps } from './YAxis.types'; | ||
|
||
const getOptions = ({ | ||
theme, | ||
type, | ||
label, | ||
unit, | ||
formatter, | ||
}: YAxisProps & { theme: Theme }): Partial<ChartOptions> => { | ||
const options: Partial<ChartOptions> = { | ||
yAxis: { | ||
|
@@ -42,10 +42,7 @@ const getOptions = ({ | |
color: color[theme].text[Variant.Secondary][InteractionState.Default], | ||
align: 'right', | ||
margin: spacing[200], | ||
formatter: | ||
unit && type === YAxisType.Value | ||
? (value: string) => `${value}${unit}` | ||
: undefined, | ||
formatter, | ||
}, | ||
axisTick: { | ||
show: false, | ||
|
@@ -95,16 +92,16 @@ const unsetAxisOptions = { | |
* <YAxis | ||
* type="value", | ||
* label="My Y-Axis Data", | ||
* unit="GB" | ||
* formatter="{value}GB" | ||
* /> | ||
* </Chart> | ||
*/ | ||
export function YAxis({ type, label, unit }: YAxisProps) { | ||
export function YAxis({ type, label, formatter }: YAxisProps) { | ||
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. Looks like there's a lot of similarity between XAxis & YAxis. Not necessary in this PR, but could be worth DRY-ing this up a bit 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. Yup, they're almost identical atm with just a few different props. I considered having a more common component that these two work off of but decided not to since there are only 2, but I agree, it probably makes sense. I'll move in this direction in a future PR. |
||
const { updateChartOptions } = useChartContext(); | ||
const { theme } = useDarkMode(); | ||
|
||
useEffect(() => { | ||
updateChartOptions(getOptions({ type, label, unit, theme })); | ||
updateChartOptions(getOptions({ type, label, formatter, theme })); | ||
|
||
return () => { | ||
/** | ||
|
@@ -114,7 +111,7 @@ export function YAxis({ type, label, unit }: YAxisProps) { | |
yAxis: unsetAxisOptions, | ||
}); | ||
}; | ||
}, [type, label, unit, theme, updateChartOptions]); | ||
}, [type, label, formatter, theme, updateChartOptions]); | ||
|
||
return null; | ||
} |
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.
I guess this is technically major since it's not backwards compatible, but since it's still in v0, I made it minor. Let me know if this is incorrect, though!
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, pre-v1 changesets can be a bit loose. Rule of thumb for me is everything shifts down a level