In my iPhone app, I have a view with two separate bar plots. Each plot has a different number of data elements. I would like to label each bar in each plot with the appropriate data, but apparently the "dataLabelForPlot" method only allows one instance of "CPTTextLayer." I would like to have the CPTTextLayer be a function of the plot and its data.
The plots have separate identifiers: "Bar Plot 1" and "Bar Plot 2." I can use the conditional if statement in the "numberOfRecordsForPlot" and "numberForPlot" methods, but it fails in the "dataLabelForPlot" method with errors saying that "label" is an unused variable. Seem very odd to me seeing that it is defined in the conditional statements.
My code looks like this:
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
if (plot.identifier == @"Bar Plot 1") {
return 5; }
else { return 4; }
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSDecimalNumber *num = nil;
if (plot.identifier == @"Bar Plot 1") {
switch ( fieldEnum ) {
case CPTBarPlotFieldBarLocation:
num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
break;
case CPTBarPlotFieldBarTip:
num = (NSDecimalNumber *)[dataTemp1 objectAtIndex:index];
break;
}
}
else {
switch ( fieldEnum ) {
case CPTBarPlotFieldBarLocation:
num = (NSDecimalNumber *)[NSDecimalNumber numberWithUnsignedInteger:index];
break;
case CPTBarPlotFieldBarTip:
num = (NSDecimalNumber *)[dataTemp2 objectAtIndex:index];
break;
}
}
return num;
}
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index {
if (plot.identifier == @"Bar Plot 1") {
CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@", [dataTemp1 objectAtIndex:index]]];
}
else { CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@", [dataTemp2 objectAtIndex:index]]]; }
CPTMutableTextStyle *textStyle = [label.textStyle mutableCopy];
textStyle.color = [CPTColor redColor];
label.textStyle = textStyle;
[textStyle release];
return [label autorelease];
}