Heatmap
Категории по обеим осям, значение colorField задаёт цвет ячейки по непрерывной шкале. Справа автоматически появляется градиентная легенда.
По умолчанию оси heatmap рисуются без линий, тиков и сетки — остаются только подписи категорий (вернуть можно через axes: [{ ..., line: { enabled: true } }]).
import { getData } from './data';
import type { ChartOptions } from 'grafit-charts';
export function createOptions(): ChartOptions {
return {
data: getData(),
title: { text: 'Load by hour' },
series: [
{
type: 'heatmap',
xField: 'day',
yField: 'hour',
colorField: 'load',
colorName: 'Load',
colorRange: ['#e8f0fe', '#1d4fd7'],
},
],
legend: { enabled: false },
};
}export function getData() {
const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
const hours = ['00', '04', '08', '12', '16', '20'];
const data: Array<{ day: string; hour: string; load: number }> = [];
days.forEach((day, d) => {
hours.forEach((hour, h) => {
const base = h === 2 || h === 3 ? 60 : h === 4 ? 45 : 15;
const weekend = d >= 5 ? 0.5 : 1;
data.push({ day, hour, load: Math.round(base * weekend + ((d * 7 + h * 3) % 13)) });
});
});
return data;
}Расположение цветовой шкалы
gradientLegend управляет позицией, отступом и толщиной шкалы (в сборке через grafit-charts/core шкала — отдельный модуль: register(gradientLegendModule)):
import { getData } from './data';
import type { ChartOptions } from 'grafit-charts';
export function createOptions(): ChartOptions {
return {
data: getData(),
title: { text: 'Scale at the bottom with spacing' },
series: [{ type: 'heatmap', xField: 'week', yField: 'day', colorField: 'deploys', colorName: 'Deploys' }],
gradientLegend: { position: 'bottom', spacing: 14, thickness: 10 },
legend: { enabled: false },
};
}export function getData() {
const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
const weeks = ['W1', 'W2', 'W3', 'W4'];
const data: Array<{ week: string; day: string; deploys: number }> = [];
weeks.forEach((week, w) => {
days.forEach((day, d) => {
data.push({ week, day, deploys: ((w * 5 + d * 3) % 11) + (d === 2 ? 6 : 1) });
});
});
return data;
}| Опция | Тип | По умолчанию | Описание |
|---|---|---|---|
gradientLegend.enabled | boolean | true | показывать шкалу |
gradientLegend.position | 'right' | 'bottom' | 'right' | сторона размещения |
gradientLegend.spacing | Pixels | 10 | отступ от области построения |
gradientLegend.thickness | Pixels | 12 | толщина полосы |
Подписи значений
label: { enabled: true } выводит значение в каждой ячейке; цвет текста подбирается автоматически по светимости фона. Тултип здесь привязан к центру ячейки (tooltip.position.anchorTo: 'center', по умолчанию — верхний край):
import { getData } from './data';
import type { ChartOptions } from 'grafit-charts';
export function createOptions(): ChartOptions {
return {
data: getData(),
title: { text: 'Value labels' },
series: [
{
type: 'heatmap',
xField: 'week',
yField: 'day',
colorField: 'deploys',
colorName: 'Deploys',
// text color is chosen automatically based on cell luminance
label: { enabled: true },
},
],
// tooltip at the cell center
tooltip: { position: { anchorTo: 'center' } },
legend: { enabled: false },
};
}export function getData() {
const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
const weeks = ['W1', 'W2', 'W3', 'W4'];
const data: Array<{ week: string; day: string; deploys: number }> = [];
weeks.forEach((week, w) => {
days.forEach((day, d) => {
data.push({ week, day, deploys: ((w * 5 + d * 3) % 11) + (d === 2 ? 6 : 1) });
});
});
return data;
}Кастомные подписи
formatter, шрифт, цвет и размещение (placement: 'top') настраиваются; шкалу можно отключить:
import { getData } from './data';
import type { ChartOptions } from 'grafit-charts';
export function createOptions(): ChartOptions {
return {
data: getData(),
title: { text: 'Custom labels' },
subtitle: { text: 'formatter, font and color; scale disabled' },
series: [
{
type: 'heatmap',
xField: 'week',
yField: 'day',
colorField: 'deploys',
colorRange: ['#fef3e2', '#e8590c'],
itemPadding: 4,
cornerRadius: 8,
label: {
enabled: true,
placement: 'top-left',
formatter: ({ value }) => `${value}×`,
fontSize: 13,
fontWeight: 'bold',
},
},
],
gradientLegend: { enabled: false },
legend: { enabled: false },
};
}export function getData() {
const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];
const weeks = ['W1', 'W2', 'W3', 'W4'];
const data: Array<{ week: string; day: string; deploys: number }> = [];
weeks.forEach((week, w) => {
days.forEach((day, d) => {
data.push({ week, day, deploys: ((w * 5 + d * 3) % 11) + (d === 2 ? 6 : 1) });
});
});
return data;
}| Опция | Тип | По умолчанию | Описание |
|---|---|---|---|
xField | string | — | категории по осям |
yField | string | — | категории по осям |
colorField | string | — | числовое значение → цвет |
colorRange | ColorValue[] | сине-голубая | стопы шкалы (2+) |
itemPadding | Pixels | 1 | зазор между ячейками |
cornerRadius | Pixels | 2 | скругление ячеек |
colorName | string | имя colorField | подпись значения в тултипе |
label.enabled | boolean | false | показать подписи значений |
label.placement | center, края и углы (9 позиций) | 'center' | позиция подписи |
label.formatter | ({ value, datum }) => string | значение | содержимое подписи |
label.fontSize | Pixels | 11 | размер шрифта подписи |
label.fontWeight | string | number | normal | насыщенность |
label.fontFamily | string | шрифт темы | гарнитура |
label.color | ColorValue | foreground; внутри — автоконтраст | цвет текста |
Опции
Общие опции всех серий (name, showInLegend, tooltip.renderer, …) — в разделе Общие опции серий.