Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stacked scatter tooltip not show #764

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/core/src/components/graphs/scatter-stacked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,48 @@ export class StackedScatter extends Scatter {
// Add event listeners to elements drawn
this.addEventListeners();
}

getTooltipData(hoveredX, hoveredY) {
const domainIdentifier = this.services.cartesianScales.getDomainIdentifier();
const rangeIdentifier = this.services.cartesianScales.getRangeIdentifier();
const options = this.model.getOptions();
const { groupMapsTo } = options.data;
const percentage = Object.keys(options.axes).some(
(axis) => options.axes[axis].percentage
);
const stackedData = this.model.getStackedData({ percentage });
const tooltipData = [];
stackedData.forEach((groupData, groupDataIndex) => {
groupData.forEach((datum, dataIndex) => {
const group = datum[groupMapsTo];
const domainValue = datum["data"]["sharedStackKey"];
let rangeValue = datum["data"][group];
const stackedRangeValue = datum[1];
if (
rangeValue &&
hoveredX ===
this.services.cartesianScales.getDomainValue(
domainValue
) &&
hoveredY ===
this.services.cartesianScales.getRangeValue(
stackedRangeValue
)
) {
if (percentage) {
rangeValue = this.model.getStackedData()[
groupDataIndex
][dataIndex]["data"][group];
}

tooltipData.push({
[groupMapsTo]: group,
[domainIdentifier]: domainValue,
[rangeIdentifier]: rangeValue
});
}
});
});
return tooltipData;
}
}
25 changes: 11 additions & 14 deletions packages/core/src/components/graphs/scatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@ export class Scatter extends Component {
.attr("opacity", 1);
}

getTooltipData(hoveredX, hoveredY) {
return this.model.getDisplayData().filter((d) => {
return (
hoveredX === this.services.cartesianScales.getDomainValue(d) &&
hoveredY === this.services.cartesianScales.getRangeValue(d)
);
});
}

addEventListeners() {
const self = this;
const { groupMapsTo } = this.model.getOptions().data;
Expand All @@ -318,23 +327,11 @@ export class Scatter extends Component {
const hoveredY = self.services.cartesianScales.getRangeValue(
datum
);
const overlappingData = self.model
.getDisplayData()
.filter((d) => {
return (
hoveredX ===
self.services.cartesianScales.getDomainValue(
d
) &&
hoveredY ===
self.services.cartesianScales.getRangeValue(d)
);
});

const tooltipData = self.getTooltipData(hoveredX, hoveredY);
// Show tooltip
self.services.events.dispatchEvent(Events.Tooltip.SHOW, {
hoveredElement,
data: overlappingData
data: tooltipData
});

// Dispatch mouse event
Expand Down