Best way to average data based on labels selected in graphs legend

Hey all. I’m hoping I word this correctly and use the correct terminology, so please bare with me!

I’m working out the best way to calculate averages only based on the fields/labels that have been selected in the graphs legend.
So far, I’m using the following query avg(avg_over_time(thread_frametime{thread=~"main", sessionId=~"$sessionId"}[48h]))
The query works and returns the data on my time series graph for each main thread found across the different services.

In my visuals field/legend, I’m removing the sessions that basically show no information due to no activity or a shutdown/quit after a short period of time. The query above includes all threads which is is expected, but I need to filter by sessionId with the sessions that have the most activity based on the legend options that are showing on the visual. If there’s only a few sessions, I can enter the sessionIds manually, but if I am to have 20+ session ids to enter, it’ll become quite tedious.

Does anyone have any advice on how I could achieve this?
Once again, sorry if it’s worded badly…