refactor when legend is drawn