Irving Gonzalez
Irving Gonzalez

Reputation: 25

Swift - Format ios Charts PieEntry Label to include custom label that shows hours and minutes

I have a pieChart that is set up to track a user's time in 24 hours.

PieChart Screenshot without event

I have programmed it to take a user's custom event and track its length of time spent in hours and minutes.

I have been trying to change the labels 15.50 and 8.50 and customize it to show 15 hours and 30 minutes, and 8 hours and 30 minutes.

I have studied the documentation but I haven't been able to find a way to customize each entry label?

Example of piechart with 1 event

Upvotes: 0

Views: 436

Answers (1)

aiwiguna
aiwiguna

Reputation: 2922

you need to create a custom IValueFormatter

public class CustomPieValueFormatter: NSObject, IValueFormatter {
    public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
        
        let number = modf(value)
        let hour = String(format: "%.0f", number.0)
        let minute = String(format: "%.0f", 60 * number.1)
        return "\(hour) hour \(minute) minute"
    }
}

and set the value formatter to your PieChartData

let data = PieChartData(dataSet: dataSet)
        
data.setValueFormatter(CustomPieValueFormatter())

Upvotes: 2

Related Questions