如何在CorePlot中将y轴设置为特定的x值? [英] How can I get the y-axis to be at a particular x-value in CorePlot?

查看:72
本文介绍了如何在CorePlot中将y轴设置为特定的x值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下条形图.我希望y轴出现在x = 2005(x范围2006-2012)上,但是似乎只想在将x轴范围更改为0时显示:

I have the following bar graph. I want the y-axis to be present at x = 2005 (x range 2006-2012), however it seems to only want to show up when I change the x-axis range to 0-whatever:

这是我在其中配置绘图的代码:

Here is my code where I configure the plot:

//Create graph and set it as host view's graph
self.graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
[self.hostView setHostedGraph:self.graph];

//set graph padding and theme
self.graph.plotAreaFrame.paddingTop = 10.0f;
self.graph.plotAreaFrame.paddingRight = 10.0f;
self.graph.plotAreaFrame.paddingBottom = 60.0f;
self.graph.plotAreaFrame.paddingLeft = 60.0f;
[self.graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];

//set axes ranges
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:
                    CPTDecimalFromFloat(2005) //min year minus 1
                                                length:CPTDecimalFromFloat((8))]; //year difference plus 2

plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:
                    CPTDecimalFromFloat(0)
                                                length:CPTDecimalFromFloat((700))]; // round up to next 50

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
//set axes' title, labels and their text styles
CPTMutableTextStyle *axisStyle = [CPTMutableTextStyle textStyle];
axisStyle.fontName = @"ArialRoundedMTBold";
axisStyle.fontSize = 20;
axisStyle.color = [CPTColor whiteColor];

//label text style
CPTMutableTextStyle *labelStyle = [CPTMutableTextStyle textStyle];
labelStyle.fontName = @"ArialRoundedMTBold";
labelStyle.fontSize = 16;
labelStyle.color = [CPTColor whiteColor];

axisSet.xAxis.title = @"Year";
axisSet.yAxis.title = @"Distance (mi)";
axisSet.xAxis.titleTextStyle = axisStyle;
axisSet.yAxis.titleTextStyle = axisStyle;
axisSet.xAxis.titleOffset = 30.0f;
axisSet.yAxis.titleOffset = 30.0f;
axisSet.xAxis.labelTextStyle = labelStyle;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.yAxis.labelTextStyle = labelStyle;
axisSet.yAxis.labelOffset = 3.0f;
//set axes' line styles and interval ticks
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor whiteColor];
lineStyle.lineWidth = 3.0f;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(100.0f);
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(50.0f);
axisSet.xAxis.majorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 5.0f;

// Create bar plot and add it to the graph
CPTBarPlot *plot = [[CPTBarPlot alloc] init] ;
plot.dataSource = self;
plot.delegate = self;
plot.barWidth = [[NSDecimalNumber decimalNumberWithString:@"0.75"]
                 decimalValue];
plot.barOffset = [[NSDecimalNumber decimalNumberWithString:@"0.0"]
                  decimalValue];
plot.barCornerRadius = 5.0;
// Remove bar outlines
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor clearColor];
plot.lineStyle = borderLineStyle;
// Identifiers are handy if you want multiple plots in one graph
plot.identifier = @"yearlymiles";
[self.graph addPlot:plot];

推荐答案

有两种控制方法:

  1. 设置orthogonalCoordinateDecimal属性.例如,

axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(2005.0);

  • 使用约束.例如,

  • Use constraints. For example,

    axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
    

  • 这篇关于如何在CorePlot中将y轴设置为特定的x值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