lineChartView轴标签在标签上添加额外的行

时间:2020-08-14 02:15:30

标签: ios swift charts swift4

嗨,我想在每个这样的标签上方添加一行

enter image description here

我尝试过

class XAxisRendererWithTicks: XAxisRenderer {

override func drawLabel(context: CGContext, formattedLabel: String, x: CGFloat, y: CGFloat, attributes: [NSAttributedString.Key: Any], constrainedToSize: CGSize, anchor: CGPoint, angleRadians: CGFloat) {

super.drawLabel(context: context, formattedLabel: formattedLabel, x: x, y: y, attributes: attributes, constrainedToSize: constrainedToSize, anchor: anchor, angleRadians: angleRadians)

context.beginPath()

context.move(to: CGPoint(x: x, y: y))
context.addLine(to: CGPoint(x: x, y: self.viewPortHandler.contentBottom))

context.strokePath()
}}

let customXAxisRenderer = XAxisRendererWithTicks(viewPortHandler: lineChartView.viewPortHandler, xAxis: lineChartView.xAxis, transformer: lineChartView.getTransformer(forAxis: .left))
    lineChartView.xAxisRenderer = customXAxisRenderer
    

这可以使短线,但是如何删除没有标签的短线呢?

enter image description here

谢谢

1 个答案:

答案 0 :(得分:0)

我以此解决了我的问题

override func drawLabel(context: CGContext, formattedLabel: String, x: CGFloat, y: CGFloat, attributes: [NSAttributedString.Key: Any], constrainedToSize: CGSize, anchor: CGPoint, angleRadians: CGFloat) {

    super.drawLabel(context: context, formattedLabel: formattedLabel, x: x, y: y, attributes: attributes, constrainedToSize: constrainedToSize, anchor: anchor, angleRadians: angleRadians)

    if(formattedLabel == ""){
        return
    }
    context.beginPath()

    context.move(to: CGPoint(x: x, y: y))
    context.addLine(to: CGPoint(x: x, y: self.viewPortHandler.contentBottom))

    context.strokePath()
}