Search code examples
swiftios-charts

How to change CenterText Font in PieChart in ios-charts?


I would like to change the Center Text Font and Font Size of a PieChart. I would like to make it as big as it's possible inside the center circle. Is there any on-board feature that helps me to change the font and font size or do i have to overwrite some Framework classes?

Didn't find any useful information in the documentation :(

That's my basic Chart Formating method:

    func setAttributes(view: PieChartView){
    view.legend.enabled = true
    view.descriptionText = ""
    view.userInteractionEnabled = false
    view.drawSliceTextEnabled = false
    view.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: ChartEasingOption.EaseInOutBack)
}

Regards!


Solution

  • The centered text in pie chart is called centerAttributedText, in PieChartView. It's NSAttributedText, so you can define many custom attributes.

    You can simply change its font and size like below:

    centerText = @"Whatever you like";
    
    [centerText setAttributes:@{
                                NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:12.f],
                                NSParagraphStyleAttributeName: paragraphStyle
                                } range:NSMakeRange(0, centerText.length)];
    
    pieChartView.centerAttributedText = centerText;
    

    It's in Objective-C, but should be easy for you to translate into swift, since you only need to care about the attributes