alter legend function refactor