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

Allow plot tooltip to show extra columns #9800

Merged
merged 11 commits into from
Nov 2, 2024
4 changes: 3 additions & 1 deletion gradio/components/native_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(
x_axis_labels_visible: bool = True,
caption: str | None = None,
sort: Literal["x", "y", "-x", "-y"] | list[str] | None = None,
tooltip: Literal["axis", "none", "all"] = "axis",
height: int | None = None,
label: str | None = None,
show_label: bool | None = None,
Expand Down Expand Up @@ -94,6 +95,7 @@ def __init__(
x_axis_labels_visible: Whether the x-axis labels should be visible. Can be hidden when many x-axis labels are present.
caption: The (optional) caption to display below the plot.
sort: The sorting order of the x values, if x column is type string/category. Can be "x", "y", "-x", "-y", or list of strings that represent the order of the categories.
tooltip: The tooltip to display when hovering on a point. "axis" shows the values for the axis columns, "all" shows all column values, and "none" shows no tooltips.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better api would be to just let it take a list of column names. Often, dataframes will have a large number of irrelevant columns and I'm sure we'll get the request to be able to select specific columns to show in the tooltip

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok added support for passing list of column names

height: The height of the plot in pixels.
label: The (optional) label to display on the top left corner of the plot.
show_label: Whether the label should be displayed.
Expand Down Expand Up @@ -125,6 +127,7 @@ def __init__(
self.x_axis_labels_visible = x_axis_labels_visible
self.caption = caption
self.sort = sort
self.tooltip = tooltip
self.height = height

if label is None and show_label is None:
Expand All @@ -150,7 +153,6 @@ def __init__(
if key in [
"stroke_dash",
"overlay_point",
"tooltip",
"x_label_angle",
"y_label_angle",
"interactive",
Expand Down
92 changes: 58 additions & 34 deletions js/nativeplot/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
export let x_axis_labels_visible = true;
export let caption: string | null = null;
export let sort: "x" | "y" | "-x" | "-y" | string[] | null = null;
export let tooltip: "axis" | "none" | "all" = "axis";
function reformat_sort(
_sort: typeof sort
):
Expand Down Expand Up @@ -114,19 +115,29 @@
function reformat_data(data: PlotData): {
[x: string]: string | number;
}[] {
let x_index = data.columns.indexOf(x);
let y_index = data.columns.indexOf(y);
let color_index = color ? data.columns.indexOf(color) : null;
return data.data.map((row) => {
const obj = {
[x]: row[x_index],
[y]: row[y_index]
};
if (color && color_index !== null) {
obj[color] = row[color_index];
}
return obj;
});
if (tooltip == "all") {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't filter out "irrelevant" columns if tooltip == "all"

return data.data.map((row) => {
const obj: { [x: string]: string | number } = {};
data.columns.forEach((col, i) => {
obj[col] = row[i];
});
return obj;
});
} else {
let x_index = data.columns.indexOf(x);
let y_index = data.columns.indexOf(y);
let color_index = color ? data.columns.indexOf(color) : null;
return data.data.map((row) => {
const obj = {
[x]: row[x_index],
[y]: row[y_index]
};
if (color && color_index !== null) {
obj[color] = row[color_index];
}
return obj;
});
}
}
$: _data = value ? reformat_data(value) : [];

Expand Down Expand Up @@ -411,29 +422,42 @@
type: value.datatypes[color]
}
: undefined,
tooltip: [
{
field: y,
type: value.datatypes[y],
aggregate: aggregating ? _y_aggregate : undefined,
title: y_title || y
},
{
field: x,
type: value.datatypes[x],
title: x_title || x,
format: x_temporal ? "%Y-%m-%d %H:%M:%S" : undefined,
bin: _x_bin ? { step: _x_bin } : undefined
},
...(color
? [
tooltip:
tooltip == "none"
? undefined
: [
{
field: color,
type: value.datatypes[color]
}
field: y,
type: value.datatypes[y],
aggregate: aggregating ? _y_aggregate : undefined,
title: y_title || y
},
{
field: x,
type: value.datatypes[x],
title: x_title || x,
format: x_temporal ? "%Y-%m-%d %H:%M:%S" : undefined,
bin: _x_bin ? { step: _x_bin } : undefined
},
...(color
? [
{
field: color,
type: value.datatypes[color]
}
]
: []),
...(tooltip === "axis"
? []
: value?.columns
.filter(
(col) => col !== x && col !== y && col !== color
)
.map((column) => ({
field: column,
type: value.datatypes[column]
})))
]
: [])
]
},
strokeDash: {},
mark: { clip: true, type: mode === "hover" ? "point" : value.mark },
Expand Down
Loading