From cfbab5a199dd8fc150ffd15777b3f88b3f3a2cce Mon Sep 17 00:00:00 2001 From: MiniPear Date: Mon, 24 Apr 2023 20:55:53 +0800 Subject: [PATCH 1/2] fix(demo): disable series tooltip for regression --- .../analysis/regression/demo/exponential-regression.ts | 5 ++++- .../analysis/regression/demo/logarithmic-regression.ts | 5 ++++- .../analysis/regression/demo/polynomial-regression.ts | 5 ++++- .../analysis/regression/demo/quadratic-regression.ts | 5 ++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/site/examples/analysis/regression/demo/exponential-regression.ts b/site/examples/analysis/regression/demo/exponential-regression.ts index d8de519f02..3b750a6bdc 100644 --- a/site/examples/analysis/regression/demo/exponential-regression.ts +++ b/site/examples/analysis/regression/demo/exponential-regression.ts @@ -46,6 +46,9 @@ chart style: { textAlign: 'end', }, - }); + }) + .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..a24e85081f 100644 --- a/site/examples/analysis/regression/demo/logarithmic-regression.ts +++ b/site/examples/analysis/regression/demo/logarithmic-regression.ts @@ -48,6 +48,9 @@ chart style: { textAlign: 'end', }, - }); + }) + .tooltip(false); + +chart.interaction('tooltip', { series: 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..02a8f92635 100644 --- a/site/examples/analysis/regression/demo/polynomial-regression.ts +++ b/site/examples/analysis/regression/demo/polynomial-regression.ts @@ -56,6 +56,9 @@ chart textAlign: 'end', dx: -8, }, - }); + }) + .tooltip(null); + +chart.interaction('tooltip', { series: false }); chart.render(); diff --git a/site/examples/analysis/regression/demo/quadratic-regression.ts b/site/examples/analysis/regression/demo/quadratic-regression.ts index 1e25b14203..7760e9ec9d 100644 --- a/site/examples/analysis/regression/demo/quadratic-regression.ts +++ b/site/examples/analysis/regression/demo/quadratic-regression.ts @@ -49,9 +49,12 @@ 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]); +chart.interaction('tooltip', { series: false }); + chart.render(); From e7866f782a7b1920ee41eb5c37c6d8cd86eaac8c Mon Sep 17 00:00:00 2001 From: MiniPear Date: Tue, 25 Apr 2023 11:08:07 +0800 Subject: [PATCH 2/2] 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 | 2 - .../regression/demo/linear-regression.ts | 2 - .../regression/demo/logarithmic-regression.ts | 2 - .../regression/demo/polynomial-regression.ts | 2 - .../regression/demo/quadratic-regression.ts | 2 - src/interaction/tooltip.ts | 10 ++++- 9 files changed, 52 insertions(+), 12 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 3b750a6bdc..e949f01750 100644 --- a/site/examples/analysis/regression/demo/exponential-regression.ts +++ b/site/examples/analysis/regression/demo/exponential-regression.ts @@ -49,6 +49,4 @@ chart }) .tooltip(false); -chart.interaction('tooltip', { series: 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 a24e85081f..ba7d99c884 100644 --- a/site/examples/analysis/regression/demo/logarithmic-regression.ts +++ b/site/examples/analysis/regression/demo/logarithmic-regression.ts @@ -51,6 +51,4 @@ chart }) .tooltip(false); -chart.interaction('tooltip', { series: false }); - chart.render(); diff --git a/site/examples/analysis/regression/demo/polynomial-regression.ts b/site/examples/analysis/regression/demo/polynomial-regression.ts index 02a8f92635..207ca6c9a4 100644 --- a/site/examples/analysis/regression/demo/polynomial-regression.ts +++ b/site/examples/analysis/regression/demo/polynomial-regression.ts @@ -59,6 +59,4 @@ chart }) .tooltip(null); -chart.interaction('tooltip', { series: false }); - chart.render(); diff --git a/site/examples/analysis/regression/demo/quadratic-regression.ts b/site/examples/analysis/regression/demo/quadratic-regression.ts index 7760e9ec9d..97391142c2 100644 --- a/site/examples/analysis/regression/demo/quadratic-regression.ts +++ b/site/examples/analysis/regression/demo/quadratic-regression.ts @@ -55,6 +55,4 @@ chart chart.lineX().data([0]); chart.lineY().data([0]); -chart.interaction('tooltip', { series: false }); - chart.render(); 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,