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

feat : Added boxplot stats as tooltip for axis label #5125

Merged
merged 3 commits into from
Mar 11, 2025
Merged
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
82 changes: 69 additions & 13 deletions src/shared/components/plots/BoxScatterPlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export type BoxModel = {
q3: number;
x?: number;
y?: number;
eventkey?: number;
sortedVector: number[];
};

Expand Down Expand Up @@ -168,6 +169,7 @@ export default class BoxScatterPlot<
@observable.ref private container: HTMLDivElement;
@observable.ref private boxPlotTooltipModel: any | null;
@observable private mousePosition = { x: 0, y: 0 };
@observable.ref private axisLabelTooltipModel: any | null;

private scatterPlotTooltipHelper: ScatterPlotTooltipHelper = new ScatterPlotTooltipHelper();

Expand Down Expand Up @@ -224,6 +226,41 @@ export default class BoxScatterPlot<
}
return [];
}
private get axisLabelEvents() {
if (this.props.boxPlotTooltip) {
const self = this;
return [
{
target: 'tickLabels',
eventHandlers: {
onMouseEnter: (event: any, props: any) => {
const groupIndex = props.index;
if (
groupIndex !== undefined &&
self.boxPlotData[groupIndex]
) {
self.axisLabelTooltipModel = {
datum: self.boxPlotData[groupIndex],
eventkey: groupIndex,
};
self.mousePosition = {
x: event.pageX,
y: event.pageY,
};
}
return [];
},

onMouseLeave: () => {
self.axisLabelTooltipModel = null;
return [];
},
},
},
];
}
return [];
}

private get title() {
if (this.props.title) {
Expand Down Expand Up @@ -641,6 +678,7 @@ export default class BoxScatterPlot<
}
/>
}
events={this.axisLabelEvents}
/>
);
}
Expand Down Expand Up @@ -687,6 +725,7 @@ export default class BoxScatterPlot<
axisLabelComponent={
<VictoryLabel dy={this.yAxisLabelVertOffset} />
}
events={this.axisLabelEvents}
/>
);
}
Expand Down Expand Up @@ -800,6 +839,7 @@ export default class BoxScatterPlot<
} else {
box.x = this.categoryCoord(i);
}
box.eventkey = i;
};

return toBoxPlotData(
Expand Down Expand Up @@ -941,13 +981,14 @@ export default class BoxScatterPlot<
}

@autobind
private getBoxPlotTooltipComponent() {
if (
this.container &&
this.boxPlotTooltipModel &&
this.props.boxPlotTooltip
) {
private getTooltipComponent(
tooltipModel: any,
verticalOffsetAdjustment: number
) {
if (this.container && tooltipModel && this.props.boxPlotTooltip) {
const maxWidth = 400;
const eventKey =
tooltipModel.datum.eventKey ?? tooltipModel.datum.eventkey;
let tooltipPlacement =
this.mousePosition.x > WindowStore.size.width - maxWidth
? 'left'
Expand All @@ -962,7 +1003,9 @@ export default class BoxScatterPlot<
? -HORIZONTAL_OFFSET
: HORIZONTAL_OFFSET)
}
positionTop={this.mousePosition.y - VERTICAL_OFFSET}
positionTop={
this.mousePosition.y + verticalOffsetAdjustment
}
style={{
transform:
tooltipPlacement === 'left'
Expand All @@ -974,12 +1017,8 @@ export default class BoxScatterPlot<
>
<div>
{this.props.boxPlotTooltip(
[this.boxPlotTooltipModel.datum],
[
this.props.data[
this.boxPlotTooltipModel.datum.eventKey
].label,
]
[tooltipModel.datum],
[this.props.data[eventKey]?.label]
)}
</div>
</Popover>,
Expand All @@ -990,6 +1029,22 @@ export default class BoxScatterPlot<
}
}

@autobind
private getBoxPlotTooltipComponent() {
return this.getTooltipComponent(
this.boxPlotTooltipModel,
-VERTICAL_OFFSET
);
}

@autobind
private getAxisLabelTooltipComponent() {
return this.getTooltipComponent(
this.axisLabelTooltipModel,
VERTICAL_OFFSET
);
}

render() {
if (!this.props.data.length) {
return <div className={'alert alert-info'}>No data to plot.</div>;
Expand All @@ -999,6 +1054,7 @@ export default class BoxScatterPlot<
<Observer>{this.getChart}</Observer>
<Observer>{this.getScatterPlotTooltip}</Observer>
<Observer>{this.getBoxPlotTooltipComponent}</Observer>
<Observer>{this.getAxisLabelTooltipComponent}</Observer>
</div>
);
}
Expand Down
Loading