feat: Utility function to render chart tooltips (#27950)

This commit is contained in:
Michael S. Molina
2024-05-07 13:00:30 -03:00
committed by GitHub
parent 467e612533
commit b549977f05
23 changed files with 512 additions and 425 deletions

View File

@@ -22,13 +22,11 @@ import {
NumberFormats,
GenericDataType,
getMetricLabel,
t,
smartDateVerboseFormatter,
TimeFormatter,
getXAxisLabel,
Metric,
ValueFormatter,
getValueFormatter,
t,
tooltipHtml,
} from '@superset-ui/core';
import { EChartsCoreOption, graphic } from 'echarts';
import {
@@ -41,24 +39,6 @@ import { getDateFormatter, parseMetricValue } from '../utils';
import { getDefaultTooltip } from '../../utils/tooltip';
import { Refs } from '../../types';
const defaultNumberFormatter = getNumberFormatter();
export function renderTooltipFactory(
formatDate: TimeFormatter = smartDateVerboseFormatter,
formatValue: ValueFormatter | TimeFormatter = defaultNumberFormatter,
) {
return function renderTooltip(params: { data: TimeSeriesDatum }[]) {
return `
${formatDate(params[0].data[0])}
<br />
<strong>
${
params[0].data[1] === null ? t('N/A') : formatValue(params[0].data[1])
}
</strong>
`;
};
}
const formatPercentChange = getNumberFormatter(
NumberFormats.PERCENT_SIGNED_1_POINT,
);
@@ -249,7 +229,18 @@ export default function transformProps(
...getDefaultTooltip(refs),
show: !inContextMenu,
trigger: 'axis',
formatter: renderTooltipFactory(formatTime, headerFormatter),
formatter: (params: { data: TimeSeriesDatum }[]) =>
tooltipHtml(
[
[
metricName,
params[0].data[1] === null
? t('N/A')
: headerFormatter.format(params[0].data[1]),
],
],
formatTime(params[0].data[0]),
),
},
aria: {
enabled: true,