From accd9e73012f1365f5b8b0b184b7c49588e427ee Mon Sep 17 00:00:00 2001 From: MiniPear Date: Tue, 25 Apr 2023 14:56:25 +0800 Subject: [PATCH] fix(demo): disable series tooltip for regression (#4912) * fix(demo): disable series tooltip for regression * fix(tooltip): series tooltip need defiend series element --- .../points-point-regression-quad/step0.html | 1 + __tests__/plots/tooltip/index.ts | 1 + .../tooltip/points-point-regression-quad.ts | 42 +++++++++++++++++++ .../regression/demo/exponential-regression.ts | 3 +- .../regression/demo/linear-regression.ts | 2 - .../regression/demo/logarithmic-regression.ts | 3 +- .../regression/demo/polynomial-regression.ts | 3 +- .../regression/demo/quadratic-regression.ts | 3 +- src/interaction/tooltip.ts | 10 ++++- 9 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 __tests__/integration/snapshots/tooltip/points-point-regression-quad/step0.html create mode 100644 __tests__/plots/tooltip/points-point-regression-quad.ts diff --git a/__tests__/integration/snapshots/tooltip/points-point-regression-quad/step0.html b/__tests__/integration/snapshots/tooltip/points-point-regression-quad/step0.html new file mode 100644 index 0000000000..ec747fa47d --- /dev/null +++ b/__tests__/integration/snapshots/tooltip/points-point-regression-quad/step0.html @@ -0,0 +1 @@ +null \ No newline at end of file diff --git a/__tests__/plots/tooltip/index.ts b/__tests__/plots/tooltip/index.ts index 0ad22b7b14..47fb59ed52 100644 --- a/__tests__/plots/tooltip/index.ts +++ b/__tests__/plots/tooltip/index.ts @@ -60,3 +60,4 @@ export { aaplAreaMissingDataTranspose } from './aapl-area-missing-data-transpose export { alphabetIntervalBrushTooltip } from './alphabet-interval-brush-tooltip'; export { mockLineFalsy } from './mock-line-falsy'; export { provincesLineGroupName } from './provinces-line-group-name'; +export { pointsPointRegressionQuad } from './points-point-regression-quad'; diff --git a/__tests__/plots/tooltip/points-point-regression-quad.ts b/__tests__/plots/tooltip/points-point-regression-quad.ts new file mode 100644 index 0000000000..95a3fdbae4 --- /dev/null +++ b/__tests__/plots/tooltip/points-point-regression-quad.ts @@ -0,0 +1,42 @@ +import { regressionQuad } from 'd3-regression'; +import { G2Spec } from '../../../src'; +import { points } from '../../data/points'; +import { seriesTooltipSteps } from './utils'; + +const regression = regressionQuad() + .x((d) => d.x) + // @ts-ignore + .y((d) => d.y) + .domain([-4, 4]); + +export function pointsPointRegressionQuad(): G2Spec { + return { + type: 'view', + data: points, + scale: { x: { domain: [-4, 4] }, y: { domain: [-2, 14] } }, + axis: { x: { title: false }, y: { title: false } }, + children: [ + { + type: 'point', + encode: { x: 'x', y: 'y', shape: 'point' }, + }, + { + type: 'line', + data: { transform: [{ type: 'custom', callback: regression }] }, + encode: { + x: (d) => d[0], + y: (d) => d[1], + }, + style: { + stroke: '#30BF78', + lineWidth: 2, + }, + tooltip: false, + }, + { type: 'lineX', data: [0] }, + { type: 'lineY', data: [0] }, + ], + }; +} + +pointsPointRegressionQuad.steps = seriesTooltipSteps([100, 300]); diff --git a/site/examples/analysis/regression/demo/exponential-regression.ts b/site/examples/analysis/regression/demo/exponential-regression.ts index d8de519f02..e949f01750 100644 --- a/site/examples/analysis/regression/demo/exponential-regression.ts +++ b/site/examples/analysis/regression/demo/exponential-regression.ts @@ -46,6 +46,7 @@ chart style: { textAlign: 'end', }, - }); + }) + .tooltip(false); chart.render(); diff --git a/site/examples/analysis/regression/demo/linear-regression.ts b/site/examples/analysis/regression/demo/linear-regression.ts index 09db7ec006..823475448b 100644 --- a/site/examples/analysis/regression/demo/linear-regression.ts +++ b/site/examples/analysis/regression/demo/linear-regression.ts @@ -49,6 +49,4 @@ chart }) .tooltip(false); -chart.interaction('tooltip', { series: false }); - chart.render(); diff --git a/site/examples/analysis/regression/demo/logarithmic-regression.ts b/site/examples/analysis/regression/demo/logarithmic-regression.ts index 67a80b2018..ba7d99c884 100644 --- a/site/examples/analysis/regression/demo/logarithmic-regression.ts +++ b/site/examples/analysis/regression/demo/logarithmic-regression.ts @@ -48,6 +48,7 @@ chart style: { textAlign: 'end', }, - }); + }) + .tooltip(false); chart.render(); diff --git a/site/examples/analysis/regression/demo/polynomial-regression.ts b/site/examples/analysis/regression/demo/polynomial-regression.ts index e24c662e1f..207ca6c9a4 100644 --- a/site/examples/analysis/regression/demo/polynomial-regression.ts +++ b/site/examples/analysis/regression/demo/polynomial-regression.ts @@ -56,6 +56,7 @@ chart textAlign: 'end', dx: -8, }, - }); + }) + .tooltip(null); chart.render(); diff --git a/site/examples/analysis/regression/demo/quadratic-regression.ts b/site/examples/analysis/regression/demo/quadratic-regression.ts index 1e25b14203..97391142c2 100644 --- a/site/examples/analysis/regression/demo/quadratic-regression.ts +++ b/site/examples/analysis/regression/demo/quadratic-regression.ts @@ -49,7 +49,8 @@ chart .encode('x', (d) => d[0]) .encode('y', (d) => d[1]) .style('stroke', '#30BF78') - .style('lineWidth', 2); + .style('lineWidth', 2) + .tooltip(false); chart.lineX().data([0]); chart.lineY().data([0]); diff --git a/src/interaction/tooltip.ts b/src/interaction/tooltip.ts index 31bc817b1e..3dd43bb17d 100644 --- a/src/interaction/tooltip.ts +++ b/src/interaction/tooltip.ts @@ -279,6 +279,13 @@ function isEmptyTooltipData(data) { return false; } +function hasSeries(markState): boolean { + return Array.from(markState.values()).some( + // @ts-ignore + (d) => d.interaction?.seriesTooltip && d.tooltip, + ); +} + /** * Show tooltip for series item. */ @@ -599,7 +606,6 @@ export function Tooltip(options) { return (target, viewInstances) => { const { container, view } = target; const { scale, markState, coordinate } = view; - // Get default value from mark states. const defaultSeries = interactionKeyof(markState, 'seriesTooltip'); const defaultShowCrosshairs = interactionKeyof(markState, 'crosshairs'); @@ -607,7 +613,7 @@ export function Tooltip(options) { const isSeries = maybeValue(series, defaultSeries); // For non-facet and series tooltip. - if (isSeries && !facet) { + if (isSeries && hasSeries(markState) && !facet) { return seriesTooltip(plotArea, { ...rest, elements: selectG2Elements,