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
TimeSeries/BarChart: Add support for sorting series in the tooltip #43615
Conversation
dfb4b9c
to
a36f956
Compare
* Null values are always sorted to the end regardless of the sort order | ||
*/ | ||
function sortValues(sort: TooltipSortOrder) { | ||
return (a: number | null | undefined, b: number | null | undefined) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return (a: number | null | undefined, b: number | null | undefined) => { | |
return (a: any, b: any) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryantxu this fn is now moved to grafana-data/arrayUtils and now has the support for string values (using localeCompare
)
|
||
if (sort === TooltipSortOrder.Descending) { | ||
return b - a; | ||
} | ||
return a - b; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (sort === TooltipSortOrder.Descending) { | |
return b - a; | |
} | |
return a - b; | |
const order = a > b ? 1: -1; | |
if (sort === TooltipSortOrder.Descending) { | |
return order * -1; | |
} | |
return order; |
Can we implement this so it also works for string inputs? Maybe something like the above (untested!!!) approach. For the time series tooltip it likely makes no difference, but if forced to numbers, then we will for sure need custom implementations for other panels (though we are already working on that anyway)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you'd want to use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
if you want it to be fast, you'd first init & cache an Intl.Collator instance and use that: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare#performance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's done now, thanks for the feedback folks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This worked as expected in time series panel. What was strange that it showed up in barchart but didn't work.
That's tru and it's because of the custom tooltip being used there. But In the latest comit I've added this feature to BarChart too |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great!
@@ -230,6 +235,10 @@ export const TooltipPlugin: React.FC<TooltipPluginProps> = ({ | |||
}); | |||
} | |||
|
|||
if (sortOrder !== SortOrder.None) { | |||
series.sort((a, b) => arrayUtils.sortValues(sortOrder)(a.value, b.value)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this, a.value
andb.value
is formatted display value, whether should use the original value to sort ? I am not clearly on the context, hope someone can explain it if i wrong :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please open a separate issue with your findings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure :)
Closes #38861
This PR brings ability to sort series in the Timeseries panel's tooltip as it was possible with the old graph panel. Additionally, a migration for legacy tooltip options is added.
Note that the
null
/undefined
values are sorted at the end of the tooltip regardless the sort order configured.Screen.Recording.2022-01-03.at.14.10.24.mov