본문으로 건너뛰기Skip to content
예제 첫화면Examples home

내장 꺾은선 스파크The built-in line sparkline

cellRenderer 'sparkline'은 숫자 배열을 칸 안의 작은 꺾은선으로 그려요. curve로 곡선, area로 영역, marks로 표시 점, tone으로 색을 정해요. 직접 만든 렌더러 없이도 접근 이름과 어두운 모드가 따라와요.cellRenderer 'sparkline' draws an array of numbers as a small line in the cell. curve makes it smooth, area fills below it, marks picks the dots and tone sets the color. Without a custom renderer you still get an accessible name and dark mode.

입문Beginner
직접 써 보기Live preview내장 꺾은선 스파크The built-in line sparkline

코드Code

탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.

이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<div id="app">
  <div class="toolbar">
    <select aria-label="곡선" id="curve">
      <option value="linear" selected>꺾은선</option>
      <option value="smooth">곡선</option>
    </select>
    <select aria-label="영역" id="area">
      <option value="off" selected>없음</option>
      <option value="on">채움</option>
    </select>
    <select aria-label="표시 점" id="marks">
      <option value="last" selected>마지막</option>
      <option value="ends">처음·끝</option>
      <option value="extremes">최소·최대</option>
    </select>
    <button type="button" id="add">값 더하기</button>
  </div>
  <div id="grid" style="height: 300px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('curve').addEventListener('change', (event) => actions.curve(api, event.target.value));
document.getElementById('area').addEventListener('change', (event) => actions.area(api, event.target.value));
document.getElementById('marks').addEventListener('change', (event) => actions.marks(api, event.target.value));
document.getElementById('add').addEventListener('click', () => actions.add(api));
example.js
const initial = () => [
  { id: 1, name: '한빛전자', trend: [68, 69, 67, 70, 72, 71, 74] },
  { id: 2, name: '파란에너지', trend: [44, 43, 45, 41, 40, 39, 38] },
  { id: 3, name: '단풍제약', trend: [110, 112, 118, 117, 121, 123, 125] },
  { id: 4, name: '새로 상장', trend: [10] },
];
// sparkline은 셀 값(숫자 배열)을 그대로 그려요. curve: 'smooth', area: true, marks: 점 자리, tone: 색, min·max: 축 고정.
const state = {
  curve: 'linear',
  area: false,
  marks: ['last'],
};
const columns = () => [
  { id: 'name', title: '종목', width: 140 },
  {
    id: 'trend',
    title: '최근 7일',
    width: 220,
    cellRenderer: 'sparkline',
    cellRendererParams: { curve: state.curve, area: state.area, marks: state.marks, height: 22 },
  },
];
let rows = initial();
export const options = { columns: columns(), rows, rowKey: 'id' };
const apply = (api) => api.setColumns(columns());
export const actions = {
  curve: (api, value) => {
    state.curve = value === 'smooth' ? 'smooth' : 'linear';
    apply(api);
  },
  area: (api, value) => {
    state.area = value === 'on';
    apply(api);
  },
  marks: (api, value) => {
    state.marks =
      value === 'ends' ? ['first', 'last'] : value === 'extremes' ? ['min', 'max'] : ['last'];
    apply(api);
  },
  add: (api) => {
    rows = rows.map((row, i) => ({
      ...row,
      trend: [
        ...row.trend,
        Math.max(1, (row.trend[row.trend.length - 1] ?? 10) + ((i * 7 + row.trend.length * 3) % 9) - 4),
      ],
    }));
    api.setRows(rows);
  },
};
export function setup(_api, log) {
  log({
    ko: '"곡선", "영역", "표시 점"을 바꾸고 "값 더하기"로 점을 늘려 보세요. 마지막 줄은 점이 하나뿐이라 줄표예요.',
    en: 'Change "Curve", "Area" and "Dots", and use "Add value" to grow the series. The last row has one point so it shows a dash.',
  });
}

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<div id="app">
  <div class="toolbar">
    <select aria-label="Curve" id="curve">
      <option value="linear" selected>Linear</option>
      <option value="smooth">Smooth</option>
    </select>
    <select aria-label="Area" id="area">
      <option value="off" selected>Off</option>
      <option value="on">On</option>
    </select>
    <select aria-label="Dots" id="marks">
      <option value="last" selected>Last</option>
      <option value="ends">First and last</option>
      <option value="extremes">Min and max</option>
    </select>
    <button type="button" id="add">Add value</button>
  </div>
  <div id="grid" style="height: 300px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('curve').addEventListener('change', (event) => actions.curve(api, event.target.value));
document.getElementById('area').addEventListener('change', (event) => actions.area(api, event.target.value));
document.getElementById('marks').addEventListener('change', (event) => actions.marks(api, event.target.value));
document.getElementById('add').addEventListener('click', () => actions.add(api));
example.js
const initial = () => [
  { id: 1, name: 'Bright Electronics', trend: [68, 69, 67, 70, 72, 71, 74] },
  { id: 2, name: 'Blue Energy', trend: [44, 43, 45, 41, 40, 39, 38] },
  { id: 3, name: 'Maple Pharma', trend: [110, 112, 118, 117, 121, 123, 125] },
  { id: 4, name: 'New Listing', trend: [10] },
];
// sparkline draws the cell value (an array of numbers) as is. curve: 'smooth', area: true, marks: dot positions, tone: color, min/max: fixed axis.
const state = {
  curve: 'linear',
  area: false,
  marks: ['last'],
};
const columns = () => [
  { id: 'name', title: 'Stock', width: 140 },
  {
    id: 'trend',
    title: 'Last 7 days',
    width: 220,
    cellRenderer: 'sparkline',
    cellRendererParams: { curve: state.curve, area: state.area, marks: state.marks, height: 22 },
  },
];
let rows = initial();
export const options = { columns: columns(), rows, rowKey: 'id' };
const apply = (api) => api.setColumns(columns());
export const actions = {
  curve: (api, value) => {
    state.curve = value === 'smooth' ? 'smooth' : 'linear';
    apply(api);
  },
  area: (api, value) => {
    state.area = value === 'on';
    apply(api);
  },
  marks: (api, value) => {
    state.marks =
      value === 'ends' ? ['first', 'last'] : value === 'extremes' ? ['min', 'max'] : ['last'];
    apply(api);
  },
  add: (api) => {
    rows = rows.map((row, i) => ({
      ...row,
      trend: [
        ...row.trend,
        Math.max(1, (row.trend[row.trend.length - 1] ?? 10) + ((i * 7 + row.trend.length * 3) % 9) - 4),
      ],
    }));
    api.setRows(rows);
  },
};
export function setup(_api, log) {
  log('Change "Curve", "Area" and "Dots", and use "Add value" to grow the series. The last row has one point so it shows a dash.');
}