Tooltip: partial perf improvement (#31774) (#31837)

This commit is contained in:
Leon Sorokin 2021-03-09 14:25:40 -06:00 committed by GitHub
parent 2d660ee502
commit 04e46e3853
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -59,6 +59,7 @@ export const TooltipPlugin: React.FC<TooltipPluginProps> = ({ mode = 'single', t
// when interacting with a point in single mode
if (mode === 'single' && originFieldIndex !== null) {
const field = otherProps.data[originFieldIndex.frameIndex].fields[originFieldIndex.fieldIndex];
const plotSeries = plotContext.getSeries();
const fieldFmt = field.display || getDisplayProcessor({ field, timeZone });
tooltip = (
@ -66,7 +67,7 @@ export const TooltipPlugin: React.FC<TooltipPluginProps> = ({ mode = 'single', t
series={[
{
// TODO: align with uPlot typings
color: (plotContext.getSeries()[focusedSeriesIdx!].stroke as any)(),
color: (plotSeries[focusedSeriesIdx!].stroke as any)(),
label: getFieldDisplayName(field, otherProps.data[originFieldIndex.frameIndex]),
value: fieldFmt(field.values.get(focusedPointIdx)).text,
},
@ -77,34 +78,38 @@ export const TooltipPlugin: React.FC<TooltipPluginProps> = ({ mode = 'single', t
}
if (mode === 'multi') {
const plotSeries = plotContext.getSeries();
let series: SeriesTableRowProps[] = [];
for (let i = 0; i < otherProps.data.length; i++) {
series = series.concat(
otherProps.data[i].fields.reduce<SeriesTableRowProps[]>((agg, f, j) => {
// skipping xField, time fields, and non-numeric fields
if (f === xField || f.type === FieldType.time || f.type !== FieldType.number) {
return agg;
}
let frames = otherProps.data;
if (f.config.custom?.hideFrom?.tooltip) {
return agg;
}
for (let i = 0; i < frames.length; i++) {
let fields = frames[i].fields;
return [
...agg,
{
// TODO: align with uPlot typings
color: (plotContext.getSeries()[j].stroke as any)!(),
label: getFieldDisplayName(f, otherProps.data[i]),
value: formattedValueToString(f.display!(f.values.get(focusedPointIdx!))),
isActive: originFieldIndex
? originFieldIndex.frameIndex === i && originFieldIndex.fieldIndex === j
: false,
},
];
}, [])
);
for (let j = 0; j < fields.length; j++) {
let f = fields[j];
// skipping xField, time fields, non-numeric, and hidden fields
if (
f === xField ||
f.type === FieldType.time ||
f.type !== FieldType.number ||
f.config.custom?.hideFrom?.tooltip
) {
continue;
}
series.push({
// TODO: align with uPlot typings
color: (plotSeries[j].stroke as any)!(),
label: getFieldDisplayName(f, otherProps.data[i]),
value: formattedValueToString(f.display!(f.values.get(focusedPointIdx!))),
isActive: originFieldIndex
? originFieldIndex.frameIndex === i && originFieldIndex.fieldIndex === j
: false,
});
}
}
tooltip = <SeriesTable series={series} timestamp={xVal} />;