Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline value labels on BarChart. #3826

Closed
IgorShvetsov-Noveo opened this issue Jan 30, 2019 · 4 comments
Closed

Multiline value labels on BarChart. #3826

IgorShvetsov-Noveo opened this issue Jan 30, 2019 · 4 comments

Comments

@IgorShvetsov-Noveo
Copy link

I want to use multiline value labels above chart bars. If I put multiline value (with '\n' inside) value overlaps bar. So I subclassed BarChartRenderer and added offset to the y position of the point.

public class MultilineLabelBarChartRenderer: BarChartRenderer {
    @objc
    open override func drawValue(context: CGContext, value: String, xPos: CGFloat, yPos: CGFloat, font: NSUIFont, align: NSTextAlignment, color: NSUIColor) {        
        ChartUtils.drawText(context: context, text: value, point: CGPoint(x: xPos, y: yPos - 15), align: .center, attributes: [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: color])
    }
}

So now I have 2 lines labels above bars. But they are not center aligned because the point calculates wrong.

Is there's a way to have multiline labels above bars center aligned?

Charts version 3.2.1
Xcode version: 10.0
Swift version: 4.2
ios 11, 12
multiline_chart_labels

@anton-filimonov
Copy link

Try adding text alignment attribute to what you sent as attributes like this:

public class MultilineLabelBarChartRenderer: BarChartRenderer {
    @objc
    open override func drawValue(context: CGContext, value: String, xPos: CGFloat, yPos: CGFloat, font: NSUIFont, align: NSTextAlignment, color: NSUIColor) {      
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center  
        ChartUtils.drawText(context: context, text: value, point: CGPoint(x: xPos, y: yPos - 15), align: .center, attributes: [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: color, NSAttributedString.Key.paragraphStyle: paragraphStyle])
    }
}

@IgorShvetsov-Noveo
Copy link
Author

@anton-filimonov thank you for response. Unfortunately, it didn't help. The same result as on screenshot.

@IgorShvetsov-Noveo
Copy link
Author

It helped me to use drawMultilineText methods from ChartUtils. But they marked with internal. So I had to copy them. I would prefer another way to have multiline value labels.

@liuxuan30
Copy link
Member

you have to override the drawValues method to do it yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants