add legend method described in stackoverflow 14534130