问题

我正在用Core Plot绘制条形图。我将numberForPlot作为数据源的实现似乎正确返回了值。但是,根据barPlot.Offset的设置,图表以错误的值开头。

数据源实施

我实现了数据源,如下所示:

- (NSArray *)numbersForPlot:(CPTPlot *)plot
                 field:(NSUInteger)fieldEnum
      recordIndexRange:(NSRange)indexRange;

if(fieldEnum == 3) {

NSString *coa = [[NSString alloc] init];
coa = (NSString*)[plot identifier];

NSMutableArray *values = [[NSMutableArray alloc] init] ;
for (IBFinPeriod *fp in periodsArray) {
    NSNumber *value = [self.company valueForCoa:coa FiscalYear:[fp FiscalYear] FiscalPeriod:[fp FiscalPeriod]];
    [values addObject:value];
    }
NSLog(@"plot values: %@",values);
return values;

}
else

    return    [[NSArray alloc]initWithObjects:[NSDecimalNumber numberWithFloat:1.0],
               [NSDecimalNumber numberWithFloat:2.0],
               [NSDecimalNumber numberWithFloat:3.0],
               [NSDecimalNumber numberWithFloat:4.0],
               [NSDecimalNumber numberWithFloat:5.0],
               nil];
}


此代码返回y值:“ 1841.059”,
    “ 2492.619”,
    “ 3011.582”,
    “ 2643.865”,
    “ 2914.845”,
    “ 3470.457”,这是正确的。

小节偏移

但是,仅当barPlot.barOffset = CPTDecimalFromFloat(-0.50f);时,绘图才能从左侧的第一个值正确开始。



如果barPlot.barOffset
设置为0,它从最后一个值(3470.57)的左半部分开始:



图表设置

这是设置图表的代码:

- (void) setChart;


{
    graph1 = [[CPTXYGraph alloc] initWithFrame:self.view.bounds];

CPTTheme *theme = [CPTTheme themeNamed:kCPTPlainWhiteTheme];
[graph1 applyTheme:theme];
self.chartLeft.hostedGraph = graph1;
graph1.plotAreaFrame.masksToBorder = NO;


// Border
graph1.plotAreaFrame.borderLineStyle = nil;
graph1.plotAreaFrame.cornerRadius = 5.0f;

// Paddings
graph1.paddingLeft  = 10.0;
graph1.paddingTop   = 10.0;
graph1.paddingRight = 10.0;
graph1.paddingBottom = 10.0;

graph1.plotAreaFrame.paddingLeft = 50.0;
graph1.plotAreaFrame.paddingTop = 20.0;
graph1.plotAreaFrame.paddingRight = 20.0;
graph1.plotAreaFrame.paddingBottom = 50.0;
graph1.plotAreaFrame.backgroundColor = nil;

graph1.paddingLeft = 20.0;
graph1.paddingTop = 20.0;
graph1.paddingRight = 20.0;
graph1.paddingBottom = 20.0;

// Setup plot space
// The plot space determines the data ranges that are visible in the graph, not its physical size in the view
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph1.defaultPlotSpace;
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(4000.0f)];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(5.0f)];

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph1.axisSet;
CPTXYAxis *x          = axisSet.xAxis;
x.axisLineStyle               = nil;
x.majorTickLineStyle          = nil;
x.minorTickLineStyle          = nil;
x.majorIntervalLength         = CPTDecimalFromString(@"1");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
x.title                       = @"X Axis";
x.titleLocation               = CPTDecimalFromFloat(7.5f);
x.titleOffset                 = 55.0f;

// Custom labels omitted...

CPTXYAxis *y = axisSet.yAxis;
y.axisLineStyle               = nil;
y.majorTickLineStyle          = nil;
y.minorTickLineStyle          = nil;
y.majorIntervalLength         = CPTDecimalFromString(@"1000");
y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
y.title                       = @"Y Axis";
y.titleOffset                 = 45.0f;
y.titleLocation               = CPTDecimalFromFloat(150.0f);

// First bar plot
CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor darkGrayColor] horizontalBars:NO];
barPlot.baseValue  = CPTDecimalFromString(@"0");
barPlot.barOffset  = CPTDecimalFromFloat(-0.50f);
barPlot.identifier = @"SOPI";
barPlot.dataSource = self;
[graph1 addPlot:barPlot toPlotSpace:plotSpace];


}



我的实现有什么问题?

最佳答案

我找到了解决问题的方法:

我的x值返回语句缺少numberWithFloat 6.0

return    [[NSArray alloc]initWithObjects:[NSDecimalNumber numberWithFloat:1.0],
           [NSDecimalNumber numberWithFloat:2.0],
           [NSDecimalNumber numberWithFloat:3.0],
           [NSDecimalNumber numberWithFloat:4.0],
           [NSDecimalNumber numberWithFloat:5.0],
           nil];


此外,plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(5.0f)]中的长度仅为5。但是,我有6个x值。我现在将长度设置为6.5。但是,将其设置为6.0时,将显示右栏

关于objective-c - 核心图1.0:Barchart从错误的bar开始(取决于Barplot.Offset),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9547258/

10-10 06:14