Problem
I am drawing a bar chart with Core Plot. My implementation of the numberForPlot as the data source seems to return the values correctly. However, depending on the setting of barPlot.Offset the chart starts with the wrong value.
Datasource implementation
I have implemented the datasource as follows:
- (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];
}
This code returns the y-values: "1841.059", "2492.619", "3011.582", "2643.865", "2914.845", "3470.457", which is correct.
Barplot offset
However, only when barPlot.barOffset = CPTDecimalFromFloat(-0.50f);
the plot starts correctly with the first value on the left.

If barPlot.barOffset
is set to 0, it starts with the left half of the last value (3470.57):

Chart set-up
This is the code to set-up the chart:
- (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];
}
Question
What is wrong with my implementation?
barPlot.barOffset = CPTDecimalFromFloat(-0.50f)! - Steve HHH