Unless I am missing something here, unfortunately what you are after currently isn't possible (stored procedure or otherwise). You can only turn the filter legend on or off.
This has been raised as an enhancement in the past (task ID# 228748) and is currently considered 'planned'. This means we are looking to implement this functionality. I have gone ahead and annotated your interest in this functionality as well as raised the priority of the task.
Anyway, let me know if you have any questions on any of this. If I don't hear back, have a great weekend!