Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/component/matrix/MatrixView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import { LineShape } from 'zrender/src/graphic/shape/Line';
import { subPixelOptimize } from 'zrender/src/graphic/helper/subPixelOptimize';
import { Group, Text, Rect, Line, XY, setTooltipConfig, expandOrShrinkRect } from '../../util/graphic';
import { clearTmpModel, ListIterator } from '../../util/model';
import { clone, retrieve2 } from 'zrender/src/core/util';
import { clone, retrieve2, isFunction, isString } from 'zrender/src/core/util';
import { formatTplSimple } from '../../util/format';
import { invert } from 'zrender/src/core/matrix';
import { MatrixBodyCorner, MatrixBodyOrCornerKind } from '../../coord/matrix/MatrixBodyCorner';
import { setLabelStyle } from '../../label/labelStyle';
Expand Down Expand Up @@ -289,12 +290,33 @@ function createMatrixCell(
let cellText: Text | NullUndefined;

if (textValue != null) {
const text = textValue + '';
let text = textValue + '';
_tmpCellLabelModel.option = cellOption ? cellOption.label : null;
_tmpCellLabelModel.parentModel = parentLabelModel;
// This is to accept `option.textStyle` as the default.
_tmpCellLabelModel.ecModel = ecModel;

const formatter = _tmpCellLabelModel.getShallow('formatter');
if (formatter) {
const params = {
componentType: 'matrix' as const,
componentIndex: matrixModel.componentIndex,
name: text,
value: textValue as unknown,
xyLocator: xyLocator.slice() as MatrixXYLocator[],
$vars: ['name', 'value', 'xyLocator'] as const
};
if (isFunction(formatter)) {
const formattedText = formatter(params);
if (formattedText != null) {
text = formattedText + '';
}
}
else if (isString(formatter)) {
text = formatTplSimple(formatter, params);
}
}

setLabelStyle(
cellRect,
// Currently do not support other states (`emphasis`, `select`, `blur`)
Expand Down
15 changes: 14 additions & 1 deletion src/coord/matrix/MatrixModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ export interface MatrixDimensionLevelOption {
export interface MatrixDimensionModel extends Model<MatrixDimensionOption> {
}

export interface MatrixLabelOption extends LabelOption {
formatter?: string | ((params: MatrixLabelFormatterParams) => string);
}

export interface MatrixLabelFormatterParams {
componentType: 'matrix';
componentIndex: number;
name: string;
value: unknown;
xyLocator: MatrixXYLocator[];
$vars: readonly ['name', 'value', 'xyLocator'];
}

/**
* Two levels of cascade inheritance:
* - priority-high: style options defined in `matrix.x/y/coner/body.data[i]` (in cell)
Expand All @@ -209,7 +222,7 @@ export interface MatrixCellStyleOption {
// The text truncation rect is obtained by cell rect minus by padding.
// - The inner series / other coord sys padding is not supported, to avoid necessary complexity.
// Consider some series, such as heatmap, prefer no padding.
label?: LabelOption;
label?: MatrixLabelOption;
itemStyle?: ItemStyleOption;
cursor?: string;
// By default, auto decide whether to be silent, considering tooltip.
Expand Down
208 changes: 208 additions & 0 deletions test/matrix-label-formatter.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.