X 轴上的垂直线 iOS-图表
Vertical lines at X Axis iOS-charts
也许这是一个简单的问题,但我想知道如何在 iOS-charts 中的 X 轴下和 X 轴上的标签下绘制垂直线。 (看图,像红线一样)
更新:我正在使用的库是这个 https://github.com/danielgindi/ios-charts
你可以,但你必须覆盖一些方法,例如看一下 x 轴渲染器中的 drawGridLine()
和 drawLabels()
。
drawGridLine
为您提供了有关如何在 ios-图表中绘制网格线的完整详细信息,了解它对于编写您自己的自定义项非常有帮助。
为要获得红色线条的轴设置主要刻度位置和次要刻度位置。请尝试使用以下为图表的 x 轴编写的示例代码。
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.
编码愉快..!!
我通过重写 drawLabel
设法在 x 轴下方绘制刻度线。此函数在 x 轴下方绘制标签,因此可以在那里绘制其他内容。例如我们想要的报价!
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
示例图片:请不要介意颜色,但我想您明白我的意思了。 ;)
希望对您有所帮助!
也许这是一个简单的问题,但我想知道如何在 iOS-charts 中的 X 轴下和 X 轴上的标签下绘制垂直线。 (看图,像红线一样)
更新:我正在使用的库是这个 https://github.com/danielgindi/ios-charts
你可以,但你必须覆盖一些方法,例如看一下 x 轴渲染器中的 drawGridLine()
和 drawLabels()
。
drawGridLine
为您提供了有关如何在 ios-图表中绘制网格线的完整详细信息,了解它对于编写您自己的自定义项非常有帮助。
为要获得红色线条的轴设置主要刻度位置和次要刻度位置。请尝试使用以下为图表的 x 轴编写的示例代码。
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.
编码愉快..!!
我通过重写 drawLabel
设法在 x 轴下方绘制刻度线。此函数在 x 轴下方绘制标签,因此可以在那里绘制其他内容。例如我们想要的报价!
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
示例图片:请不要介意颜色,但我想您明白我的意思了。 ;)
希望对您有所帮助!