`useDrawingArea()` 사용법
useDrawingArea() 사용법
차트의 드로잉 영역(drawing area) 크기와 좌표를 얻어서 커스텀 콘텐츠를 배치할 수 있어요.
useDrawingArea()는 차트의 드로잉 영역 크기와 위치를 반환해요. 이 값을 사용해 차트 영역 안에 커스텀 요소를 배치할 수 있어요.
본문
사용법 (Usage)
import { useDrawingArea } from '@mui/x-charts/hooks';
function CustomOverlay() {
const { left, right, top, bottom, width, height } = useDrawingArea();
// Position custom elements within the chart area
}
아래 데모에서는 다음을 보여줘요:
- 드로잉 영역의 정확한 경계를 나타내는 빨간 점선 테두리
- 드로잉 영역 각 모서리에 표시되는 모서리 마커
- 드로잉 영역의 중앙을 표시하는 중앙 십자선
- 실시간 크기 및 중심점 정보
import { useDrawingArea } from '@mui/x-charts/hooks';
import { LineChart } from '@mui/x-charts/LineChart';
function CustomOverlay() {
const { left, top, width, height } = useDrawingArea();
return (
<g>
{/* Drawing area border */}
<rect
x={left}
y={top}
width={width}
height={height}
fill="transparent"
stroke="red"
strokeWidth={2}
strokeDasharray="5,5"
/>
{/* Corner markers */}
<circle cx={left} cy={top} r={4} fill="red" />
<circle cx={width + left} cy={top} r={4} fill="red" />
<circle cx={width + left} cy={top + height} r={4} fill="red" />
<circle cx={left} cy={top + height} r={4} fill="red" />
{/* Center cross */}
<g stroke="red" strokeWidth={1}>
<line
x1={left + width / 2 - 10}
y1={top + height / 2}
x2={left + width / 2 + 10}
y2={top + height / 2}
/>
<line
x1={left + width / 2}
y1={top + height / 2 - 10}
x2={left + width / 2}
y2={top + height / 2 + 10}
/>
</g>
{/* Info text */}
<text
x={left + 10}
y={top + 20}
fontSize="12"
fill="red"
fontFamily="monospace"
>
Drawing Area: {width}×{height}
</text>
<text
x={left + 10}
y={top + 35}
fontSize="12"
fill="red"
fontFamily="monospace"
>
Center: ({left + width / 2}, {top + height / 2})
</text>
</g>
);
}
export default function UseDrawingArea() {
return (
<LineChart
dataset={[
{ x: 1, y: 2 },
{ x: 2, y: 5 },
{ x: 3, y: 3 },
{ x: 4, y: 8 },
{ x: 5, y: 1 },
{ x: 6, y: 7 },
]}
xAxis={[{ dataKey: 'x', label: 'X Axis' }]}
yAxis={[{ label: 'Y Axis' }]}
series={[{ dataKey: 'y', label: 'Sample Data', color: '#8884d8' }]}
height={400}
hideLegend
>
<CustomOverlay />
</LineChart>
);
}
반환 값 (Return value)
useDrawingArea()는 다음과 같은 속성을 가진 객체를 반환해요:
| Property | Type | Description |
|---|---|---|
left |
number |
The gap between the left border of the SVG and the drawing area. |
right |
number |
The gap between the right border of the SVG and the drawing area. |
top |
number |
The gap between the top border of the SVG and the drawing area. |
bottom |
number |
The gap between the bottom border of the SVG and the drawing area. |
width |
number |
The width of the drawing area. |
height |
number |
The height of the drawing area. |
주의사항 (Caveats)
이 훅은 차트 컨텍스트 안에서만 사용할 수 있어요. 사용 요구사항은 hooks overview 문서를 확인해 주세요.