Search code examples
ios-charts

How to set valueFormatter for Markers in iOS-charts


For the y-axis we can do something like this:

chartView.leftAxis.valueFormatter = SomeCustomTimeFormatter()

But how do we change the format for markers (the popups that appear when you tap a data point)? I am using BalloonMarker from the sample code.


Solution

  • if you use BalloonMarker from the sample you can update the code in this line

    open override func refreshContent(entry: ChartDataEntry, highlight: Highlight)
    {
        //setLabel(String(entry.y)) //change label value in this line to use your desired format
       setLabel(String("\(Int(entry.y)/60):\(Int(entry.y)%60)"
    }
    
    open func setLabel(_ newLabel: String)
    {
            label = newLabel 
            //.......
    }
    

    and set the marker for your chartview

    let marker:BalloonMarker = BalloonMarker(color: UIColor(red: 93/255, green: 186/255, blue: 215/255, alpha: 1), font: UIFont(name: "Helvetica", size: 12)!, textColor: UIColor.white, insets: UIEdgeInsets(top: 7.0, left: 7.0, bottom: 25.0, right: 7.0))
     marker.minimumSize = CGSize(width: 75.0, height: 35.0)
    charts.marker = marker