Skip to content

Commit

Permalink
fix(demo): disable series tooltip for regression (#4912)
Browse files Browse the repository at this point in the history
* fix(demo): disable series tooltip for regression

* fix(tooltip): series tooltip need defiend series element
  • Loading branch information
pearmini authored and hustcc committed May 16, 2023
1 parent c8576e7 commit accd9e7
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
1 change: 1 addition & 0 deletions __tests__/plots/tooltip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
42 changes: 42 additions & 0 deletions __tests__/plots/tooltip/points-point-regression-quad.ts
Original file line number Diff line number Diff line change
@@ -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]);
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ chart
style: {
textAlign: 'end',
},
});
})
.tooltip(false);

chart.render();
2 changes: 0 additions & 2 deletions site/examples/analysis/regression/demo/linear-regression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,4 @@ chart
})
.tooltip(false);

chart.interaction('tooltip', { series: false });

chart.render();
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ chart
style: {
textAlign: 'end',
},
});
})
.tooltip(false);

chart.render();
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ chart
textAlign: 'end',
dx: -8,
},
});
})
.tooltip(null);

chart.render();
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
10 changes: 8 additions & 2 deletions src/interaction/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -599,15 +606,14 @@ 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');
const plotArea = selectPlotArea(container);
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,
Expand Down

0 comments on commit accd9e7

Please sign in to comment.