2

I created a bubble chart with iOS Charts that has three different colors. Now I want to add a legend for every color. The little colored squares for the legend already show up but the text doesn't. That is how I tried to add the legend text, is there anything wrong with that or is something missing?

let firstLegend = LegendEntry.init(label: "Below 50", form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: CGFloat.nan, formLineDashLengths: nil, formColor: UIColor.black)
let secondLegend = LegendEntry.init(label: "Between 50 and 75", form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: CGFloat.nan, formLineDashLengths: nil, formColor: UIColor.black)
let thirdLegend = LegendEntry.init(label: "Over 75", form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: CGFloat.nan, formLineDashLengths: nil, formColor: UIColor.black)

bubbleView.legend.entries = [firstLegend, secondLegend, thirdLegend] 
OSX55
  • 160
  • 2
  • 10
  • Hi am using above code to display the legend entries,but texts are overlapping with each other?is there any solution? – Shilpashree MC Dec 14 '17 at 12:43
  • @ShilpashreeMC I am also facing same overlapping issue, were you able to solve it? – Molly Jul 16 '19 at 08:00
  • I had to use `bubbleView.setCustom(entries: [firstLegend, secondLegend, thirdLegend])` to get custom legend to appear. This doesn't fix the overlap issue. – P. Stern Aug 05 '22 at 16:51

3 Answers3

1

Set this in the code, it will not accept custom legend otherwise

isLegendCustom = true

Abhijit_R
  • 13
  • 8
0

You are doing nothing wrong. The only reason is that one Legend object only stores one legend label color.

open var textColor = NSUIColor.black

A LegendEntry object doesn't have a label color property. So, you can't set labels in one legend to different colors unless you customize a sub class of LegendRenderer.swift.

Xcoder
  • 1,433
  • 3
  • 17
  • 37
Y.Bi
  • 657
  • 6
  • 11
0

Change your code like this, it'll work.

let firstLegend = LegendEntry.init(label: "Below 50", form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: CGFloat.nan, formLineDashLengths: nil, formColor: UIColor.black)
let secondLegend = LegendEntry.init(label: "Between 50 and 75", form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: CGFloat.nan, formLineDashLengths: nil, formColor: UIColor.black)
let thirdLegend = LegendEntry.init(label: "Over 75", form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: CGFloat.nan, formLineDashLengths: nil, formColor: UIColor.black)


chartView.legend.setCustom(entries: [firstLegend, secondLegend, thirdLegend])
Balaji G
  • 77
  • 5