-
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 4 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 |
---|---|---|
|
@@ -26,7 +26,7 @@ import { Chart, Line, Grid, XAxis, YAxis } from '@lg-charts/core'; | |
<Chart> | ||
<Grid vertical={false}> | ||
<XAxis type="time" /> | ||
<YAxis type="value" unit="GB" /> | ||
<YAxis type="value" 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. this api does feel a little tied to echarts - should we leverage a more expected API and then normalize it on our side before sending thru 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. Certainly open to discussion here, just wanted to call it out 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. Agreed! I'm thinking that we just publicly expose the callback. Addressing this more in this thread 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. awesome |
||
<Line | ||
name="Series 1" | ||
data={[ | ||
|
@@ -92,20 +92,20 @@ Renders an x-axis. | |
|
||
#### Props | ||
|
||
| Name | Description | Type | Default | | ||
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- | ------- | | ||
| `type` | Type of axis. | 'category' \| 'value' \| 'time' \| 'log' | | | ||
| `label` _(optional)_ | Label name to be rendered on the axis. | string | | | ||
| `unit` _(optional)_ | String that will be appended to the values of the axis. Only applies if `type` of `value`. _Note: this unit will not impact the data. E.g. if data is given in MB and the units are set to "GB", the component won’t convert these values. Conversion of data is up to the consumer._ | string | | | ||
| Name | Description | Type | Default | | ||
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- | ------- | | ||
| `type` | Type of axis. | 'category' \| 'value' \| 'time' \| 'log' | | | ||
| `label` _(optional)_ | Label name to be rendered on the axis. | string | | | ||
| `formatter` _(optional)_ | Formatter of axis label, which supports string template and callback function. (See [ECharts xAxis.axisLabel.formatter docs](https://echarts.apache.org/en/option.html#xAxis.axisLabel.formatter) for more details) | string \| ((value: string, index: number) => string) \| { [key: string]: string } | | | ||
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. Is it weird to link out to the ECharts docs in our docs? 🤔 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. Not strange to link out, but we might want to be careful with the wording. Do we want to commit ourselves to sticking with the ECharts API, or do we want to give ourselves more freedom? (I think this is fine, but just want to be careful about implying that our API is exactly the same as theirs) 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. Good call out - agreed 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. Addressing this more in thread here (TL;DR - I think this probably should be removed, and we should only publicly support a subset for this) |
||
|
||
### `YAxis` | ||
|
||
Renders a y-axis. | ||
|
||
#### Props | ||
|
||
| Name | Description | Type | Default | | ||
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- | ------- | | ||
| `type` | Type of axis. | 'category' \| 'value' \| 'time' \| 'log' | | | ||
| `label` _(optional)_ | Label name to be rendered on the axis. | string | | | ||
| `unit` _(optional)_ | String that will be appended to the values of the axis. Only applies if `type` of `value`. _Note: this unit will not impact the data. E.g. if data is given in MB and the units are set to "GB", the component won’t convert these values. Conversion of data is up to the consumer._ | string | | | ||
| Name | Description | Type | Default | | ||
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- | ------- | | ||
| `type` | Type of axis. | 'category' \| 'value' \| 'time' \| 'log' | | | ||
| `label` _(optional)_ | Label name to be rendered on the axis. | string | | | ||
| `formatter` _(optional)_ | Formatter of axis label, which supports string template and callback function. (See [ECharts yAxis.axisLabel.formatter docs](https://echarts.apache.org/en/option.html#yAxis.axisLabel.formatter) for more details) | string \| ((value: string, index: number) => string) \| { [key: string]: string } | | |
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, | ||
|
@@ -87,17 +84,17 @@ const getOptions = ({ | |
* <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 })); | ||
}, [type, label, unit, theme, updateChartOptions]); | ||
updateChartOptions(getOptions({ type, label, formatter, theme })); | ||
}, [type, label, formatter, theme, updateChartOptions]); | ||
|
||
return null; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,7 +18,21 @@ export interface XAxisProps { | |||||
label?: string; | ||||||
|
||||||
/** | ||||||
* Unit of the axis to be rendered with value. | ||||||
* | ||||||
* Formatter of axis label, which supports string template and callback function. | ||||||
* | ||||||
* ```ts | ||||||
* // Use string template; template variable is the default label of axis {value} | ||||||
* formatter: '{value} kg' | ||||||
* | ||||||
* // Use callback. | ||||||
* formatter: function (value, index) { | ||||||
* return value + 'kg'; | ||||||
*} | ||||||
* ``` | ||||||
*/ | ||||||
unit?: string; | ||||||
formatter?: | ||||||
| string | ||||||
| ((value: string, index: number) => string) | ||||||
| { [key: string]: string }; | ||||||
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. What would an object formatter look like? 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 object formatter is what echarts calls a "cascading template" and only works with time. From their site:
I guess that means this could actually be typed better:
Suggested change
where type CascadingTemplateLevels =
| 'year'
| 'month'
| 'day'
| 'hour'
| 'minute'
| 'second'
| 'millisecond'
| 'none'; However, I think given your point in this comment, I'm wondering if we should publicly support this. Given that EChart's string interpolation isn't standard JS either, I'm almost wondering if we should just publicly support the callback function. Consumers can still get hacky, override the type and use either of these, but by not publicly supporting them it probably helps save some headaches in the future if we ever need to switch the underlying library. What do you think? 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. Decided to make the decision to only support the callback function and have pushed changes to reflect that. Let me know if there's any disagreement here. |
||||||
} |
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, | ||
|
@@ -86,17 +83,17 @@ const getOptions = ({ | |
* <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 })); | ||
}, [type, label, unit, theme, updateChartOptions]); | ||
updateChartOptions(getOptions({ type, label, formatter, theme })); | ||
}, [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