막대 스파크: 음수와 강조Bar sparkline: negatives and highlight
cellRenderer 'sparkbars'는 0을 기준선으로 위·아래 막대를 그려요. 음수는 negativeTone 색이고 highlight를 주면 마지막·최소·최대 막대만 진하게 보여요. min·max로 축을 맞추면 줄끼리 높이를 비교할 수 있어요.cellRenderer 'sparkbars' draws bars up and down from a zero baseline. Negatives use negativeTone and highlight makes only the last, minimum or maximum bar strong. Fixing the axis with min and max lets you compare heights across rows.
코드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)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<div id="app">
<div class="toolbar">
<select aria-label="강조" id="highlight">
<option value="none">없음</option>
<option value="last" selected>마지막</option>
<option value="min">최소</option>
<option value="max">최대</option>
</select>
<select aria-label="축 맞춤" id="axis">
<option value="auto" selected>자동</option>
<option value="shared">같은 눈금</option>
</select>
<select aria-label="막대 간격" id="gap">
<option value="0">0</option>
<option value="1" selected>1</option>
<option value="3">3</option>
</select>
</div>
<div id="grid" style="height: 200px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('highlight').addEventListener('change', (event) => actions.highlight(api, event.target.value));
document.getElementById('axis').addEventListener('change', (event) => actions.axis(api, event.target.value));
document.getElementById('gap').addEventListener('change', (event) => actions.gap(api, event.target.value));const rows = [
{ id: 1, name: '한빛전자', pnl: [12, -8, 20, 35, -15, 28] },
{ id: 2, name: '파란에너지', pnl: [-30, -12, 5, 18, 22, 40] },
{ id: 3, name: '단풍제약', pnl: [5, 4, -3, 2, -1, 6] },
];
// sparkbars: 0이 기준선이고 음수는 negativeTone 색이에요. highlight는 한 막대만 진하게 하고, min·max로 축을 맞춰요.
const state = { highlight: 'last', shared: false, gap: 1 };
const columns = () => [
{ id: 'name', title: '종목', width: 140 },
{
id: 'pnl',
title: '월별 손익',
width: 200,
cellRenderer: 'sparkbars',
cellRendererParams: {
highlight: state.highlight,
gap: state.gap,
height: 24,
tone: 'success',
negativeTone: 'danger',
...(state.shared ? { min: -50, max: 50 } : {}),
},
},
];
export const options = { columns: columns(), rows, rowKey: 'id' };
export const actions = {
highlight: (api, value) => {
state.highlight = value === 'last' || value === 'min' || value === 'max' ? value : null;
api.setColumns(columns());
},
axis: (api, value) => {
state.shared = value === 'shared';
api.setColumns(columns());
},
gap: (api, value) => {
state.gap = Number(value) || 0;
api.setColumns(columns());
},
};
export function setup(_api, log) {
log({
ko: '"강조", "축 맞춤", "막대 간격"을 바꿔 보세요. 음수 막대는 아래로 내려가요.',
en: 'Change "Highlight", "Shared axis" and "Gap". Negative bars go down.',
});
}<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>막대 스파크: 음수와 강조</title>
</head>
<body>
<div class="toolbar">
<select aria-label="강조" id="highlight">
<option value="none">없음</option>
<option value="last" selected>마지막</option>
<option value="min">최소</option>
<option value="max">최대</option>
</select>
<select aria-label="축 맞춤" id="axis">
<option value="auto" selected>자동</option>
<option value="shared">같은 눈금</option>
</select>
<select aria-label="막대 간격" id="gap">
<option value="0">0</option>
<option value="1" selected>1</option>
<option value="3">3</option>
</select>
</div>
<div id="grid" style="height: 200px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = [
{ id: 1, name: '한빛전자', pnl: [12, -8, 20, 35, -15, 28] },
{ id: 2, name: '파란에너지', pnl: [-30, -12, 5, 18, 22, 40] },
{ id: 3, name: '단풍제약', pnl: [5, 4, -3, 2, -1, 6] },
];
// sparkbars: 0이 기준선이고 음수는 negativeTone 색이에요. highlight는 한 막대만 진하게 하고, min·max로 축을 맞춰요.
const state = { highlight: 'last', shared: false, gap: 1 };
const columns = () => [
{ id: 'name', title: '종목', width: 140 },
{
id: 'pnl',
title: '월별 손익',
width: 200,
cellRenderer: 'sparkbars',
cellRendererParams: {
highlight: state.highlight,
gap: state.gap,
height: 24,
tone: 'success',
negativeTone: 'danger',
...(state.shared ? { min: -50, max: 50 } : {}),
},
},
];
const options = { columns: columns(), rows, rowKey: 'id' };
const actions = {
highlight: (api, value) => {
state.highlight = value === 'last' || value === 'min' || value === 'max' ? value : null;
api.setColumns(columns());
},
axis: (api, value) => {
state.shared = value === 'shared';
api.setColumns(columns());
},
gap: (api, value) => {
state.gap = Number(value) || 0;
api.setColumns(columns());
},
};
function setup(_api, log) {
log({
ko: '"강조", "축 맞춤", "막대 간격"을 바꿔 보세요. 음수 막대는 아래로 내려가요.',
en: 'Change "Highlight", "Shared axis" and "Gap". Negative bars go down.',
});
}
options.ariaLabel = "막대 스파크: 음수와 강조";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('highlight').addEventListener('change', (event) => actions.highlight(api, event.target.value));
document.getElementById('axis').addEventListener('change', (event) => actions.axis(api, event.target.value));
document.getElementById('gap').addEventListener('change', (event) => actions.gap(api, event.target.value));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="강조"
disabled={!api}
onChange={(event) => api && actions.highlight(api, event.target.value)}
>
<option value="none">없음</option>
<option value="last">마지막</option>
<option value="min">최소</option>
<option value="max">최대</option>
</select>
<select defaultValue="last"
aria-label="축 맞춤"
disabled={!api}
onChange={(event) => api && actions.axis(api, event.target.value)}
>
<option value="auto">자동</option>
<option value="shared">같은 눈금</option>
</select>
<select defaultValue="auto"
aria-label="막대 간격"
disabled={!api}
onChange={(event) => api && actions.gap(api, event.target.value)}
>
<option value="0">0</option>
<option value="1">1</option>
<option value="3">3</option>
</select>
</div>
<Grid style={{ height: 200 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
pnl: number[];
}
const rows: Row[] = [
{ id: 1, name: '한빛전자', pnl: [12, -8, 20, 35, -15, 28] },
{ id: 2, name: '파란에너지', pnl: [-30, -12, 5, 18, 22, 40] },
{ id: 3, name: '단풍제약', pnl: [5, 4, -3, 2, -1, 6] },
];
// sparkbars: 0이 기준선이고 음수는 negativeTone 색이에요. highlight는 한 막대만 진하게 하고, min·max로 축을 맞춰요.
const state = { highlight: 'last' as 'last' | 'min' | 'max' | null, shared: false, gap: 1 };
const columns = (): ColumnDef<Row>[] => [
{ id: 'name', title: '종목', width: 140 },
{
id: 'pnl',
title: '월별 손익',
width: 200,
cellRenderer: 'sparkbars',
cellRendererParams: {
highlight: state.highlight,
gap: state.gap,
height: 24,
tone: 'success',
negativeTone: 'danger',
...(state.shared ? { min: -50, max: 50 } : {}),
},
},
];
export const options: GridOptions<Row> = { columns: columns(), rows, rowKey: 'id' };
export const actions = {
highlight: (api: GridApi<Row>, value?: string) => {
state.highlight = value === 'last' || value === 'min' || value === 'max' ? value : null;
api.setColumns(columns());
},
axis: (api: GridApi<Row>, value?: string) => {
state.shared = value === 'shared';
api.setColumns(columns());
},
gap: (api: GridApi<Row>, value?: string) => {
state.gap = Number(value) || 0;
api.setColumns(columns());
},
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '"강조", "축 맞춤", "막대 간격"을 바꿔 보세요. 음수 막대는 아래로 내려가요.',
en: 'Change "Highlight", "Shared axis" and "Gap". Negative bars go down.',
});
}버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="강조"
:disabled="!api"
@change="api && actions.highlight(api, ($event.target as HTMLSelectElement).value)"
>
<option value="none">없음</option>
<option value="last" selected>마지막</option>
<option value="min">최소</option>
<option value="max">최대</option>
</select>
<select
aria-label="축 맞춤"
:disabled="!api"
@change="api && actions.axis(api, ($event.target as HTMLSelectElement).value)"
>
<option value="auto" selected>자동</option>
<option value="shared">같은 눈금</option>
</select>
<select
aria-label="막대 간격"
:disabled="!api"
@change="api && actions.gap(api, ($event.target as HTMLSelectElement).value)"
>
<option value="0">0</option>
<option value="1" selected>1</option>
<option value="3">3</option>
</select>
</div>
<Grid style="height: 200px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
pnl: number[];
}
const rows: Row[] = [
{ id: 1, name: '한빛전자', pnl: [12, -8, 20, 35, -15, 28] },
{ id: 2, name: '파란에너지', pnl: [-30, -12, 5, 18, 22, 40] },
{ id: 3, name: '단풍제약', pnl: [5, 4, -3, 2, -1, 6] },
];
// sparkbars: 0이 기준선이고 음수는 negativeTone 색이에요. highlight는 한 막대만 진하게 하고, min·max로 축을 맞춰요.
const state = { highlight: 'last' as 'last' | 'min' | 'max' | null, shared: false, gap: 1 };
const columns = (): ColumnDef<Row>[] => [
{ id: 'name', title: '종목', width: 140 },
{
id: 'pnl',
title: '월별 손익',
width: 200,
cellRenderer: 'sparkbars',
cellRendererParams: {
highlight: state.highlight,
gap: state.gap,
height: 24,
tone: 'success',
negativeTone: 'danger',
...(state.shared ? { min: -50, max: 50 } : {}),
},
},
];
export const options: GridOptions<Row> = { columns: columns(), rows, rowKey: 'id' };
export const actions = {
highlight: (api: GridApi<Row>, value?: string) => {
state.highlight = value === 'last' || value === 'min' || value === 'max' ? value : null;
api.setColumns(columns());
},
axis: (api: GridApi<Row>, value?: string) => {
state.shared = value === 'shared';
api.setColumns(columns());
},
gap: (api: GridApi<Row>, value?: string) => {
state.gap = Number(value) || 0;
api.setColumns(columns());
},
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '"강조", "축 맞춤", "막대 간격"을 바꿔 보세요. 음수 막대는 아래로 내려가요.',
en: 'Change "Highlight", "Shared axis" and "Gap". Negative bars go down.',
});
}버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="강조"
[disabled]="!api"
(change)="api && actions.highlight(api, $any($event.target).value)"
>
<option value="none">없음</option>
<option value="last" selected>마지막</option>
<option value="min">최소</option>
<option value="max">최대</option>
</select>
<select
aria-label="축 맞춤"
[disabled]="!api"
(change)="api && actions.axis(api, $any($event.target).value)"
>
<option value="auto" selected>자동</option>
<option value="shared">같은 눈금</option>
</select>
<select
aria-label="막대 간격"
[disabled]="!api"
(change)="api && actions.gap(api, $any($event.target).value)"
>
<option value="0">0</option>
<option value="1" selected>1</option>
<option value="3">3</option>
</select>
</div>
<mx-grid style="height: 200px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
pnl: number[];
}
const rows: Row[] = [
{ id: 1, name: '한빛전자', pnl: [12, -8, 20, 35, -15, 28] },
{ id: 2, name: '파란에너지', pnl: [-30, -12, 5, 18, 22, 40] },
{ id: 3, name: '단풍제약', pnl: [5, 4, -3, 2, -1, 6] },
];
// sparkbars: 0이 기준선이고 음수는 negativeTone 색이에요. highlight는 한 막대만 진하게 하고, min·max로 축을 맞춰요.
const state = { highlight: 'last' as 'last' | 'min' | 'max' | null, shared: false, gap: 1 };
const columns = (): ColumnDef<Row>[] => [
{ id: 'name', title: '종목', width: 140 },
{
id: 'pnl',
title: '월별 손익',
width: 200,
cellRenderer: 'sparkbars',
cellRendererParams: {
highlight: state.highlight,
gap: state.gap,
height: 24,
tone: 'success',
negativeTone: 'danger',
...(state.shared ? { min: -50, max: 50 } : {}),
},
},
];
export const options: GridOptions<Row> = { columns: columns(), rows, rowKey: 'id' };
export const actions = {
highlight: (api: GridApi<Row>, value?: string) => {
state.highlight = value === 'last' || value === 'min' || value === 'max' ? value : null;
api.setColumns(columns());
},
axis: (api: GridApi<Row>, value?: string) => {
state.shared = value === 'shared';
api.setColumns(columns());
},
gap: (api: GridApi<Row>, value?: string) => {
state.gap = Number(value) || 0;
api.setColumns(columns());
},
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log({
ko: '"강조", "축 맞춤", "막대 간격"을 바꿔 보세요. 음수 막대는 아래로 내려가요.',
en: 'Change "Highlight", "Shared axis" and "Gap". Negative bars go down.',
});
}When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<select aria-label="Highlight" id="highlight">
<option value="none">None</option>
<option value="last" selected>Last</option>
<option value="min">Min</option>
<option value="max">Max</option>
</select>
<select aria-label="Shared axis" id="axis">
<option value="auto" selected>Auto</option>
<option value="shared">Shared</option>
</select>
<select aria-label="Gap" id="gap">
<option value="0">0</option>
<option value="1" selected>1</option>
<option value="3">3</option>
</select>
</div>
<div id="grid" style="height: 200px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('highlight').addEventListener('change', (event) => actions.highlight(api, event.target.value));
document.getElementById('axis').addEventListener('change', (event) => actions.axis(api, event.target.value));
document.getElementById('gap').addEventListener('change', (event) => actions.gap(api, event.target.value));const rows = [
{ id: 1, name: 'Bright Electronics', pnl: [12, -8, 20, 35, -15, 28] },
{ id: 2, name: 'Blue Energy', pnl: [-30, -12, 5, 18, 22, 40] },
{ id: 3, name: 'Maple Pharma', pnl: [5, 4, -3, 2, -1, 6] },
];
// sparkbars: 0 is the baseline and negatives use the negativeTone color. highlight makes one bar strong, and min/max fix the axis.
const state = { highlight: 'last', shared: false, gap: 1 };
const columns = () => [
{ id: 'name', title: 'Stock', width: 140 },
{
id: 'pnl',
title: 'Monthly P&L',
width: 200,
cellRenderer: 'sparkbars',
cellRendererParams: {
highlight: state.highlight,
gap: state.gap,
height: 24,
tone: 'success',
negativeTone: 'danger',
...(state.shared ? { min: -50, max: 50 } : {}),
},
},
];
export const options = { columns: columns(), rows, rowKey: 'id' };
export const actions = {
highlight: (api, value) => {
state.highlight = value === 'last' || value === 'min' || value === 'max' ? value : null;
api.setColumns(columns());
},
axis: (api, value) => {
state.shared = value === 'shared';
api.setColumns(columns());
},
gap: (api, value) => {
state.gap = Number(value) || 0;
api.setColumns(columns());
},
};
export function setup(_api, log) {
log('Change "Highlight", "Shared axis" and "Gap". Negative bars go down.');
}<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bar sparkline: negatives and highlight</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Highlight" id="highlight">
<option value="none">None</option>
<option value="last" selected>Last</option>
<option value="min">Min</option>
<option value="max">Max</option>
</select>
<select aria-label="Shared axis" id="axis">
<option value="auto" selected>Auto</option>
<option value="shared">Shared</option>
</select>
<select aria-label="Gap" id="gap">
<option value="0">0</option>
<option value="1" selected>1</option>
<option value="3">3</option>
</select>
</div>
<div id="grid" style="height: 200px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = [
{ id: 1, name: 'Bright Electronics', pnl: [12, -8, 20, 35, -15, 28] },
{ id: 2, name: 'Blue Energy', pnl: [-30, -12, 5, 18, 22, 40] },
{ id: 3, name: 'Maple Pharma', pnl: [5, 4, -3, 2, -1, 6] },
];
// sparkbars: 0 is the baseline and negatives use the negativeTone color. highlight makes one bar strong, and min/max fix the axis.
const state = { highlight: 'last', shared: false, gap: 1 };
const columns = () => [
{ id: 'name', title: 'Stock', width: 140 },
{
id: 'pnl',
title: 'Monthly P&L',
width: 200,
cellRenderer: 'sparkbars',
cellRendererParams: {
highlight: state.highlight,
gap: state.gap,
height: 24,
tone: 'success',
negativeTone: 'danger',
...(state.shared ? { min: -50, max: 50 } : {}),
},
},
];
const options = { columns: columns(), rows, rowKey: 'id' };
const actions = {
highlight: (api, value) => {
state.highlight = value === 'last' || value === 'min' || value === 'max' ? value : null;
api.setColumns(columns());
},
axis: (api, value) => {
state.shared = value === 'shared';
api.setColumns(columns());
},
gap: (api, value) => {
state.gap = Number(value) || 0;
api.setColumns(columns());
},
};
function setup(_api, log) {
log('Change "Highlight", "Shared axis" and "Gap". Negative bars go down.');
}
options.ariaLabel = "Bar sparkline: negatives and highlight";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
document.getElementById('highlight').addEventListener('change', (event) => actions.highlight(api, event.target.value));
document.getElementById('axis').addEventListener('change', (event) => actions.axis(api, event.target.value));
document.getElementById('gap').addEventListener('change', (event) => actions.gap(api, event.target.value));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { useEffect } from 'react';
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, setup, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
useEffect(() => (api ? setup(api, console.log) : undefined), [api]);
return (
<>
<div className="toolbar">
<select
aria-label="Highlight"
disabled={!api}
onChange={(event) => api && actions.highlight(api, event.target.value)}
>
<option value="none">None</option>
<option value="last">Last</option>
<option value="min">Min</option>
<option value="max">Max</option>
</select>
<select defaultValue="last"
aria-label="Shared axis"
disabled={!api}
onChange={(event) => api && actions.axis(api, event.target.value)}
>
<option value="auto">Auto</option>
<option value="shared">Shared</option>
</select>
<select defaultValue="auto"
aria-label="Gap"
disabled={!api}
onChange={(event) => api && actions.gap(api, event.target.value)}
>
<option value="0">0</option>
<option value="1">1</option>
<option value="3">3</option>
</select>
</div>
<Grid style={{ height: 200 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
pnl: number[];
}
const rows: Row[] = [
{ id: 1, name: 'Bright Electronics', pnl: [12, -8, 20, 35, -15, 28] },
{ id: 2, name: 'Blue Energy', pnl: [-30, -12, 5, 18, 22, 40] },
{ id: 3, name: 'Maple Pharma', pnl: [5, 4, -3, 2, -1, 6] },
];
// sparkbars: 0 is the baseline and negatives use the negativeTone color. highlight makes one bar strong, and min/max fix the axis.
const state = { highlight: 'last' as 'last' | 'min' | 'max' | null, shared: false, gap: 1 };
const columns = (): ColumnDef<Row>[] => [
{ id: 'name', title: 'Stock', width: 140 },
{
id: 'pnl',
title: 'Monthly P&L',
width: 200,
cellRenderer: 'sparkbars',
cellRendererParams: {
highlight: state.highlight,
gap: state.gap,
height: 24,
tone: 'success',
negativeTone: 'danger',
...(state.shared ? { min: -50, max: 50 } : {}),
},
},
];
export const options: GridOptions<Row> = { columns: columns(), rows, rowKey: 'id' };
export const actions = {
highlight: (api: GridApi<Row>, value?: string) => {
state.highlight = value === 'last' || value === 'min' || value === 'max' ? value : null;
api.setColumns(columns());
},
axis: (api: GridApi<Row>, value?: string) => {
state.shared = value === 'shared';
api.setColumns(columns());
},
gap: (api: GridApi<Row>, value?: string) => {
state.gap = Number(value) || 0;
api.setColumns(columns());
},
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log('Change "Highlight", "Shared axis" and "Gap". Negative bars go down.');
}When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { onBeforeUnmount, shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, setup, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
let stop: (() => void) | undefined;
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
stop = setup(event.api, console.log);
}
onBeforeUnmount(() => stop?.());
</script>
<template>
<div class="toolbar">
<select
aria-label="Highlight"
:disabled="!api"
@change="api && actions.highlight(api, ($event.target as HTMLSelectElement).value)"
>
<option value="none">None</option>
<option value="last" selected>Last</option>
<option value="min">Min</option>
<option value="max">Max</option>
</select>
<select
aria-label="Shared axis"
:disabled="!api"
@change="api && actions.axis(api, ($event.target as HTMLSelectElement).value)"
>
<option value="auto" selected>Auto</option>
<option value="shared">Shared</option>
</select>
<select
aria-label="Gap"
:disabled="!api"
@change="api && actions.gap(api, ($event.target as HTMLSelectElement).value)"
>
<option value="0">0</option>
<option value="1" selected>1</option>
<option value="3">3</option>
</select>
</div>
<Grid style="height: 200px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
pnl: number[];
}
const rows: Row[] = [
{ id: 1, name: 'Bright Electronics', pnl: [12, -8, 20, 35, -15, 28] },
{ id: 2, name: 'Blue Energy', pnl: [-30, -12, 5, 18, 22, 40] },
{ id: 3, name: 'Maple Pharma', pnl: [5, 4, -3, 2, -1, 6] },
];
// sparkbars: 0 is the baseline and negatives use the negativeTone color. highlight makes one bar strong, and min/max fix the axis.
const state = { highlight: 'last' as 'last' | 'min' | 'max' | null, shared: false, gap: 1 };
const columns = (): ColumnDef<Row>[] => [
{ id: 'name', title: 'Stock', width: 140 },
{
id: 'pnl',
title: 'Monthly P&L',
width: 200,
cellRenderer: 'sparkbars',
cellRendererParams: {
highlight: state.highlight,
gap: state.gap,
height: 24,
tone: 'success',
negativeTone: 'danger',
...(state.shared ? { min: -50, max: 50 } : {}),
},
},
];
export const options: GridOptions<Row> = { columns: columns(), rows, rowKey: 'id' };
export const actions = {
highlight: (api: GridApi<Row>, value?: string) => {
state.highlight = value === 'last' || value === 'min' || value === 'max' ? value : null;
api.setColumns(columns());
},
axis: (api: GridApi<Row>, value?: string) => {
state.shared = value === 'shared';
api.setColumns(columns());
},
gap: (api: GridApi<Row>, value?: string) => {
state.gap = Number(value) || 0;
api.setColumns(columns());
},
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log('Change "Highlight", "Shared axis" and "Gap". Negative bars go down.');
}When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component, OnDestroy } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, setup, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="Highlight"
[disabled]="!api"
(change)="api && actions.highlight(api, $any($event.target).value)"
>
<option value="none">None</option>
<option value="last" selected>Last</option>
<option value="min">Min</option>
<option value="max">Max</option>
</select>
<select
aria-label="Shared axis"
[disabled]="!api"
(change)="api && actions.axis(api, $any($event.target).value)"
>
<option value="auto" selected>Auto</option>
<option value="shared">Shared</option>
</select>
<select
aria-label="Gap"
[disabled]="!api"
(change)="api && actions.gap(api, $any($event.target).value)"
>
<option value="0">0</option>
<option value="1" selected>1</option>
<option value="3">3</option>
</select>
</div>
<mx-grid style="height: 200px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent implements OnDestroy {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
private stop: (() => void) | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
this.stop = setup(this.api, console.log);
}
}
ngOnDestroy(): void {
this.stop?.();
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
name: string;
pnl: number[];
}
const rows: Row[] = [
{ id: 1, name: 'Bright Electronics', pnl: [12, -8, 20, 35, -15, 28] },
{ id: 2, name: 'Blue Energy', pnl: [-30, -12, 5, 18, 22, 40] },
{ id: 3, name: 'Maple Pharma', pnl: [5, 4, -3, 2, -1, 6] },
];
// sparkbars: 0 is the baseline and negatives use the negativeTone color. highlight makes one bar strong, and min/max fix the axis.
const state = { highlight: 'last' as 'last' | 'min' | 'max' | null, shared: false, gap: 1 };
const columns = (): ColumnDef<Row>[] => [
{ id: 'name', title: 'Stock', width: 140 },
{
id: 'pnl',
title: 'Monthly P&L',
width: 200,
cellRenderer: 'sparkbars',
cellRendererParams: {
highlight: state.highlight,
gap: state.gap,
height: 24,
tone: 'success',
negativeTone: 'danger',
...(state.shared ? { min: -50, max: 50 } : {}),
},
},
];
export const options: GridOptions<Row> = { columns: columns(), rows, rowKey: 'id' };
export const actions = {
highlight: (api: GridApi<Row>, value?: string) => {
state.highlight = value === 'last' || value === 'min' || value === 'max' ? value : null;
api.setColumns(columns());
},
axis: (api: GridApi<Row>, value?: string) => {
state.shared = value === 'shared';
api.setColumns(columns());
},
gap: (api: GridApi<Row>, value?: string) => {
state.gap = Number(value) || 0;
api.setColumns(columns());
},
};
export function setup(_api: GridApi<Row>, log: (message: LogText) => void) {
log('Change "Highlight", "Shared axis" and "Gap". Negative bars go down.');
}