Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest using 'coord' instead of 'xyLocatoer' to align with MatrixBodyCornerCellOption.coord.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

};
if (isFunction(formatter)) {
const formattedText = formatter(params);
if (formattedText != null) {
text = formattedText + '';
}
}
else if (isString(formatter)) {
text = formatTplSimple(formatter, params);
}
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The order of checks should be isString(formatter) first, then isFunction(formatter) to maintain consistency with the rest of the codebase. See similar patterns in src/component/axisPointer/viewHelper.ts:181-184, src/component/tooltip/TooltipView.ts:837-847, and src/component/calendar/CalendarView.ts:232-237.

Suggested change
if (isFunction(formatter)) {
const formattedText = formatter(params);
if (formattedText != null) {
text = formattedText + '';
}
}
else if (isString(formatter)) {
text = formatTplSimple(formatter, params);
}
if (isString(formatter)) {
text = formatTplSimple(formatter, params);
}
else if (isFunction(formatter)) {
const formattedText = formatter(params);
if (formattedText != null) {
text = formattedText + '';
}
}

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nice-to-have suggestion. Not a must.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

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'];
Copy link

Copilot AI Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The readonly modifier on $vars is inconsistent with other FormatterParams interfaces in the codebase. For consistency with interfaces like ToolboxTooltipFormatterParams, LegendTooltipFormatterParams, and GeoTooltipFormatterParams, this should be $vars: ['name', 'value', 'xyLocator'] without the readonly modifier.

Suggested change
$vars: readonly ['name', 'value', 'xyLocator'];
$vars: ['name', 'value', 'xyLocator'];

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see other places using readonly, although not $vars, and it's reasonable to use it here.

}

/**
* 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.