added more code for legend.