πŸš€ μ‹œμž‘ν•˜κΈ°

August 17, 2021 Β· View on GitHub

μ„€μΉ˜ν•˜κΈ°

TOAST UI μ œν’ˆλ“€μ€ νŒ¨ν‚€μ§€ λ§€λ‹ˆμ €λ₯Ό μ΄μš©ν•˜κ±°λ‚˜, 직접 μ†ŒμŠ€ μ½”λ“œλ₯Ό λ‹€μš΄λ°›μ•„ μ‚¬μš©ν•  수 μžˆλ‹€. ν•˜μ§€λ§Œ νŒ¨ν‚€μ§€ λ§€λ‹ˆμ € μ‚¬μš©μ„ ꢌμž₯ν•œλ‹€.

νŒ¨ν‚€μ§€ λ§€λ‹ˆμ € μ‚¬μš©ν•˜κΈ°

TOAST UI μ œν’ˆλ“€μ€ npm νŒ¨ν‚€μ§€ λ§€λ‹ˆμ €μ— λ“±λ‘λ˜μ–΄ μžˆλ‹€. 각 νŒ¨ν‚€μ§€ λ§€λ‹ˆμ €κ°€ μ œκ³΅ν•˜λŠ” CLI 도ꡬλ₯Ό μ‚¬μš©ν•˜λ©΄ μ‰½κ²Œ νŒ¨ν‚€μ§€λ₯Ό μ„€μΉ˜ν•  수 μžˆλ‹€. npm μ‚¬μš©μ„ μœ„ν•΄μ„  Node.jsλ₯Ό 미리 μ„€μΉ˜ν•΄μ•Ό ν•œλ‹€.

npm

$ npm install --save @toast-ui/chart # μ΅œμ‹  버전
$ npm install --save @toast-ui/chart@<version> # νŠΉμ • 버전

Contents Delivery Network (CDN) μ‚¬μš©ν•˜κΈ°

TOAST UI ChartλŠ” CDN을 톡해 μ‚¬μš©ν•  수 μžˆλ‹€.

  • μ•„λž˜μ˜ μ½”λ“œλ‘œ CDN을 μ‚¬μš©ν•  수 μžˆλ‹€.
<link rel="stylesheet" href="https://uicdn.toast.com/chart/latest/toastui-chart.min.css" />
<script src="https://uicdn.toast.com/chart/latest/toastui-chart.min.js"></script>
  • CDN은 μ•„λž˜μ˜ 디렉토리 ꡬ쑰둜 κ΅¬μ„±λ˜μ–΄ μžˆλ‹€.
- uicdn.toast.com/
  β”œβ”€ chart/
  β”‚  β”œβ”€ latest
  β”‚  β”‚  β”œβ”€ toastui-chart.js
  β”‚  β”‚  β”œβ”€ toastui-chart.min.js
  β”‚  β”‚  β”œβ”€ toastui-chart.css
  β”‚  β”‚  β”œβ”€ toastui-chart.min.css
  β”‚  β”œβ”€ v4.0.0/

μ†ŒμŠ€ 파일 λ‹€μš΄λ‘œλ“œ

μ‚¬μš©ν•˜κΈ°

HTML

TOAST UI Chartκ°€ 생성될 μ»¨ν…Œμ΄λ„ˆ μš”μ†Œλ₯Ό μΆ”κ°€ν•œλ‹€.

<div id="chart"></div>

μžλ°”μŠ€ν¬λ¦½νŠΈ

뢈러였기

TOAST UI μ°¨νŠΈλŠ” μƒμ„±μž ν•¨μˆ˜λ₯Ό 톡해 μΈμŠ€ν„΄μŠ€λ₯Ό 생성할 수 μžˆλ‹€. μƒμ„±μž ν•¨μˆ˜μ— μ ‘κ·Όν•˜κΈ° μœ„ν•΄μ„œλŠ” ν™˜κ²½μ— 따라 μ ‘κ·Όν•  수 μžˆλŠ” μ„Έ κ°€μ§€ 방법이 μ‘΄μž¬ν•œλ‹€.

/* namespace */
const Chart = toastui.Chart;
/* Node.js의 CommonJS */
const Chart = require('@toast-ui/chart');
/* Node.js의 ES6 */
import Chart from '@toast-ui/chart';
import { BarChart } from '@toast-ui/chart';

