1

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

具有有效 x 范围的条形图没有 y 轴 x 范围错误的条形图有一个 y 轴

这是我配置绘图的代码:

//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];
4

1 回答 1

2

有两种方法可以控制它:

  1. 设置orthogonalCoordinateDecimal属性。例如,

    axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromDouble(2005.0);
    
  2. 使用约束。例如,

    axisSet.yAxis.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
    
于 2012-08-18T00:09:51.577 回答