【问题标题】:How can I get the y-axis to be at a particular x-value in CorePlot?如何让 y 轴处于 CorePlot 中的特定 x 值?
【发布时间】:2012-08-17 19:00:08
【问题描述】:

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

这是我配置绘图的代码:

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

【问题讨论】:

    标签: objective-c core-plot


    【解决方案1】:

    有两种方法可以控制:

    1. 设置orthogonalCoordinateDecimal 属性。例如,

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

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

    【讨论】:

    • @Eric 我尝试了这两种方法,但它似乎对我不起作用。有没有其他方法可以实现
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多