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

Tailwind CSS 차트 (Charts)

원문 보기 위키 갱신

Flowbite Library의 차트 컴포넌트는 MIT 라이선스로 오픈소스이며 Tailwind CSS의 유틸리티 클래스로 스타일링되고 오픈소스 ApexCharts 라이브러리를 기반으로 해요.

area, bar, column, pie, radial과 같은 광범위한 반응형 차트 유형 컬렉션을 제공해서 관리자 대시보드 레이아웃, 분석, 다이어그램 등에서 복잡한 데이터를 그래프 안에 시각화하는 데 도움을 줘요.

이 차트들의 색상, 크기, 옵션도 Tailwind CSS로 직접 또는 ApexCharts 라이브러리의 JavaScript 옵션으로 쉽게 커스터마이징할 수 있어요. 다크 모드도 기본 지원돼요.

출처: 문서

본문

시작하기 (Getting started)

계속 진행하기 전에 프로젝트에 Tailwind CSS, Flowbite, ApexCharts가 설치되어 있는지 확인하세요.

Flowbite의 Quickstart 가이드를 따라 플러그인 스타일과 기능을 설치하고, ApexCharts가 프로젝트에 설치·구성되어 있는지 확인하세요.

NPM으로 ApexCharts를 설치하고 package.json 파일에 저장하세요:

npm install apexcharts@3.46.0 --save

또는 CDN 링크를 포함해도 돼요:

<script src="https://cdn.jsdelivr.net/npm/apexcharts@3.46.0/dist/apexcharts.min.js"></script>

모든 라이브러리를 설치했으면 아래 차트 예시들을 코드에 복사-붙여넣기할 수 있어요.

영역 차트 (Area chart)

이 예시는 JavaScript에서 type: "area" 옵션을 설정해 기본 영역 차트를 보여줘요.

<div class="max-w-sm w-full bg-neutral-primary-soft border border-default rounded-base shadow-xs p-4 md:p-6">
  <div class="flex justify-between items-start">
    <div>
      <h5 class="text-2xl font-semibold text-heading">32.4k</h5>
      <p class="text-body">Users this week</p>
    </div>
    <div class="flex items-center px-2.5 py-0.5 font-medium text-fg-success text-center">
      <svg class="w-5 h-5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v13m0-13 4 4m-4-4-4 4"/></svg>
      12%
    </div>
  </div>
  <div id="area-chart"></div>
  <div class="grid grid-cols-1 items-center border-light border-t justify-between">
    <div class="flex justify-between items-center pt-4 md:pt-6">
      <!-- Button -->
      <button id="dropdownDefaultButton" data-dropdown-toggle="lastDaysdropdown" data-dropdown-placement="bottom" class="text-sm font-medium text-body hover:text-heading text-center inline-flex items-center" type="button">
          Last 7 days
          <svg class="w-4 h-4 ms-1.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m19 9-7 7-7-7"/></svg>
      </button>
      <!-- Dropdown menu -->
      <div id="lastDaysdropdown" class="z-10 hidden bg-neutral-primary-medium border border-default-medium rounded-base shadow-lg w-44">
          <ul class="p-2 text-sm text-body font-medium" aria-labelledby="dropdownDefaultButton">
            <li>
              <a href="#" class="inline-flex items-center w-full p-2 hover:bg-neutral-tertiary-medium hover:text-heading rounded">Yesterday</a>
            </li>
            <li>
              <a href="#" class="inline-flex items-center w-full p-2 hover:bg-neutral-tertiary-medium hover:text-heading rounded">Today</a>
            </li>
            <li>
              <a href="#" class="inline-flex items-center w-full p-2 hover:bg-neutral-tertiary-medium hover:text-heading rounded">Last 7 days</a>
            </li>
            <li>
              <a href="#" class="inline-flex items-center w-full p-2 hover:bg-neutral-tertiary-medium hover:text-heading rounded">Last 30 days</a>
            </li>
            <li>
              <a href="#" class="inline-flex items-center w-full p-2 hover:bg-neutral-tertiary-medium hover:text-heading rounded">Last 90 days</a>
            </li>
          </ul>
      </div>
      <a href="#" class="inline-flex items-center text-fg-brand bg-transparent box-border border border-transparent hover:bg-neutral-secondary-medium focus:ring-4 focus:ring-neutral-tertiary font-medium leading-5 rounded-base text-sm px-3 py-2 focus:outline-none">
        Users Report
        <svg class="w-4 h-4 ms-1.5 -me-0.5 rtl:rotate-180" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 12H5m14 0-4 4m4-4-4-4"/></svg>
      </a>
    </div>
  </div>
</div>

그리고 이 차트의 기능을 담당하는 ApexCharts JavaScript 옵션을 추가하세요:

// Get the CSS variable --color-brand and convert it to hex for ApexCharts
const getBrandColor = () => {
  // Get the computed style of the document's root element
  const computedStyle = getComputedStyle(document.documentElement);
  
  // Get the value of the --color-brand CSS variable
  return computedStyle.getPropertyValue('--color-fg-brand').trim() || "#1447E6";
};

const brandColor = getBrandColor();

const options = {
  chart: {
    height: "100%",
    maxWidth: "100%",
    type: "area",
    fontFamily: "Inter, sans-serif",
    dropShadow: {
      enabled: false,
    },
    toolbar: {
      show: false,
    },
  },
  tooltip: {
    enabled: true,
    x: {
      show: false,
    },
  },
  fill: {
    type: "gradient",
    gradient: {
      opacityFrom: 0.55,
      opacityTo: 0,
      shade: brandColor,
      gradientToColors: [brandColor],
    },
  },
  dataLabels: {
    enabled: false,
  },
  stroke: {
    width: 6,
  },
  grid: {
    show: false,
    strokeDashArray: 4,
    padding: {
      left: 2,
      right: 2,
      top: 0
    },
  },
  series: [
    {
      name: "New users",
      data: [6500, 6418, 6456, 6526, 6356, 6456],
      color: brandColor,
    },
  ],
  xaxis: {
    categories: ['01 February', '02 February', '03 February', '04 February', '05 February', '06 February', '07 February'],
    labels: {
      show: false,
    },
    axisBorder: {
      show: false,
    },
    axisTicks: {
      show: false,
    },
  },
  yaxis: {
    show: false,
  },
}

if (document.getElementById("area-chart") && typeof ApexCharts !== 'undefined') {
  const chart = new ApexCharts(document.getElementById("area-chart"), options);
  chart.render();
}

선 차트 (Line chart)

type: "line" 옵션을 설정해 기본 선 차트를 보여주는 예시예요. 시간에 따른 추세를 깔끔한 선으로 보여줄 때 유용해요.

컬럼 차트 (Column chart)

type: "bar"(세로 방향, 컬럼) 옵션을 설정해 카테고리별 값을 세로 막대로 보여주는 예시예요. 각 항목의 수치를 비교할 때 적합해요.

막대 차트 (Bar chart)

type: "bar"에 plotOptions.bar.horizontal: true를 조합한 가로 방향 막대 차트 예시예요. 길이가 긴 카테고리 이름을 표시하거나 항목 순위를 보여줄 때 유용해요.

파이 차트 (Pie chart)

type: "pie" 옵션을 설정해 전체 비율을 원형 조각으로 보여주는 예시예요. 데이터의 구성 비율을 한눈에 보여줄 때 적합해요.

더 알아보기 (Learn more)

  • ApexCharts 설치 가이드 (NPM / CDN)
  • Flowbite Quickstart 가이드 (플러그인 스타일·기능 설치)
  • Flowbite Library의 대시보드·분석 레이아웃 관련 UI 컴포넌트