Fix double layer space in GetLegendGraphic and remove code redundancy