본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

차트 - 라벨

원문 보기 위키 갱신

차트에서 시리즈와 데이터 포인트가 어떻게 표시되는지(라벨링)를 커스터마이즈하는 방법을 다룹니다.

라벨은 차트에서 시리즈나 데이터 포인트를 식별하는 텍스트로, 범례(legend), 툴팁(tooltip), 또는 차트 요소 위에 직접 표시될 수 있습니다.

시리즈의 label 속성에 문자열을 전달하면 시리즈 라벨을 설정할 수 있습니다. 이 라벨은 범례와 툴팁 등 여러 위치에 표시됩니다.

:::info Pie 차트는 아래의 Pie 차트 라벨에서 설명하는 특별한 동작을 가집니다. :::

import { BarChart } from '@mui/x-charts/BarChart';

export default function BasicLabel() {
  return (
    <BarChart
      {...props}
      series={[
        {
          data: [2400, 1398, 9800],
          label: 'label 1',
        },
      ]}
    />
  );
}

const props = {
  height: 300,
  xAxis: [{ data: ['A', 'B', 'C'] }],
  yAxis: [{ width: 50 }],
};

조건부 서식 (Conditional formatting)

label 속성은 위치(location)에 따라 라벨 내용을 바꿀 수 있는 함수를 받습니다. 이 함수는 첫 번째 인자로 location을 받으며, 다음 값들을 가질 수 있습니다:

  • 'legend': 범례에서 라벨을 표시합니다
  • 'tooltip': 툴팁에서 라벨을 표시합니다
import { BarChart } from '@mui/x-charts/BarChart';

export default function FunctionLabel() {
  return (
    <BarChart
      {...props}
      series={[
        { data: [2400, 1398, 9800], label: 'simple label' },
        { data: [500, 2398, 4300], label: (location) => `${location} label` },
      ]}
    />
  );
}

const props = {
  height: 300,
  xAxis: [{ data: ['A', 'B', 'C'] }],
  yAxis: [{ width: 50 }],
};

Pie 차트 라벨

Pie 차트는 그 특성상 다른 동작을 보입니다. 시리즈별이 아니라 슬라이스(slice)별로 라벨을 가지며, 라벨을 렌더링할 수 있는 추가 위치도 하나 더 제공합니다. 라벨을 시리즈의 일부로 받는 대신, 시리즈 안의 data 집합의 일부로 받습니다.

location 인자는 다음 값들을 가질 수 있습니다:

  • 'legend': 범례에서 라벨을 표시합니다
  • 'tooltip': 툴팁에서 라벨을 표시합니다
  • 'arc': arcLabel이 'label'로 설정되었을 때 Arc 라벨을 표시합니다
import { PieChart } from '@mui/x-charts/PieChart';

export default function PieLabel() {
  return (
    <PieChart
      {...props}
      series={[
        {
          data: [
            { id: 0, value: 10, label: (location) => `${location}+A` },
            { id: 1, value: 15, label: (location) => `${location}+B` },
            { id: 2, value: 20, label: (location) => `${location}+C` },
          ],
          type: 'pie',
          arcLabel: 'label',
        },
      ]}
    />
  );
}

const props = {
  width: 200,
  height: 200,
};

더 알아보기 (Learn more)