Skip to content
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] Add line animation #11620

Merged
merged 35 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
bde2f6c
WIP
alexfauquette Jan 10, 2024
87acc0c
Merge remote-tracking branch 'upstream/next' into add-line-animation
alexfauquette Jan 22, 2024
b65785f
ts fixes
alexfauquette Jan 23, 2024
2501bb0
animate mark
alexfauquette Jan 23, 2024
4627fff
investagiate bug
alexfauquette Jan 24, 2024
8e27312
fix add animation
alexfauquette Jan 24, 2024
ec51da2
Merge remote-tracking branch 'upstream/next' into add-line-animation
alexfauquette Jan 24, 2024
c6a792d
support skip animation
alexfauquette Jan 24, 2024
90d397c
add docs
alexfauquette Jan 24, 2024
c7e28e5
disable animation on sparkilne
alexfauquette Jan 24, 2024
635707b
scripts
alexfauquette Jan 24, 2024
6b479e4
script
alexfauquette Jan 25, 2024
e67eb78
remove comment
alexfauquette Jan 25, 2024
543ae16
update docs
alexfauquette Jan 25, 2024
fd4094c
remove extra spaces
alexfauquette Jan 25, 2024
4061941
fix clip path id
alexfauquette Jan 25, 2024
176eb3f
add some delay to add a series
alexfauquette Jan 25, 2024
8a055c4
use one group per mark series
alexfauquette Jan 25, 2024
066fc7f
remove delay and reorder series area
alexfauquette Jan 25, 2024
7fd4a3a
Merge remote-tracking branch 'upstream/next' into add-line-animation
alexfauquette Jan 25, 2024
f8cc194
skip animation on interactive demos
alexfauquette Jan 25, 2024
875e6d8
proptype fix
alexfauquette Jan 25, 2024
6fed4f0
prettier
alexfauquette Jan 25, 2024
21559ba
fix
alexfauquette Jan 25, 2024
3383cad
script
alexfauquette Jan 25, 2024
6df0cbc
Apply suggestions from code review
alexfauquette Jan 29, 2024
d80b9fd
Apply suggestions from code review
alexfauquette Jan 29, 2024
b1c0ff2
typo
alexfauquette Jan 29, 2024
f1810b8
Align error message
alexfauquette Jan 29, 2024
6f9b1c2
rename completed by agregated
alexfauquette Jan 29, 2024
79fb189
be more explicit about the rendered element
alexfauquette Jan 29, 2024
45d35d0
docs:api
alexfauquette Jan 29, 2024
c76644f
Move the animation inside line and area slots
alexfauquette Jan 29, 2024
2cc4334
fix
alexfauquette Jan 29, 2024
6fa4b07
Merge remote-tracking branch 'upstream/next' into add-line-animation
alexfauquette Jan 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/data/charts/lines/ConnectNulls.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function ConnectNulls() {
]}
height={200}
margin={{ top: 10, bottom: 20 }}
skipAnimation
/>
</Stack>
);
Expand Down
1 change: 1 addition & 0 deletions docs/data/charts/lines/ConnectNulls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function ConnectNulls() {
]}
height={200}
margin={{ top: 10, bottom: 20 }}
skipAnimation
LukasTy marked this conversation as resolved.
Show resolved Hide resolved
/>
</Stack>
);
Expand Down
1 change: 1 addition & 0 deletions docs/data/charts/lines/InterpolationDemoNoSnap.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default function InterpolationDemoNoSnap() {
]}
height={300}
margin={{ top: 10, bottom: 30 }}
skipAnimation
/>
<HighlightedCode code={getExample(curveType)} language="tsx" />
</Box>
Expand Down
87 changes: 87 additions & 0 deletions docs/data/charts/lines/LineAnimation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as React from 'react';
import Button from '@mui/material/Button';
import Stack from '@mui/material/Stack';
import FormControlLabel from '@mui/material/FormControlLabel';
import Checkbox from '@mui/material/Checkbox';
import { LineChart } from '@mui/x-charts/LineChart';
import { mangoFusionPalette } from '@mui/x-charts/colorPalettes';

