diff --git a/src/component/tooltip/TooltipView.ts b/src/component/tooltip/TooltipView.ts index 044a12ffca..17fef2296f 100644 --- a/src/component/tooltip/TooltipView.ts +++ b/src/component/tooltip/TooltipView.ts @@ -20,7 +20,7 @@ import { bind, each, clone, trim, isString, isFunction, isArray, isObject, exten import env from 'zrender/src/core/env'; import TooltipHTMLContent from './TooltipHTMLContent'; import TooltipRichContent from './TooltipRichContent'; -import { convertToColorString, encodeHTML, formatTpl, TooltipMarker } from '../../util/format'; +import { convertToColorString, encodeHTML, formatTpl } from '../../util/format'; import { parsePercent } from '../../util/number'; import { Rect } from '../../util/graphic'; import findPointFromSeries from '../axisPointer/findPointFromSeries'; @@ -124,16 +124,7 @@ interface TryShowParams { positionDefault?: TooltipOption['position'] } -type TooltipCallbackDataParams = CallbackDataParams & { - axisDim?: string - axisIndex?: number - axisType?: string - axisId?: string - // TODO: TYPE Value type - axisValue?: string | number - axisValueLabel?: string - marker?: TooltipMarker -}; +type TooltipCallbackDataParams = CallbackDataParams; class TooltipView extends ComponentView { static type = 'tooltip' as const; diff --git a/src/util/types.ts b/src/util/types.ts index 3496f5eacc..5312234cd7 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -980,6 +980,14 @@ export interface CallbackDataParams { dimensionIndex?: number; percent?: number; // Only for chart like 'pie' + // Axis context properties (provided in axis callbacks or axis-triggered tooltips) + axisDim?: string; + axisIndex?: number; + axisType?: string; + axisId?: string; + axisValue?: string | number; + axisValueLabel?: string; + // Param name list for mapping `a`, `b`, `c`, `d`, `e` $vars: string[]; } diff --git a/test/ut/spec/util/types.test.ts b/test/ut/spec/util/types.test.ts new file mode 100644 index 0000000000..c7f629b7d0 --- /dev/null +++ b/test/ut/spec/util/types.test.ts @@ -0,0 +1,64 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* 'License'); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ + +import { CallbackDataParams } from '@/src/util/types'; + +describe('util/types', function () { + describe('CallbackDataParams', function () { + it('should allow accessing axis context properties without type assertions', function () { + const params: CallbackDataParams = { + componentType: 'series', + componentSubType: 'line', + componentIndex: 0, + seriesType: 'line', + seriesIndex: 0, + seriesId: 'series_0', + seriesName: 'Sales', + name: 'Monday', + dataIndex: 1, + data: 120, + value: 120, + $vars: ['name', 'value'], + axisDim: 'x', + axisIndex: 0, + axisType: 'categoryAxis', + axisId: 'xAxis_0', + axisValue: 1, + axisValueLabel: 'Monday' + }; + + expect(params.axisDim).toBe('x'); + expect(params.axisIndex).toBe(0); + expect(params.axisType).toBe('categoryAxis'); + expect(params.axisId).toBe('xAxis_0'); + expect(params.axisValue).toBe(1); + expect(params.axisValueLabel).toBe('Monday'); + + // Formatter simulation using CallbackDataParams + function formatTooltip(cbParams: CallbackDataParams): string { + if (cbParams.axisValueLabel != null) { + return `${cbParams.axisValueLabel}: ${cbParams.value}`; + } + return `${cbParams.name}: ${cbParams.value}`; + } + + expect(formatTooltip(params)).toBe('Monday: 120'); + }); + }); +});