-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Document how to customize a subsection of a line chart (mui#13210
) Signed-off-by: Alexandre Fauquette <[email protected]> Co-authored-by: Jose C Quintas Jr <[email protected]>
- Loading branch information
Showing
4 changed files
with
173 additions
and
0 deletions.
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,70 @@ | ||
import * as React from 'react'; | ||
import { LineChart, AnimatedLine } from '@mui/x-charts/LineChart'; | ||
import { useChartId, useDrawingArea, useXScale } from '@mui/x-charts/hooks'; | ||
|
||
function CustomAnimatedLine(props) { | ||
const { limit, sxBefore, sxAfter, ...other } = props; | ||
const { top, bottom, height, left, width } = useDrawingArea(); | ||
const scale = useXScale(); | ||
const chartId = useChartId(); | ||
|
||
if (limit === undefined) { | ||
return <AnimatedLine {...other} />; | ||
} | ||
|
||
const limitPosition = scale(limit); // Convert value to x coordinate. | ||
|
||
if (limitPosition === undefined) { | ||
return <AnimatedLine {...other} />; | ||
} | ||
|
||
const clipIdleft = `${chartId}-${props.ownerState.id}-line-limit-${limit}-1`; | ||
const clipIdRight = `${chartId}-${props.ownerState.id}-line-limit-${limit}-2`; | ||
return ( | ||
<React.Fragment> | ||
{/* Clip to show the line before the limit */} | ||
<clipPath id={clipIdleft}> | ||
<rect | ||
x={left} | ||
y={0} | ||
width={limitPosition - left} | ||
height={top + height + bottom} | ||
/> | ||
</clipPath> | ||
{/* Clip to show the line after the limit */} | ||
<clipPath id={clipIdRight}> | ||
<rect | ||
x={limitPosition} | ||
y={0} | ||
width={left + width - limitPosition} | ||
height={top + height + bottom} | ||
/> | ||
</clipPath> | ||
<g clipPath={`url(#${clipIdleft})`}> | ||
<AnimatedLine {...other} sx={sxBefore} /> | ||
</g> | ||
<g clipPath={`url(#${clipIdRight})`}> | ||
<AnimatedLine {...other} sx={sxAfter} /> | ||
</g> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
export default function LineWithPrediction() { | ||
return ( | ||
<LineChart | ||
series={[ | ||
{ | ||
type: 'line', | ||
data: [1, 2, 3, 4, 1, 2, 3, 4, 5], | ||
valueFormatter: (v, i) => `${v}${i.dataIndex > 5 ? ' (estimated)' : ''}`, | ||
}, | ||
]} | ||
xAxis={[{ data: [0, 1, 2, 3, 4, 5, 6, 7, 8] }]} | ||
height={200} | ||
width={400} | ||
slots={{ line: CustomAnimatedLine }} | ||
slotProps={{ line: { limit: 5, sxAfter: { strokeDasharray: '10 5' } } }} | ||
/> | ||
); | ||
} |
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,77 @@ | ||
import * as React from 'react'; | ||
import { LineChart, AnimatedLine, AnimatedLineProps } from '@mui/x-charts/LineChart'; | ||
import { useChartId, useDrawingArea, useXScale } from '@mui/x-charts/hooks'; | ||
import { SxProps, Theme } from '@mui/system'; | ||
|
||
interface CustomAnimatedLineProps extends AnimatedLineProps { | ||
limit?: number; | ||
sxBefore?: SxProps<Theme>; | ||
sxAfter?: SxProps<Theme>; | ||
} | ||
|
||
function CustomAnimatedLine(props: CustomAnimatedLineProps) { | ||
const { limit, sxBefore, sxAfter, ...other } = props; | ||
const { top, bottom, height, left, width } = useDrawingArea(); | ||
const scale = useXScale(); | ||
const chartId = useChartId(); | ||
|
||
if (limit === undefined) { | ||
return <AnimatedLine {...other} />; | ||
} | ||
|
||
const limitPosition = scale(limit); // Convert value to x coordinate. | ||
|
||
if (limitPosition === undefined) { | ||
return <AnimatedLine {...other} />; | ||
} | ||
|
||
const clipIdleft = `${chartId}-${props.ownerState.id}-line-limit-${limit}-1`; | ||
const clipIdRight = `${chartId}-${props.ownerState.id}-line-limit-${limit}-2`; | ||
return ( | ||
<React.Fragment> | ||
{/* Clip to show the line before the limit */} | ||
<clipPath id={clipIdleft}> | ||
<rect | ||
x={left} | ||
y={0} | ||
width={limitPosition - left} | ||
height={top + height + bottom} | ||
/> | ||
</clipPath> | ||
{/* Clip to show the line after the limit */} | ||
<clipPath id={clipIdRight}> | ||
<rect | ||
x={limitPosition} | ||
y={0} | ||
width={left + width - limitPosition} | ||
height={top + height + bottom} | ||
/> | ||
</clipPath> | ||
<g clipPath={`url(#${clipIdleft})`}> | ||
<AnimatedLine {...other} sx={sxBefore} /> | ||
</g> | ||
<g clipPath={`url(#${clipIdRight})`}> | ||
<AnimatedLine {...other} sx={sxAfter} /> | ||
</g> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
export default function LineWithPrediction() { | ||
return ( | ||
<LineChart | ||
series={[ | ||
{ | ||
type: 'line', | ||
data: [1, 2, 3, 4, 1, 2, 3, 4, 5], | ||
valueFormatter: (v, i) => `${v}${i.dataIndex > 5 ? ' (estimated)' : ''}`, | ||
}, | ||
]} | ||
xAxis={[{ data: [0, 1, 2, 3, 4, 5, 6, 7, 8] }]} | ||
height={200} | ||
width={400} | ||
slots={{ line: CustomAnimatedLine }} | ||
slotProps={{ line: { limit: 5, sxAfter: { strokeDasharray: '10 5' } } as any }} | ||
/> | ||
); | ||
} |
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,14 @@ | ||
<LineChart | ||
series={[ | ||
{ | ||
type: 'line', | ||
data: [1, 2, 3, 4, 1, 2, 3, 4, 5], | ||
valueFormatter: (v, i) => `${v}${i.dataIndex > 5 ? ' (estimated)' : ''}`, | ||
}, | ||
]} | ||
xAxis={[{ data: [0, 1, 2, 3, 4, 5, 6, 7, 8] }]} | ||
height={200} | ||
width={400} | ||
slots={{ line: CustomAnimatedLine }} | ||
slotProps={{ line: { limit: 5, sxAfter: { strokeDasharray: '10 5' } } as any }} | ||
/> |
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