-
Notifications
You must be signed in to change notification settings - Fork 63
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-4619: Add axes components #2525
Merged
+487
−149
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
937aa93
Implement components
tsck 2d9b28e
Fix up storybook
tsck a9736a3
Get labels working as expected
tsck fe7439a
Fix update logic
tsck f94d9aa
Update YAxis
tsck 010814d
Changeset
tsck 364ccaa
Fix hook test
tsck 7de6114
Fix border
tsck 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 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,11 @@ | ||
--- | ||
'@lg-charts/core': minor | ||
--- | ||
|
||
Adds `XAxis` and `YAxis` components, improves update logic, and improves Storybook. | ||
|
||
- Adds `XAxis` component for adding an x-axis to a chart. | ||
- Adds `YAxis` component for adding a y-axis to a chart. | ||
- Improves chart options update logic | ||
- `lodash.merge` was being used originally, but this didn't work quite as expected. The goal is to merge partial options objects in without overwriting previously set options that are unrelated to the update. This was not how it worked, leading to bugs when updating the same base keys with different options. I.e. when adding splitLine configurations on xAxis in order to add grid lines, then adding more axis specific config in XAxis related to the actual axis, the splitLine would be totally overwritten. | ||
- Improves @lg-charts/core Storybook, by adding better descriptions and organization. |
This file contains 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 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 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 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 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 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 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 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,103 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import { useDarkMode } from '@leafygreen-ui/leafygreen-provider'; | ||
import { Theme } from '@leafygreen-ui/lib'; | ||
import { | ||
color, | ||
fontFamilies, | ||
fontWeights, | ||
InteractionState, | ||
spacing, | ||
Variant, | ||
} from '@leafygreen-ui/tokens'; | ||
|
||
import { ChartOptions } from '../Chart/Chart.types'; | ||
import { useChartContext } from '../ChartContext'; | ||
|
||
import { XAxisProps, XAxisType } from './XAxis.types'; | ||
|
||
const getOptions = ({ | ||
theme, | ||
type, | ||
label, | ||
unit, | ||
}: XAxisProps & { theme: Theme }): Partial<ChartOptions> => { | ||
const options: Partial<ChartOptions> = { | ||
xAxis: { | ||
type: type, | ||
axisLine: { | ||
show: true, | ||
lineStyle: { | ||
color: | ||
color[theme].border[Variant.Secondary][InteractionState.Default], | ||
width: 1, | ||
}, | ||
}, | ||
axisLabel: { | ||
show: true, | ||
fontFamily: fontFamilies.default, | ||
fontWeight: fontWeights.medium, | ||
fontSize: 11, | ||
lineHeight: spacing[400], | ||
color: color[theme].text[Variant.Secondary][InteractionState.Default], | ||
align: 'center', | ||
margin: spacing[400], | ||
formatter: | ||
unit && type === XAxisType.Value | ||
? (value: string) => `${value}${unit}` | ||
: undefined, | ||
}, | ||
axisTick: { | ||
show: false, | ||
}, | ||
name: label, | ||
nameLocation: 'middle', | ||
nameTextStyle: { | ||
fontFamily: fontFamilies.default, | ||
fontWeight: fontWeights.medium, | ||
fontSize: 11, | ||
lineHeight: spacing[400], | ||
padding: [spacing[200], 0, 0, 0], | ||
color: color[theme].text[Variant.Secondary][InteractionState.Default], | ||
}, | ||
nameGap: spacing[1000], | ||
}, | ||
}; | ||
|
||
if (label) { | ||
options.grid = { | ||
bottom: spacing[1200], // Pushes out to make room for the label | ||
}; | ||
} else { | ||
options.grid = { | ||
bottom: spacing[400], // Default bottom spacing | ||
}; | ||
} | ||
|
||
return options; | ||
}; | ||
|
||
/** | ||
* React component that can render an x-axis on a parent chart. | ||
* | ||
* This is done by updating the parent chart's canvas configuration received via context. | ||
* | ||
* ``` | ||
* <Chart> | ||
* <XAxis | ||
* type="time", | ||
* label="My X-Axis Data", | ||
* unit="GB" | ||
* /> | ||
* </Chart> | ||
*/ | ||
export function XAxis({ type, label, unit }: XAxisProps) { | ||
const { updateChartOptions } = useChartContext(); | ||
const { theme } = useDarkMode(); | ||
|
||
useEffect(() => { | ||
updateChartOptions(getOptions({ type, label, unit, theme })); | ||
}, [type, label, unit, theme, updateChartOptions]); | ||
|
||
return null; | ||
} |
This file contains 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,24 @@ | ||
export const XAxisType = { | ||
Category: 'category', | ||
Value: 'value', | ||
Time: 'time', | ||
Log: 'log', | ||
} as const; | ||
type XAxisType = (typeof XAxisType)[keyof typeof XAxisType]; | ||
|
||
export interface XAxisProps { | ||
/** | ||
* Type of axis. | ||
*/ | ||
type: XAxisType; | ||
|
||
/** | ||
* Label name of the axis. | ||
*/ | ||
label?: string; | ||
|
||
/** | ||
* Unit of the axis to be rendered with value. | ||
*/ | ||
unit?: string; | ||
} |
This file contains 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,2 @@ | ||
export { XAxis } from './XAxis'; | ||
export { XAxisProps } from './XAxis.types'; |
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.
Curious why we're replacing the lodash merge, but that's a question not a blocker
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.
The goal is to merge partial options objects in without overwriting previously set options that are unrelated to the update. This wasn't actually how
lodash.merge
worked (which I thought it would), leading to bugs when updating the same base keys with different options. I.e. when addingsplitLine
configurations onxAxis
in order to add grid lines, then adding more axis specific config in theXAxis
component related to the actual axis, thesplitLine
would be totally overwritten.The updated test is a good example of what should pass and wasn't with
lodash.merge
: