Search code examples
iosios-charts

How to create a custom ios-charts highlight marker with xib?


So, I'm trying to create a custom marker from a xib file. I'm looking to the RadarMarkerView sample, it is basically the same thing. The problem is that the program compiles but the marker does not show up.

This is my custom marker class

open class DailyMarker: MarkerView {

        @IBOutlet weak var powerLabel: UILabel!
        @IBOutlet weak var hourLabel: UILabel!

        override open func awakeFromNib() {
            self.offset.x = -self.frame.size.width / 2.0
            self.offset.y = -self.frame.size.height - 7.0
        }
        open override func refreshContent(entry: ChartDataEntry, highlight: Highlight) {
            powerLabel?.text = String.init(format: "%.2f kWh", entry.y)
            hourLabel?.text = "Hora"
            layoutIfNeeded()
        }
    }

I have created a xib with the same name "DailyMarker.xib" and in the Custom Class field I set to DailyMarker.

Here is the Chart code where I set the marker

let dailyMarker: DailyMarker = DailyMarker()
dailyMarker.chartView = lineChartView
lineChartView.marker = dailyMarker

I just want to create a rounded marker with borders. Could anyone help me ?


Solution

  • So, I Got it to work!

    I think it was missing the .viewFromXib in the file that I was instantiating the graph. That's my updated code

    let dailyMarker: DailyMarker = (DailyMarker.viewFromXib() as? DailyMarker)!
    dailyMarker.chartView = lineChartView
    lineChartView.marker = dailyMarker
    

    Remembering that on the xib file inspector you put the class of the view the same of the swift file. In this case it was DailyMarker. There is no need to set the Placeholder owner.