-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[charts] Make useChartGradientId
public
#16106
Merged
JCQuintas
merged 10 commits into
mui:master
from
JCQuintas:16095-charts-make-usechartgradientid-hooks-public
Jan 13, 2025
+203
−60
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c00c710
[charts] Make `useChartGradientId` public
JCQuintas 21c4fe5
rename param
JCQuintas 9752277
remove direction param
JCQuintas 6bcdfbd
Fix continuous
JCQuintas 7b198f8
Merge commit '9c2224f430626d90f3dfe09e4c2cfc2cd1c05226' into 16095-ch…
JCQuintas 1af1f98
warn for duplicate ids in devmode
JCQuintas 36f53ff
add test for duplicate ids warning
JCQuintas 04648ab
revert package change
JCQuintas 88fc5b3
suggestions
JCQuintas faac6e5
Fix sparkline chart
JCQuintas 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
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 |
---|---|---|
|
@@ -5,7 +5,7 @@ import { MakeOptional } from '@mui/x-internals/types'; | |
import { BarPlot } from '../BarChart'; | ||
import { LinePlot, AreaPlot, LineHighlightPlot } from '../LineChart'; | ||
import { ChartContainer, ChartContainerProps } from '../ChartContainer'; | ||
import { DEFAULT_X_AXIS_KEY } from '../constants'; | ||
import { DEFAULT_X_AXIS_KEY, DEFAULT_Y_AXIS_KEY } from '../constants'; | ||
import { ChartsTooltip } from '../ChartsTooltip'; | ||
import { ChartsTooltipSlots, ChartsTooltipSlotProps } from '../ChartsTooltip/ChartTooltip.types'; | ||
import { ChartsAxisHighlight, ChartsAxisHighlightProps } from '../ChartsAxisHighlight'; | ||
|
@@ -187,7 +187,7 @@ const SparkLineChart = React.forwardRef(function SparkLineChart( | |
]} | ||
yAxis={[ | ||
{ | ||
id: DEFAULT_X_AXIS_KEY, | ||
id: DEFAULT_Y_AXIS_KEY, | ||
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 we were using X_AXIS_ID on yAxis for sparkline 🫠 |
||
...yAxis, | ||
}, | ||
]} | ||
|
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,41 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import { createRenderer, screen } from '@mui/internal-test-utils'; | ||
import { useChartGradientId, useChartGradientIdObjectBound } from './useChartGradientId'; | ||
import { ChartDataProvider } from '../context'; | ||
|
||
function UseGradientId() { | ||
const id = useChartGradientId('test-id'); | ||
return <div>{id}</div>; | ||
} | ||
|
||
function UseGradientIdObjectBound() { | ||
const id = useChartGradientIdObjectBound('test-id'); | ||
return <div>{id}</div>; | ||
} | ||
|
||
describe('useChartGradientId', () => { | ||
const { render } = createRenderer(); | ||
|
||
it('should properly generate a correct id', () => { | ||
render( | ||
<ChartDataProvider series={[]} height={100} width={100}> | ||
<UseGradientId /> | ||
</ChartDataProvider>, | ||
); | ||
|
||
expect(screen.getByText(/:\w+:-gradient-test-id/)).toBeVisible(); | ||
}); | ||
|
||
describe('useChartGradientIdObjectBound', () => { | ||
it('should properly generate a correct id', () => { | ||
render( | ||
<ChartDataProvider series={[]} height={100} width={100}> | ||
<UseGradientIdObjectBound /> | ||
</ChartDataProvider>, | ||
); | ||
|
||
expect(screen.getByText(/:\w+:-gradient-test-id-object-bound/)).toBeVisible(); | ||
}); | ||
}); | ||
}); |
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,51 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { useChartId } from './useChartId'; | ||
import { AxisId } from '../models/axis'; | ||
|
||
/** | ||
* Returns a function that generates a gradient id for the given axis id. | ||
*/ | ||
export function useChartGradientIdBuilder() { | ||
const chartId = useChartId(); | ||
return React.useCallback((axisId: AxisId) => `${chartId}-gradient-${axisId}`, [chartId]); | ||
} | ||
|
||
/** | ||
* Returns a function that generates a gradient id for the given axis id. | ||
*/ | ||
export function useChartGradientIdObjectBoundBuilder() { | ||
const chartId = useChartId(); | ||
return React.useCallback( | ||
(axisId: AxisId) => `${chartId}-gradient-${axisId}-object-bound`, | ||
[chartId], | ||
); | ||
} | ||
|
||
/** | ||
* Returns a gradient id for the given axis id. | ||
* | ||
* Can be useful when reusing the same gradient on custom components. | ||
* | ||
* For a gradient that respects the coordinates of the object on which it is applied, use `useChartGradientIdObjectBound` instead. | ||
* | ||
* @param axisId the axis id | ||
* @returns the gradient id | ||
*/ | ||
export function useChartGradientId(axisId: AxisId) { | ||
return useChartGradientIdBuilder()(axisId); | ||
} | ||
|
||
/** | ||
* Returns a gradient id for the given axis id. | ||
* | ||
* Can be useful when reusing the same gradient on custom components. | ||
* | ||
* This gradient differs from `useChartGradientId` in that it respects the coordinates of the object on which it is applied. | ||
* | ||
* @param axisId the axis id | ||
* @returns the gradient id | ||
*/ | ||
export function useChartGradientIdObjectBound(axisId: AxisId) { | ||
return useChartGradientIdObjectBoundBuilder()(axisId); | ||
} |
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
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.
@alexfauquette I've removed
direction=(x,y,z)
from usage. It didn't look useful, asaxisId
should technically be unique by itself.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.
It's a debate I got when creating the lib. Should axis id be unique in general or unique per direction.
For now, we only enforce the "unique per direction" (If not respected users will get a warning). If you remove the axis-driection in the id, users could get weird resultsaxis-direction without any error in the console.
Duplicated ids are a bit tricky to identify as bug. That's why I preferred to be conservative on it. But that's up to debate
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.
Feels a bit useless,
id
is generally expected to be unique. Although you could argue that axis direction is a different domain 🙃.Should we hold onto this while we wait for the plugins to merge? then I can try to reason with how we currently handle the warnings ad make a decision 😆
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.
That's exactly my argument :p The main usecase I've in mind is a scatter plot where you can any data grid column versus any other column.
Then the chart has the same set of x and y-axes to chose from. And so either we or the developer have to be careful about avoiding id colision.
By order of preferences, we can
So I agree about waiting for the plugins to be merged and verify if we can enforce id unicity
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.
There is no warning for duplicate axis ids, only series ids 🫠