Charts - 방사형 라인
Charts - 방사형 라인 (Radial Lines)
방사형 라인 차트로 주기적인 값의 추세를 보여줄 수 있어요.
본문
기본 (Basics)
RadialLineChart 컴포넌트는 series, rotationAxis, radiusAxis prop을 받아 극좌표(polar coordinates)로 데이터를 렌더링해요.
import * as React from 'react';
import { RadialLineChart } from '@mui/x-charts-premium/RadialLineChart';
import { dataset } from '../dataset/weather';
export default function BasicRadialLineChart() {
return (
<RadialLineChart
height={400}
dataset={dataset}
series={[
{
dataKey: 'london',
label: 'London precipitation (mm)',
curve: 'natural',
showMark: true,
},
]}
rotationAxis={[{ scaleType: 'point', dataKey: 'month', disableLine: true }]}
radiusAxis={[{ disableLine: true }]}
grid={{ rotation: true, radius: true }}
/>
);
}
경로 닫기 (Closing path)
시리즈의 마지막 지점과 첫 번째 지점 사이에 선을 그리려면 시리즈 속성 closePath를 true로 설정해요.
import * as React from 'react';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import FormControlLabel from '@mui/material/FormControlLabel';
import Checkbox from '@mui/material/Checkbox';
import { RadialLineChart } from '@mui/x-charts-premium/RadialLineChart';
const months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
export default function ClosedOpenRadialLineChart() {
const [area, setArea] = React.useState(false);
const [closePath, setClosePath] = React.useState(true);
return (
<Box sx={{ width: '100%' }}>
<Stack direction="row" sx={{ flexWrap: 'wrap', gap: 2, width: '100%' }}>
<FormControlLabel
checked={area}
control={<Checkbox onChange={(event) => setArea(event.target.checked)} />}
label="area"
labelPlacement="end"
/>
<FormControlLabel
checked={closePath}
control={
<Checkbox onChange={(event) => setClosePath(event.target.checked)} />
}
label="closePath"
labelPlacement="end"
/>
</Stack>
<RadialLineChart
height={300}
series={[
{
data: [3, 5, 7, 10, 12, 15, 18, 16, 13, 9, 6, 4],
label: 'Temperature',
area,
closePath,
},
]}
rotationAxis={[{ scaleType: 'point', data: months, disableLine: true }]}
radiusAxis={[{ disableLine: true }]}
grid={{ rotation: true, radius: true }}
/>
</Box>
);
}
축 (Axes)
라인 시리즈와 마찬가지로 회전 축(rotation axis)은 어떤 스케일 타입도 가질 수 있고, 반경 축(radius axis)은 연속 스케일 타입을 사용할 수 있어요.
방사형 축 구성에 대한 자세한 내용은 전용 페이지를 참고하세요.
다음은 연속 스케일 타입의 회전 축 예시예요.
import * as React from 'react';
import { RadialLineChart } from '@mui/x-charts-premium/RadialLineChart';
const SAMPLES = 200;
const angles = Array.from(
{ length: SAMPLES + 1 },
(_, i) => (i * 2 * Math.PI) / SAMPLES,
);
const cardioid = angles.map((angle) => 100 * (1 + Math.cos(angle * 3)));
export default function ContinuousRadialLineChart() {
return (
<RadialLineChart
height={400}
series={[
{
data: cardioid,
label: 'Cardioid',
curve: 'linear',
valueFormatter: (value: number | null) =>
value == null ? '' : `${Math.round(value)}`,
},
]}
rotationAxis={[
{
data: angles,
domainLimit: () => ({ min: 0, max: 2 * Math.PI }),
tickInterval: [0, Math.PI / 2, Math.PI, (3 * Math.PI) / 2, 2 * Math.PI],
valueFormatter: (value: number) =>
`${Math.round((value * 180) / Math.PI)}°`,
},
]}
radiusAxis={[{ position: 'none' }]}
grid={{ rotation: true, radius: true }}
/>
);
}
마크 (Marks)
showMark: true를 추가하면 마크를 표시할 수 있어요.
마크를 수정하려면 shape 속성을 사용해요. 'circle', 'square', 'diamond', 'cross', 'star', 'triangle', 'wye' 7가지 모양을 지원해요.
import * as React from 'react';
import Stack from '@mui/material/Stack';
import Box from '@mui/material/Box';
import TextField from '@mui/material/TextField';
import MenuItem from '@mui/material/MenuItem';
import { RadialLineChart } from '@mui/x-charts-premium/RadialLineChart';
import { dataset } from '../dataset/weather';
type Shape = 'circle' | 'square' | 'diamond' | 'cross' | 'star' | 'triangle' | 'wye';
const shapes: Shape[] = [
'circle',
'square',
'diamond',
'cross',
'star',
'triangle',
'wye',
];
export default function RadialLineMarkShape() {
const [shape, setShape] = React.useState<Shape>('circle');
return (
<Stack
direction={{ xs: 'column', md: 'row' }}
spacing={1}
sx={{ width: '100%', padding: 2 }}
>
<TextField
select
label="shape"
value={shape}
onChange={(event) => setShape(event.target.value as Shape)}
sx={{ minWidth: 150 }}
>
{shapes.map((option) => (
<MenuItem key={option} value={option}>
{option}
</MenuItem>
))}
</TextField>
<Box sx={{ flexGrow: 1 }}>
<RadialLineChart
height={400}
dataset={dataset}
series={[
{
dataKey: 'london',
label: 'London precipitation (mm)',
curve: 'natural',
showMark: true,
shape,
},
]}
rotationAxis={[
{ scaleType: 'point', dataKey: 'month', disableLine: true },
]}
radiusAxis={[{ disableLine: true }]}
grid={{ rotation: true, radius: true }}
/>
</Box>
</Stack>
);
}
하이라이트 (Highlight)
다른 시리즈와 마찬가지로 방사형 라인 시리즈에도 highlight와 fade 속성을 가진 객체를 받는 highlightScope 속성이 있어요.
:::info 방사형 라인 상호작용은 pointer 기반 상호작용을 사용해요. :::
import * as React from 'react';
import Stack from '@mui/material/Stack';
import Box from '@mui/material/Box';
import FormControlLabel from '@mui/material/FormControlLabel';
import Switch from '@mui/material/Switch';
import TextField from '@mui/material/TextField';
import MenuItem from '@mui/material/MenuItem';
import { RadialLineChart } from '@mui/x-charts-premium/RadialLineChart';
type HighlightOptions = 'none' | 'item' | 'series';
type FadeOptions = 'none' | 'series' | 'global';
const params = {
series: [
{
data: [3, 4, 1, 6, 5],
label: 'A',
area: false,
stack: 'total',
},
{ data: [4, 3, 1, 5, 8], label: 'B', area: false, stack: 'total' },
{
data: [4, 2, 5, 4, 1],
label: 'C',
area: false,
stack: 'total',
},
],
rotationAxis: [
{
scaleType: 'point' as const,
data: [1, 2, 3, 4, 5],
position: 'none' as const,
},
],
radiusAxis: [{ position: 'none' as const }],
height: 400,
grid: { rotation: true, radius: true },
};
export default function ElementHighlights() {
const [withArea, setWithArea] = React.useState(false);
const [highlight, setHighlight] = React.useState<HighlightOptions>('item');
const [fade, setFade] = React.useState<FadeOptions>('global');
return (
<Stack
direction={{ xs: 'column', xl: 'row' }}
spacing={1}
sx={{ width: '100%' }}
>
<Box sx={{ flexGrow: 1 }}>
<RadialLineChart
{...params}
series={params.series.map((series) => ({
...series,
area: withArea,
highlightScope: {
highlight,
fade,
},
}))}
/>
</Box>
<Stack
direction={{ xs: 'row', xl: 'column' }}
spacing={3}
useFlexGap
sx={{ justifyContent: 'center', flexWrap: 'wrap' }}
>
<TextField
select
label="highlight"
value={highlight}
onChange={(event) => setHighlight(event.target.value as HighlightOptions)}
sx={{ minWidth: 150 }}
>
<MenuItem value={'none'}>none</MenuItem>
<MenuItem value={'item'}>item</MenuItem>
<MenuItem value={'series'}>series</MenuItem>
</TextField>
<TextField
select
label="fade"
value={fade}
onChange={(event) => setFade(event.target.value as FadeOptions)}
sx={{ minWidth: 150 }}
>
<MenuItem value={'none'}>none</MenuItem>
<MenuItem value={'series'}>series</MenuItem>
<MenuItem value={'global'}>global</MenuItem>
</TextField>
<FormControlLabel
control={
<Switch
checked={withArea}
onChange={(event) => setWithArea(event.target.checked)}
/>
}
label="Fill line area"
/>
</Stack>
</Stack>
);
}
클릭 이벤트 (Click events)
RadialLineChart는 두 개의 클릭 핸들러를 제공해요:
- 특정 라인 클릭을 위한
onItemClick - 차트 영역 어디든 클릭을 위한
onAxisClick
둘 다 다음과 같은 시그니처를 제공해요:
const clickHandler = (
event, // The mouse event.
params, // An object that identifies the clicked elements.
) => {};
import * as React from 'react';
import Stack from '@mui/material/Stack';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import IconButton from '@mui/material/IconButton';
import UndoOutlinedIcon from '@mui/icons-material/UndoOutlined';
import {
RadialLineChart,
RadialLineSeries,
} from '@mui/x-charts-premium/RadialLineChart';
import { HighlightedCode } from '@mui/internal-core-docs/HighlightedCode';
import { ChartsAxisData } from '@mui/x-charts/models';
import { RadialLineItemIdentifier } from '@mui/x-charts-premium/models';
const highlightScope = { highlight: 'item' } as const;
const series: RadialLineSeries[] = [
{
id: 'series-1',
data: [3, 4, 1, 6, 5],
label: 'A',
stack: 'total',
highlightScope,
},
{
id: 'series-2',
data: [4, 3, 1, 5, 8],
label: 'B',
stack: 'total',
highlightScope,
},
{
id: 'series-3',
data: [4, 2, 5, 4, 1],
label: 'C',
highlightScope,
},
];
const radialChartParams = {
series,
rotationAxis: [
{
data: [0, 3, 6, 9, 12],
id: 'axis1',
disableLine: true,
disableTicks: true,
},
],
radiusAxis: [{ position: 'none' as const }],
grid: { rotation: true, radius: true },
height: 400,
};
export default function RadialLineClick() {
const [itemData, setItemData] = React.useState<RadialLineItemIdentifier>();
const [axisData, setAxisData] = React.useState<ChartsAxisData | null>();
return (
<Stack
direction={{ xs: 'column', md: 'row' }}
spacing={{ xs: 0, md: 4 }}
sx={{ width: '100%' }}
>
<Box sx={{ flexGrow: 1 }}>
<Box sx={{ flexGrow: 1 }}>
<RadialLineChart
{...radialChartParams}
onItemClick={(event, d) => setItemData(d)}
onAxisClick={(event, d) => setAxisData(d)}
/>
</Box>
</Box>
<Stack direction="column" sx={{ width: { xs: '100%', md: '40%' } }}>
<Box
sx={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<Typography>Click on the chart</Typography>
<IconButton
aria-label="reset"
size="small"
onClick={() => {
setItemData(undefined);
setAxisData(null);
}}
>
<UndoOutlinedIcon fontSize="small" />
</IconButton>
</Box>
<HighlightedCode
code={`// Data from item click
${itemData ? JSON.stringify(itemData, null, 2) : '// The data will appear here'}
// Data from axis click
${axisData ? JSON.stringify(axisData, null, 2) : '// The data will appear here'}
`}
language="json"
copyButtonHidden
/>
</Stack>
</Stack>
);
}
:::info
onItemClick의 event와 onAxisClick의 event 사이에는 약간의 차이가 있어요:
onItemClick의 event는 차트 컨테이너가 발생시키는 React 합성 마우스 이벤트예요. 방사형 라인에는 요소별 클릭 핸들러가 없으므로, 클릭된 라인은 포인터 위치로부터 결정돼요.onAxisClick의 event는 SVG 컴포넌트가 발생시키는 네이티브 마우스 이벤트예요.
:::
컴포지션 (Composition)
컴포지션을 위해 ChartsRadialDataProviderPremium을 사용해 series, rotationAxis, radiusAxis prop을 제공해요.
컴포지션에 사용 가능한 공통 차트 컴포넌트 외에도, RadialLinePlot, RadialAreaPlot, RadialMarkPlot, RadialLineHighlightPlot을 사용해 라인, 영역, 마크, 하이라이트 표시기를 그릴 수 있어요.
방사형 라인 차트가 이렇게 구성돼요:
<ChartsRadialDataProviderPremium>
<ChartsWrapper>
<ChartsLegend />
<ChartsSurface>
<ChartsRadialGrid />
<g clipPath={`url(#${clipPathId})`}>
<RadialAreaPlot />
<RadialLinePlot />
<ChartsOverlay />
</g>
<ChartsRadialAxisHighlight />
<ChartsRotationAxis />
<ChartsRadiusAxis />
<RadialMarkPlot />
<RadialLineHighlightPlot />
<ChartsClipPath id={clipPathId} />
</ChartsSurface>
<ChartsTooltip />
</ChartsWrapper>
</ChartsRadialDataProviderPremium>