const defaultSeries = [
{ id: '1', data: [4, 5, 1, 2, 3, 3, 2], area: true, stack: '1' },
{ id: '2', data: [7, 4, 6, 7, 2, 3, 5], area: true, stack: '1' },
{ id: '3', data: [6, 4, 1, 2, 6, 3, 3], area: true, stack: '1' },
{ id: '4', data: [4, 7, 6, 1, 2, 7, 7], area: true, stack: '1' },
{ id: '5', data: [2, 2, 1, 7, 1, 5, 3], area: true, stack: '1' },
{ id: '6', data: [6, 6, 1, 6, 7, 1, 1], area: true, stack: '1' },
{ id: '7', data: [7, 6, 1, 6, 4, 4, 6], area: true, stack: '1' },
{ id: '8', data: [4, 3, 1, 6, 6, 3, 5], area: true, stack: '1' },
{ id: '9', data: [7, 6, 2, 7, 4, 2, 7], area: true, stack: '1' },
].map((item, index) => ({
...item,
color: mangoFusionPalette('light')[index],
}));

export default function LineAnimation() {
const [series, setSeries] = React.useState(defaultSeries);
const [nbSeries, setNbSeries] = React.useState(3);
const [skipAnimation, setSkipAnimation] = React.useState(false);

return (
<div
style={{
width: '100%',
display: 'flex',
flexDirection: 'column',
}}
>
<div>
<LineChart
xAxis={[{ data: [1, 2, 3, 4, 5, 6, 7] }]}
series={[
...series.slice(0, Math.min(nbSeries, 8)),
...series.slice(8, 10),
]}
skipAnimation={skipAnimation}
height={400}
/>
</div>
<Stack spacing={1} direction="row">
<Button
variant="outlined"
onClick={() =>
setSeries((prev) =>
prev.map((item) => ({
...item,
data: item.data.map((v) => Math.max(0.5, v - 4 + 8 * Math.random())),
})),
)
}
>
randomize
</Button>
<Button
variant="outlined"
onClick={() => setNbSeries((prev) => prev - 1)}
disabled={nbSeries === 0}
>
remove
</Button>
<Button
variant="outlined"
onClick={() => setNbSeries((prev) => prev + 1)}
disabled={nbSeries === 8}
>
add
</Button>
<FormControlLabel
checked={skipAnimation}
control={
<Checkbox onChange={(event) => setSkipAnimation(event.target.checked)} />
}
label="skipAnimation"
labelPlacement="end"
/>
</Stack>
</div>
);
}
87 changes: 87 additions & 0 deletions docs/data/charts/lines/LineAnimation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as React from 'react';
import Button from '@mui/material/Button';
import Stack from '@mui/material/Stack';
import FormControlLabel from '@mui/material/FormControlLabel';
import Checkbox from '@mui/material/Checkbox';
import { LineChart } from '@mui/x-charts/LineChart';
import { mangoFusionPalette } from '@mui/x-charts/colorPalettes';

const defaultSeries = [
{ id: '1', data: [4, 5, 1, 2, 3, 3, 2], area: true, stack: '1' },
{ id: '2', data: [7, 4, 6, 7, 2, 3, 5], area: true, stack: '1' },
{ id: '3', data: [6, 4, 1, 2, 6, 3, 3], area: true, stack: '1' },
{ id: '4', data: [4, 7, 6, 1, 2, 7, 7], area: true, stack: '1' },
{ id: '5', data: [2, 2, 1, 7, 1, 5, 3], area: true, stack: '1' },
{ id: '6', data: [6, 6, 1, 6, 7, 1, 1], area: true, stack: '1' },
{ id: '7', data: [7, 6, 1, 6, 4, 4, 6], area: true, stack: '1' },
{ id: '8', data: [4, 3, 1, 6, 6, 3, 5], area: true, stack: '1' },
{ id: '9', data: [7, 6, 2, 7, 4, 2, 7], area: true, stack: '1' },
].map((item, index) => ({
...item,
color: mangoFusionPalette('light')[index],
}));

