축 hooks
축 hooks (Axis hooks)
커스텀 컴포넌트에서 직교(cartesian) 및 극(polar) 차트의 축 설정과 계산된 속성을 읽어봐요.
본문
축 hooks는 직교(x/y) 차트와 극(rotation/radius) 차트 모두의 축 설정과 계산된 속성을 노출해요.
직교 차트 (Cartesian charts)
x축과 y축 설정을 읽으려면 useXAxes(), useYAxes(), useXAxis(), useYAxis()를 사용해요.
모든 축 가져오기 (Get all axes)
주어진 방향의 모든 축을 가져오려면 useXAxes()와 useYAxes()를 사용해요:
import { useXAxes, useYAxes } from '@mui/x-charts/hooks';
function CustomComponent() {
// All x-axes
const { xAxis, xAxisIds } = useXAxes();
// All y-axes
const { yAxis, yAxisIds } = useYAxes();
}
특정 축 가져오기 (Get a specific axis)
ID로 특정 축을 가져오려면 useXAxis()와 useYAxis()를 사용해요.
ID를 생략하면 hook은 기본 축을 반환해요.
import { useXAxis, useYAxis } from '@mui/x-charts/hooks';
function CustomComponent() {
// Default x-axis
const xAxis = useXAxis();
// Specific x-axis
const xAxisCustom = useXAxis('customAxisId');
// Default y-axis
const yAxis = useYAxis();
// Specific y-axis
const yAxisLeft = useYAxis('leftAxis');
}
극 차트 (Polar charts)
극 차트(예: radar 차트)에는 useRotationAxes(), useRadiusAxes(), useRotationAxis(), useRadiusAxis()를 사용해요.
모든 rotation 및 radius 축 가져오기 (Get all rotation and radius axes)
모든 rotation 및 radius 축을 가져오려면 useRotationAxes()와 useRadiusAxes()를 사용해요.
import { useRotationAxes, useRadiusAxes } from '@mui/x-charts/hooks';
function CustomComponent() {
const { rotationAxis, rotationAxisIds } = useRotationAxes();
const { radiusAxis, radiusAxisIds } = useRadiusAxes();
}
특정 축 가져오기 (Get a specific axis)
ID나 인덱스로 특정 축을 가져오려면 useRotationAxis()와 useRadiusAxis()를 사용해요.
import { useRotationAxis, useRadiusAxis } from '@mui/x-charts/hooks';
function CustomComponent() {
// Default rotation axis
const rotationAxis = useRotationAxis();
// Specific rotation axis
const rotationAxisCustom = useRotationAxis('customAxisId');
// Access by index
const radiusAxis = useRadiusAxis(0);
// Access by ID
const radiusAxisById = useRadiusAxis('radiusAxisId');
}
사용 예제 (Usage example)
아래 예제는 이중 y축이 있는 차트에서 useXAxes()와 useYAxes()가 모든 축을 읽는 것을 보여줘요.
커스텀 컴포넌트는 이 hook들을 사용해 축 범위에 대한 시각적 표시를 그리고 축 메타데이터를 표시해요.
import * as React from 'react';
import { useYAxes, useXAxis, useLineSeries } from '@mui/x-charts/hooks';
import { LinePlot } from '@mui/x-charts/LineChart';
import { ChartsXAxis } from '@mui/x-charts/ChartsXAxis';
import { ChartsYAxis } from '@mui/x-charts/ChartsYAxis';
import { ChartsDataProvider } from '@mui/x-charts/ChartsDataProvider';
import { ChartsSurface } from '@mui/x-charts/ChartsSurface';
import { useTheme } from '@mui/material/styles';
const dataset = [
{ month: 'Jan', temperature: 5, rainfall: 45 },
{ month: 'Feb', temperature: 7, rainfall: 38 },
{ month: 'Mar', temperature: 12, rainfall: 42 },
{ month: 'Apr', temperature: 16, rainfall: 35 },
{ month: 'May', temperature: 21, rainfall: 28 },
{ month: 'Jun', temperature: 25, rainfall: 22 },
];
function AxisRangeIndicators() {
const { yAxis, yAxisIds } = useYAxes();
const xAxis = useXAxis<'band'>();
const allSeries = useLineSeries();
const theme = useTheme();
const xDomain = xAxis.scale.domain();
const xStart = xAxis.scale(xDomain[0]) ?? 0;
const xEnd = xAxis.scale(xDomain[xDomain.length - 1]) ?? 0;
return (
<g>
{allSeries.map((series) => {
const axis = yAxis[series.yAxisId ?? yAxisIds[0]];
const seriesMin = Math.min(
...(series.data.filter((v) => v != null) as number[]),
);
const seriesMax = Math.max(
...(series.data.filter((v) => v != null) as number[]),
);
const yMin = axis.scale(seriesMin) ?? 0;
const yMax = axis.scale(seriesMax) ?? 0;
const color = series.color;
return (
<g key={series.id}>
{/* Min value horizontal line */}
<line
x1={xStart}
x2={xEnd}
y1={yMin}
y2={yMin}
stroke={color}
strokeWidth={1.5}
strokeDasharray="5 5"
opacity={0.6}
/>
{/* Max value horizontal line */}
<line
x1={xStart}
x2={xEnd}
y1={yMax}
y2={yMax}
stroke={color}
strokeWidth={1.5}
strokeDasharray="5 5"
opacity={0.6}
/>
{/* Min value label */}
<text
x={(xStart + xEnd) / 2}
y={yMin - 6}
fontSize="12"
fill={theme.palette.text.secondary}
fontWeight="bold"
alignmentBaseline="middle"
textAnchor="middle"
>
{axis.id}
</text>
<text
x={xEnd + 6}
y={yMin - 6}
fontSize="12"
fill={theme.palette.text.secondary}
fontWeight="bold"
alignmentBaseline="middle"
textAnchor="start"
>
min
</text>
{/* Max value label */}
<text
x={(xStart + xEnd) / 2}
y={yMax - 6}
fontSize="12"
fill={theme.palette.text.secondary}
fontWeight="bold"
alignmentBaseline="middle"
textAnchor="middle"
>
{axis.id}
</text>
<text
x={xEnd + 6}
y={yMax - 6}
fontSize="12"
fill={theme.palette.text.secondary}
fontWeight="bold"
alignmentBaseline="middle"
textAnchor="start"
>
max
</text>
</g>
);
})}
</g>
);
}
export default function UseAxes() {
return (
<div style={{ width: '100%' }}>
<ChartsDataProvider
dataset={dataset}
xAxis={[
{
id: 'months',
scaleType: 'point',
dataKey: 'month',
},
]}
yAxis={[
{
id: 'temperature',
scaleType: 'linear',
min: 0,
max: 30,
position: 'left',
label: 'Temperature (°C)',
width: 50,
labelStyle: {
translate: '5px',
},
},
{
id: 'rainfall',
scaleType: 'linear',
min: 0,
max: 50,
position: 'left',
label: 'Rainfall (mm)',
width: 50,
labelStyle: {
translate: '5px',
},
},
]}
series={[
{
type: 'line',
dataKey: 'temperature',
label: 'Temperature (°C)',
yAxisId: 'temperature',
color: '#1976d2',
},
{
type: 'line',
dataKey: 'rainfall',
label: 'Rainfall (mm)',
yAxisId: 'rainfall',
color: '#dc004e',
},
]}
height={300}
margin={{ right: 40, left: 0 }}
>
<ChartsSurface>
<LinePlot />
<ChartsXAxis />
<ChartsYAxis axisId="temperature" />
<ChartsYAxis axisId="rainfall" />
<AxisRangeIndicators />
</ChartsSurface>
</ChartsDataProvider>
</div>
);
}
주의사항 (Caveats)
이 hook들은 차트 컨텍스트 안에서만 사용할 수 있어요. 사용 요구사항은 hooks 개요를 참고하세요.