Webpack 4 μ—μ„œλŠ” νŒ¨ν‚€μ§€ λͺ¨λ“ˆμ„ κ°€μ Έμ˜¬ λ•Œ main ν•„λ“œλ³΄λ‹€ module ν•„λ“œμ— μ •μ˜λœ μ§„μž…μ μ„ μš°μ„ μ μœΌλ‘œ κ°€μ Έμ˜¨λ‹€. λ§Œμ•½ ν”„λ‘œμ νŠΈμ—μ„œ Webpack 4λ₯Ό μ‚¬μš©ν•˜κ³  require ꡬ문을 μ‚¬μš©ν•΄ @toast-ui/chartλ₯Ό λΆˆλŸ¬μ˜¨λ‹€λ©΄, module ν•„λ“œμ— μ •μ˜λœ ESM 파일이 λ‘œλ“œλ  것이며, require ꡬ문으둜 κ°€μ Έμ˜¬ 수 μžˆλ„λ‘ 트랜슀 파일 될 것이닀. ν”„λ‘œμ νŠΈμ—μ„œ UMD 용 λ²ˆλ“€ 파일이 ν•„μš”ν•˜λ‹€λ©΄ @toast-ui/chart/dist/toastui-chart.js ν˜Ήμ€ @toast-ui/chart/dist/toastui-chart.min.js νŒŒμΌμ„ 직접 λ‘œλ“œν•˜μ—¬ μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.

const Chart = require('@toast-ui/chart/dist/toastui-chart.min.js'); // // UMD 용 λ²ˆλ“€ 파일 λ‘œλ“œ

Webpack 5λŠ” exports ν•„λ“œλ₯Ό μ§€μ›ν•œλ‹€. νŒ¨ν‚€μ§€μ—μ„œ μ •μ˜λœ exports ν•„λ“œλ₯Ό 보고 λͺ¨λ“ˆ μ§„μž…μ μ„ νŒλ‹¨ν•  수 μžˆλ‹€. λ˜ν•œ μ•„λž˜μ™€ 같이 μ„œλΈŒ 경둜둜 μ ‘κ·Όν•˜μ—¬ ν•„μš”ν•œ 차트 νŒŒμΌμ„ λ‘œλ“œν•  수 μžˆλ‹€.

const Chart = require('@toast-ui/chart'); // ./dist/toastui-chart.js

import { BarChart } from '@toast-ui/chart'; // ./dist/esm/index.js

import BarChart from '@toast-ui/chart/bar';
import ColumnChart from '@toast-ui/chart/column';
import LineChart from '@toast-ui/chart/line';
import AreaChart from '@toast-ui/chart/area';
import LineAreaChart from '@toast-ui/chart/lineArea';
import ColumnLineChart from '@toast-ui/chart/columnLine';
import BulletChart from '@toast-ui/chart/bullet';
import BoxPlotChart from '@toast-ui/chart/boxPlot';
import TreemapChart from '@toast-ui/chart/treemap';
import HeatmapChart from '@toast-ui/chart/heatmap';
import ScatterChart from '@toast-ui/chart/scatter';
import LineScatterChart from '@toast-ui/chart/lineScatter';
import BubbleChart from '@toast-ui/chart/bubble';
import PieChart from '@toast-ui/chart/pie';
import NestedPieChart from '@toast-ui/chart/nestedPie';
import RadarChart from '@toast-ui/chart/radar';

CSS

Chartλ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” CSS νŒŒμΌμ„ μΆ”κ°€ν•΄μ€˜μ•Ό ν•œλ‹€. import, requireλ₯Ό 톡해 CSS νŒŒμΌμ„ λΆˆλŸ¬μ˜€κ±°λ‚˜, CDN을 톡해 뢈러올 수 μžˆλ‹€.

  • ES6 λͺ¨λ“ˆ
import '@toast-ui/chart/dist/toastui-chart.min.css'; // Chart μŠ€νƒ€μΌ
  • Common JS
require('@toast-ui/chart/dist/toastui-chart.min.css');
  • CDN
<link rel="stylesheet" href="https://uicdn.toast.com/chart/latest/toastui-chart.min.css" />

μΈμŠ€ν„΄μŠ€ λ§Œλ“€κΈ°

μƒμ„±μž ν•¨μˆ˜λŠ” el, data, options μ„Έ 개λ₯Ό 인자둜 κ°–λŠ”λ‹€.

  • el: 차트λ₯Ό μžμ‹μš”μ†Œλ‘œ κ°–λŠ” HTML μš”μ†Œ
  • data: 차트의 기반이 λ˜λŠ” 숫자 데이터
  • options: λ²”λ‘€, μ •λ ¬, 툴팁 포맷 λ“± μ—¬λŸ¬ κΈ°λŠ₯을 λ‚˜νƒ€λ‚΄λŠ” μ˜΅μ…˜
const el = document.getElementById('chart');
const data = {
  categories: ['Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  series: [
    {
      name: 'Budget',
      data: [5000, 3000, 5000, 7000, 6000, 4000, 1000],
    },
    {
      name: 'Income',
      data: [8000, 4000, 7000, 2000, 6000, 3000, 5000],
    },
  ],
};
const options = {
  chart: { width: 700, height: 400 },
};

const chart = Chart.barChart({ el, data, options });
// const chart = new BarChart({ el, data, options }); // 두 번째 방법

image