Search code examples
iosswiftchartsios-charts

Formatting Custom Legend Entries in Chart's PieChartView?


I'm having difficulty formatting my custom legend entries for my pieChartView. I need the legend entries to be different than those on the pie chart (why I need to customize). The below works but anything larger than size 6 text (which is too small) results in overlapping. How can I resize the legend to fit small text?

(Screenshot is with size 10 text)enter image description here

let l = self.pieChartView.legend

let formSize =  CGFloat.nan

let legendEntry1 = LegendEntry(label: zonesArrayOfString[0], form: .default, formSize: formSize, formLineWidth: .nan, formLineDashPhase: .nan, formLineDashLengths: .none, formColor: zone1Color)  //set formSize, formLizeWidth, and formLineDashLengths to .nan to use default
let legendEntry2 = LegendEntry(label: zonesArrayOfString[1], form: .default, formSize: formSize, formLineWidth: .nan, formLineDashPhase: .nan, formLineDashLengths: .none, formColor: zone2Color)
let legendEntry3 = LegendEntry(label: zonesArrayOfString[2], form: .default, formSize: formSize, formLineWidth: .nan, formLineDashPhase: .nan, formLineDashLengths: .none, formColor: zone3Color)
let legendEntry4 = LegendEntry(label: zonesArrayOfString[3], form: .default, formSize: formSize, formLineWidth: .nan, formLineDashPhase: .nan, formLineDashLengths: .none, formColor: zone4Color)
let legendEntry5 = LegendEntry(label: zonesArrayOfString[4], form: .default, formSize: formSize, formLineWidth: .nan, formLineDashPhase: .nan, formLineDashLengths: .none, formColor: zone5Color)

let customLegendEntries = [legendEntry1, legendEntry2, legendEntry3, legendEntry4, legendEntry5]
l.setCustom(entries: customLegendEntries)
l.orientation = .horizontal
l.textColor = UIColor.white
l.font = myFonts.openSansRegular.of(size: 6)

Solution

  • I was able to get this looking better by using the the legend's xEntrySpace property which lets you customize the space allocated for a given entry inside the legend, though manipulating as well the the font size and form size I was able to get all of the legend entries formatted correctly.