X轴的垂直线iOS图表

问题描述 投票:3回答:3

也许这是一个简单的问题,但我想知道如何在X轴上绘制垂直线,在iOS图表中绘制X轴上的上标签。 (见图,如红线)

enter image description here

更新:我正在使用的库是这个https://github.com/danielgindi/ios-charts

ios ios-charts
3个回答
2
投票

你可以,但你必须覆盖一些方法,例如在x轴渲染器中查看drawGridLine()drawLabels()

qazxsw poi为您提供了有关如何在ios-charts中绘制网格线的完整详细信息,了解它对编写自己的自定义将非常有帮助。


0
投票

设置要获得红色线条的轴的主要刻度位置和次要刻度位置。请尝试使用以下为图表的x轴编写的示例代码。

drawGridLine

有一个快乐的编码.. !!


0
投票

我设法通过覆盖CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; CPTXYAxis *x = axisSet.xAxis; //Get the axis of the graph // x and y axis line color CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle]; axisLineStyle.lineWidth = 2.0f; axisLineStyle.lineColor = [CPTColor whiteColor]; //Set color whatever you want //Text Style for axis title CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init]; axisTextStyle.color = [CPTColor whiteColor]; //Set color whatever you want // Line color CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle]; tickLineStyle.lineColor = [CPTColor whiteColor]; //Set color whatever you want tickLineStyle.lineWidth = 2.0f; x.title = @"Title for the axis"; x.titleOffset = 50.0f; x.axisLineStyle = axisLineStyle; //axis line style x.titleTextStyle = axisTextStyle; //axis title text style x.majorTickLineStyle = axisLineStyle; //Major axis tick style which you wanted x.minorTickLineStyle = axisLineStyle; //Minor axis tick style x.labelTextStyle = axisTextStyle; //axis label text style x.labelingPolicy = CPTAxisLabelingPolicyNone; //axis labeling policy x.tickDirection = CPTSignNegative; //This will set the red lines below the x-axis as displayed in screenshot you have attached. 来绘制x轴下方的刻度线。此功能在x轴下方绘制标签,因此可以在那里绘制其他内容。例如我们想要的滴答声!

drawLabel

要使用自定义渲染器:

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: self.chartView.viewPortHandler, xAxis: xAxis, transformer: self.chartView.getTransformer(forAxis: .left)) self.chartView.xAxisRenderer = customXAxisRenderer

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.