From af4d2ee6a4f41830fa13a0443db18b397ee99292 Mon Sep 17 00:00:00 2001 From: Marek Mihok Date: Mon, 27 Feb 2023 10:38:56 +0100 Subject: [PATCH] fix: Display only the plot data inside the plot tooltip #1836 (#1841) --- ui/src/plot.tsx | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/ui/src/plot.tsx b/ui/src/plot.tsx index 8902e5b127..cce444f866 100644 --- a/ui/src/plot.tsx +++ b/ui/src/plot.tsx @@ -588,6 +588,7 @@ const return mark }, refactorData = (ds: any[], marks: MarkExt[]): any[] => { + ds.forEach((d, idx) => d.idx = idx) for (const m of marks) { if (m.x_scale === 'time') { for (const { x_field, x0_field } of marks) { @@ -1039,22 +1040,19 @@ export interface Visualization { const tooltipContainer = document.createElement('div') tooltipContainer.className = 'g2-tooltip' -const PlotTooltip = ({ items }: { items: TooltipItem[] }) => +const PlotTooltip = ({ items, originalItems }: { items: TooltipItem[], originalItems: any[] }) => <> {items.map(({ data, mappingData, color }: TooltipItem) => - Object.keys(data).map((item, idx) => -
  • + Object.keys(originalItems[data.idx]).map((itemKey, idx) => { + const item = originalItems[data.idx][itemKey] + return
  • - {item}: - - {(Array.isArray(data[item]) ? data[item] : [data[item]]).map((val: any, idx: number) => { - const value = val instanceof Date ? val.toISOString().split('T')[0] : val - return idx > 0 ? ` - ${value}` : value - })} - + {itemKey}: + {(item instanceof Date ? item.toISOString().split('T')[0] : item)}
  • + } ) )} @@ -1068,6 +1066,7 @@ export const currentChart = React.useRef(null), currentPlot = React.useRef(null), themeWatchRef = React.useRef(null), + originalDataRef = React.useRef([]), checkDimensionsPostInit = (w: F, h: F) => { // Safari fix const el = container.current if (!el) return @@ -1092,6 +1091,7 @@ export const data = refactorData(raw_data, plot.marks), { Chart } = await import('@antv/g2'), chart = plot.marks ? new Chart(makeChart(el, space, plot.marks, model.interactions || [])) : null + originalDataRef.current = unpack(model.data) currentPlot.current = plot if (chart) { chart.tooltip({ @@ -1105,7 +1105,7 @@ export const }, }, customContent: (_title, items) => { - ReactDOM.render(, tooltipContainer) + ReactDOM.render(, tooltipContainer) return tooltipContainer } }) @@ -1155,8 +1155,8 @@ export const const raw_data = unpack(model.data), data = refactorData(raw_data, currentPlot.current.marks) + originalDataRef.current = unpack(model.data) currentChart.current.changeData(data) - }, [currentChart, currentPlot, model]) const