Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nameFormatter and headerFormatter added #16942

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
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
37 changes: 31 additions & 6 deletions src/component/tooltip/TooltipView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,25 @@ class TooltipView extends ComponentView {
[series as Model<TooltipableOption>],
globalTooltipModel
).get('valueFormatter');
axisSectionMarkup.blocks.push(valueFormatter ? extend({ valueFormatter }, frag) : frag);
const nameFormatter = buildTooltipModel(
[series as Model<TooltipableOption>],
globalTooltipModel
).get('nameFormatter');
const headerFormatter = buildTooltipModel(
[series as Model<TooltipableOption>],
globalTooltipModel
).get('headerFormatter');
const newFrag = extend({}, frag);
valueFormatter && extend(newFrag, {
valueFormatter: valueFormatter
});
nameFormatter && extend(newFrag, {
nameFormatter: nameFormatter
});
headerFormatter && extend(newFrag, {
headerFormatter: headerFormatter
});
axisSectionMarkup.blocks.push(newFrag);
}
if (seriesTooltipResult.text) {
markupTextArrLegacy.push(seriesTooltipResult.text);
Expand Down Expand Up @@ -688,17 +706,24 @@ class TooltipView extends ComponentView {
);
const orderMode = tooltipModel.get('order');
const valueFormatter = tooltipModel.get('valueFormatter');
const nameFormatter = tooltipModel.get('nameFormatter');
const headerFormatter = tooltipModel.get('headerFormatter');
const frag = seriesTooltipResult.frag;
const markupText = frag ? buildTooltipMarkup(
valueFormatter ? extend({ valueFormatter }, frag) : frag,
let markupText = seriesTooltipResult.text;
if (frag) {
const newFrag = extend({}, frag);
valueFormatter && extend(newFrag, {valueFormatter: valueFormatter});
nameFormatter && extend(newFrag, {nameFormatter: nameFormatter});
headerFormatter && extend(newFrag, {headerFormatter: headerFormatter});
markupText = buildTooltipMarkup(
newFrag,
markupStyleCreator,
renderMode,
orderMode,
ecModel.get('useUTC'),
tooltipModel.get('textStyle')
)
: seriesTooltipResult.text;

);
}
const asyncTicket = 'item_' + dataModel.name + '_' + dataIndex;

this._showOrMove(tooltipModel, function (this: TooltipView) {
Expand Down
49 changes: 40 additions & 9 deletions src/component/tooltip/tooltipMarkup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ export interface TooltipMarkupSection extends TooltipMarkupBlock {
// Enable to sort blocks when making final html or richText.
sortBlocks?: boolean;

valueFormatter?: CommonTooltipOption<unknown>['valueFormatter']
valueFormatter?: CommonTooltipOption<unknown>['valueFormatter'];
nameFormatter?: CommonTooltipOption<unknown>['nameFormatter'];
headerFormatter?: CommonTooltipOption<unknown>['headerFormatter']
}

export interface TooltipMarkupNameValueBlock extends TooltipMarkupBlock {
Expand All @@ -163,7 +165,11 @@ export interface TooltipMarkupNameValueBlock extends TooltipMarkupBlock {
noName?: boolean;
noValue?: boolean;

valueFormatter?: CommonTooltipOption<unknown>['valueFormatter']
valueFormatter?: CommonTooltipOption<unknown>['valueFormatter'];
nameFormatter?: CommonTooltipOption<unknown>['nameFormatter'];
headerFormatter?: CommonTooltipOption<unknown>['headerFormatter']


}

/**
Expand Down Expand Up @@ -258,9 +264,21 @@ function buildSection(

each(subBlocks, function (subBlock, idx) {
const valueFormatter = fragment.valueFormatter;
const nameFormatter = fragment.nameFormatter;
const headerFormatter = fragment.headerFormatter;
const newCtx = extend({}, ctx);
valueFormatter && extend(newCtx, {
valueFormatter: valueFormatter
});
nameFormatter && extend(newCtx, {
nameFormatter: nameFormatter
});
headerFormatter && extend(newCtx, {
headerFormatter: headerFormatter
});
const subMarkupText = getBuilder(subBlock)(
// Inherit valueFormatter
valueFormatter ? extend(extend({}, ctx), { valueFormatter }) : ctx,
// Inherit special Formatters
newCtx,
subBlock,
idx > 0 ? gaps.html : 0,
toolTipTextStyle
Expand All @@ -278,8 +296,13 @@ function buildSection(
if (noHeader) {
return subMarkupText;
}

const displayableHeader = makeValueReadable(fragment.header, 'ordinal', ctx.useUTC);
const headerFormatter = fragment.headerFormatter
|| ctx.headerFormatter
|| subBlocks[0].headerFormatter
|| ((header) => {
return makeValueReadable(header, 'ordinal', ctx.useUTC);
});
const displayableHeader = String(headerFormatter(fragment.header as any));
const {nameStyle} = getTooltipTextStyle(toolTipTextStyle, ctx.renderMode);
if (ctx.renderMode === 'richText') {
return wrapInlineNameRichText(ctx, displayableHeader, nameStyle as RichTextStyle) + gaps.richText
Expand Down Expand Up @@ -315,6 +338,10 @@ function buildNameValue(
));
});

const nameFormatter = fragment.nameFormatter || ctx.nameFormatter || ((name) => {
return makeValueReadable(name, 'ordinal', useUTC);
});

if (noName && noValue) {
return;
}
Expand All @@ -328,7 +355,7 @@ function buildNameValue(
);
const readableName = noName
? ''
: makeValueReadable(name, 'ordinal', useUTC);
: nameFormatter(name);
const valueTypeOption = fragment.valueType;
const readableValueList = noValue ? [] : valueFormatter(fragment.value as OptionDataValue);
const valueAlignRight = !noMarker || !noName;
Expand Down Expand Up @@ -362,7 +389,9 @@ interface TooltipMarkupBuildContext {
orderMode: TooltipOrderMode;
markupStyleCreator: TooltipMarkupStyleCreator;

valueFormatter: CommonTooltipOption<unknown>['valueFormatter']
valueFormatter: CommonTooltipOption<unknown>['valueFormatter'];
nameFormatter?: CommonTooltipOption<unknown>['nameFormatter'];
headerFormatter?: CommonTooltipOption<unknown>['headerFormatter']
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing ;

}

/**
Expand All @@ -386,7 +415,9 @@ export function buildTooltipMarkup(
renderMode: renderMode,
orderMode: orderMode,
markupStyleCreator: markupStyleCreator,
valueFormatter: fragment.valueFormatter
valueFormatter: fragment.valueFormatter,
nameFormatter: fragment.nameFormatter,
headerFormatter: fragment.headerFormatter
};
return builder(ctx, fragment, 0, toolTipTextStyle);
}
Expand Down
12 changes: 12 additions & 0 deletions src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,18 @@ export interface CommonTooltipOption<FormatterParams> {
* Will be ignored if tooltip.formatter is specified.
*/
valueFormatter?: (value: OptionDataValue | OptionDataValue[]) => string
/**
* Formatter of value.
*
* Will be ignored if tooltip.formatter is specified.
*/
nameFormatter?: (value: OptionDataValue | OptionDataValue[]) => string
/**
* Formatter of value.
*
* Will be ignored if tooltip.formatter is specified.
*/
headerFormatter?: (value: OptionDataValue | OptionDataValue[]) => string
/**
* Absolution pixel [x, y] array. Or relative percent string [x, y] array.
* If trigger is 'item'. position can be set to 'inside' / 'top' / 'left' / 'right' / 'bottom',
Expand Down