Skip to content

Commit 09c721f

Browse files
committed
fix(matrix): label formatter not work
1 parent 7a39313 commit 09c721f

File tree

3 files changed

+246
-3
lines changed

3 files changed

+246
-3
lines changed

src/component/matrix/MatrixView.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import { LineShape } from 'zrender/src/graphic/shape/Line';
3131
import { subPixelOptimize } from 'zrender/src/graphic/helper/subPixelOptimize';
3232
import { Group, Text, Rect, Line, XY, setTooltipConfig, expandOrShrinkRect } from '../../util/graphic';
3333
import { clearTmpModel, ListIterator } from '../../util/model';
34-
import { clone, retrieve2 } from 'zrender/src/core/util';
34+
import { clone, retrieve2, isFunction, isString } from 'zrender/src/core/util';
35+
import { formatTplSimple } from '../../util/format';
3536
import { invert } from 'zrender/src/core/matrix';
3637
import { MatrixBodyCorner, MatrixBodyOrCornerKind } from '../../coord/matrix/MatrixBodyCorner';
3738
import { setLabelStyle } from '../../label/labelStyle';
@@ -289,12 +290,33 @@ function createMatrixCell(
289290
let cellText: Text | NullUndefined;
290291

291292
if (textValue != null) {
292-
const text = textValue + '';
293+
let text = textValue + '';
293294
_tmpCellLabelModel.option = cellOption ? cellOption.label : null;
294295
_tmpCellLabelModel.parentModel = parentLabelModel;
295296
// This is to accept `option.textStyle` as the default.
296297
_tmpCellLabelModel.ecModel = ecModel;
297298

299+
const formatter = _tmpCellLabelModel.getShallow('formatter');
300+
if (formatter) {
301+
const params = {
302+
componentType: 'matrix' as const,
303+
componentIndex: matrixModel.componentIndex,
304+
name: text,
305+
value: textValue as unknown,
306+
xyLocator: xyLocator.slice() as MatrixXYLocator[],
307+
$vars: ['name', 'value', 'xyLocator'] as const
308+
};
309+
if (isFunction(formatter)) {
310+
const formattedText = formatter(params);
311+
if (formattedText != null) {
312+
text = formattedText + '';
313+
}
314+
}
315+
else if (isString(formatter)) {
316+
text = formatTplSimple(formatter, params);
317+
}
318+
}
319+
298320
setLabelStyle(
299321
cellRect,
300322
// Currently do not support other states (`emphasis`, `select`, `blur`)

src/coord/matrix/MatrixModel.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ export interface MatrixDimensionLevelOption {
195195
export interface MatrixDimensionModel extends Model<MatrixDimensionOption> {
196196
}
197197

198+
export interface MatrixLabelOption extends LabelOption {
199+
formatter?: string | ((params: MatrixLabelFormatterParams) => string);
200+
}
201+
202+
export interface MatrixLabelFormatterParams {
203+
componentType: 'matrix';
204+
componentIndex: number;
205+
name: string;
206+
value: unknown;
207+
xyLocator: MatrixXYLocator[];
208+
$vars: readonly ['name', 'value', 'xyLocator'];
209+
}
210+
198211
/**
199212
* Two levels of cascade inheritance:
200213
* - priority-high: style options defined in `matrix.x/y/coner/body.data[i]` (in cell)
@@ -209,7 +222,7 @@ export interface MatrixCellStyleOption {
209222
// The text truncation rect is obtained by cell rect minus by padding.
210223
// - The inner series / other coord sys padding is not supported, to avoid necessary complexity.
211224
// Consider some series, such as heatmap, prefer no padding.
212-
label?: LabelOption;
225+
label?: MatrixLabelOption;
213226
itemStyle?: ItemStyleOption;
214227
cursor?: string;
215228
// By default, auto decide whether to be silent, considering tooltip.

test/matrix-label-formatter.html

Lines changed: 208 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)