export default function LineAnimation() {
const [series, setSeries] = React.useState(defaultSeries);
const [nbSeries, setNbSeries] = React.useState(3);
const [skipAnimation, setSkipAnimation] = React.useState(false);

return (
<div
style={{
width: '100%',
display: 'flex',
flexDirection: 'column',
}}
>
<div>
<LineChart
xAxis={[{ data: [1, 2, 3, 4, 5, 6, 7] }]}
series={[
...series.slice(0, Math.min(nbSeries, 8)),
...series.slice(8, 10),
]}
skipAnimation={skipAnimation}
height={400}
/>
</div>
<Stack spacing={1} direction="row">
<Button
variant="outlined"
onClick={() =>
setSeries((prev) =>
prev.map((item) => ({
...item,
data: item.data.map((v) => Math.max(0.5, v - 4 + 8 * Math.random())),
})),
)
}
>
randomize
</Button>
<Button
variant="outlined"
onClick={() => setNbSeries((prev) => prev - 1)}
disabled={nbSeries === 0}
>
remove
</Button>
<Button
variant="outlined"
onClick={() => setNbSeries((prev) => prev + 1)}
disabled={nbSeries === 8}
>
add
</Button>
<FormControlLabel
checked={skipAnimation}
control={
<Checkbox onChange={(event) => setSkipAnimation(event.target.checked)} />
}
label="skipAnimation"
labelPlacement="end"
/>
</Stack>
</div>
);
}
27 changes: 27 additions & 0 deletions docs/data/charts/lines/lines.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,30 @@ sx={{
```

{{"demo": "CSSCustomization.js"}}

## Animation

To skip animation at the creation and update of your chart, you can use the `skipAnimation` prop.
When set to `true` it skips animation powered by `@react-spring/web`.

Charts containers already use the `useReducedMotion` from `@react-spring/web` to skip animation [according to user preferences](https://react-spring.dev/docs/utilities/use-reduced-motion#why-is-it-important).

:::warning
If you support interactive way to add or remove series from your chart, you have to provide series' id.
alexfauquette marked this conversation as resolved.
Show resolved Hide resolved

Otherwise the chart will have no way to know if you are modifying, removing, or adding some series.
Ths will lead to strange behaviors.
alexfauquette marked this conversation as resolved.
Show resolved Hide resolved
:::

```jsx
// For a single component chart
<LineChart skipAnimation />

// For a composed chart
<ResponsiveChartContainer>
<LinePlot skipAnimation />
<AreaPlot skipAnimation />
</ResponsiveChartContainer>
```

{{"demo": "LineAnimation.js"}}
1 change: 1 addition & 0 deletions docs/pages/x/api/charts/area-element.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"props": {
"skipAnimation": { "type": { "name": "bool" }, "default": "false" },
"slotProps": { "type": { "name": "object" }, "default": "{}" },
"slots": {
"type": { "name": "object" },
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/charts/area-plot.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"props": {
"skipAnimation": { "type": { "name": "bool" }, "default": "false" },
"slotProps": { "type": { "name": "object" }, "default": "{}" },
"slots": {
"type": { "name": "object" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"axisData": {
"type": {
"name": "shape",
"description": "{ x?: { index?: number, value: Date<br>&#124;&nbsp;number }, y?: { index?: number, value: Date<br>&#124;&nbsp;number } }"
"description": "{ x?: { index?: number, value: Date<br>&#124;&nbsp;number<br>&#124;&nbsp;string }, y?: { index?: number, value: Date<br>&#124;&nbsp;number<br>&#124;&nbsp;string } }"
},
"required": true
},
Expand Down
8 changes: 7 additions & 1 deletion docs/pages/x/api/charts/line-chart.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"default": "null"
},
"skipAnimation": { "type": { "name": "bool" }, "default": "false" },
"slotProps": { "type": { "name": "object" }, "default": "{}" },
"slots": {
"type": { "name": "object" },
Expand Down Expand Up @@ -81,7 +82,12 @@
{ "name": "axisTickLabel", "description": "", "class": null },
{ "name": "axisLabel", "description": "", "class": null },
{ "name": "area", "description": "", "class": null },
{ "name": "line", "description": "", "class": null },
{
"name": "line",
"description": "The component that renders the root.",
"default": "LineElementPath",
"class": null
},
{ "name": "mark", "description": "", "class": null },
{ "name": "lineHighlight", "description": "", "class": null },
{ "name": "legend", "description": "", "class": null },
Expand Down
9 changes: 9 additions & 0 deletions docs/pages/x/api/charts/line-element.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"props": {
"skipAnimation": { "type": { "name": "bool" }, "default": "false" },
"slotProps": { "type": { "name": "object" }, "default": "{}" },
"slots": {
"type": { "name": "object" },
Expand All @@ -12,6 +13,14 @@
"import { LineElement } from '@mui/x-charts/LineChart';",
"import { LineElement } from '@mui/x-charts';"
],
"slots": [
{
"name": "line",
"description": "The component that renders the root.",
"default": "LineElementPath",
"class": null
}
],
"classes": [
{
"key": "faded",
Expand Down
10 changes: 9 additions & 1 deletion docs/pages/x/api/charts/line-plot.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"props": {
"skipAnimation": { "type": { "name": "bool" }, "default": "false" },
"slotProps": { "type": { "name": "object" }, "default": "{}" },
"slots": {
"type": { "name": "object" },
Expand All @@ -12,7 +13,14 @@
"import { LinePlot } from '@mui/x-charts/LineChart';",
"import { LinePlot } from '@mui/x-charts';"
],
"slots": [{ "name": "line", "description": "", "class": null }],
"slots": [
{
"name": "line",
"description": "The component that renders the root.",
"default": "LineElementPath",
"class": null
}
],
"classes": [],
"muiName": "MuiLinePlot",
"filename": "/packages/x-charts/src/LineChart/LinePlot.tsx",
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/x/api/charts/mark-element.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"description": "'circle'<br>&#124;&nbsp;'cross'<br>&#124;&nbsp;'diamond'<br>&#124;&nbsp;'square'<br>&#124;&nbsp;'star'<br>&#124;&nbsp;'triangle'<br>&#124;&nbsp;'wye'"
},
"required": true
}
},
"skipAnimation": { "type": { "name": "bool" }, "default": "false" }
},
"name": "MarkElement",
"imports": [
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/charts/mark-plot.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"props": {
"skipAnimation": { "type": { "name": "bool" }, "default": "false" },
"slotProps": { "type": { "name": "object" }, "default": "{}" },
"slots": {
"type": { "name": "object" },
Expand Down
7 changes: 6 additions & 1 deletion docs/pages/x/api/charts/spark-line-chart.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@
],
"slots": [
{ "name": "area", "description": "", "class": null },
{ "name": "line", "description": "", "class": null },
{
"name": "line",
"description": "The component that renders the root.",
"default": "LineElementPath",
"class": null
},
{ "name": "mark", "description": "", "class": null },
{ "name": "lineHighlight", "description": "", "class": null },
{ "name": "bar", "description": "", "class": null },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"componentDescription": "",
"propDescriptions": {
"skipAnimation": { "description": "If <code>true</code>, animations are skiped." },
"slotProps": { "description": "The props used for each component slot." },
"slots": { "description": "Overridable component slots." }
},
Expand Down
1 change: 1 addition & 0 deletions docs/translations/api-docs/charts/area-plot/area-plot.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"componentDescription": "",
"propDescriptions": {
"skipAnimation": { "description": "If <code>true</code>, animations are skiped." },
"slotProps": { "description": "The props used for each component slot." },
"slots": { "description": "Overridable component slots." }
},
Expand Down
3 changes: 2 additions & 1 deletion docs/translations/api-docs/charts/line-chart/line-chart.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"rightAxis": {
"description": "Indicate which axis to display the right of the charts. Can be a string (the id of the axis) or an object <code>ChartsYAxisProps</code>."
},
"skipAnimation": { "description": "If <code>true</code>, animations are skiped." },
"slotProps": { "description": "The props used for each component slot." },
"slots": { "description": "Overridable component slots." },
"topAxis": {
Expand All @@ -54,7 +55,7 @@
"axisTickLabel": "",
"itemContent": "",
"legend": "",
"line": "",
"line": "The component that renders the root.",
"lineHighlight": "",
"mark": "",
"popper": ""
Expand Down
Loading
Loading