Search code examples
iosindexingcore-plotcptbarplot

Bars only appear at first index


The bars on my barplots aren't spread out across the whole graph, rather they are confined to the first index! enter image description here

- (UIView*)pagingScrollView:(BAGPagingScrollView*)pagingScrollView
       viewForPageIndex:(int)index;

{
NSLog(@"THE INDEX COMING THROUGH INTO THE PAGING SCROLL VIEW = %i",index);
      _index = index;

_graphBase = [[CPTGraphHostingView alloc]initWithFrame:_pagingView.frame];
[self updateGraph:_graphBase forIndex:_index];

}

-(void) updateGraph:(CPTGraphHostingView*)graph forIndex:(int) index
{
....
else if (index == 5){

    graph.allowPinchScaling = NO;
    _averageOfPar = [[NSArray alloc]initWithArray:[self getParForPlayerName]];
    _averageDataValuesMultiple = [[NSArray alloc]initWithArray:[self arrayOfAverageDataValuesFromTheUserMultiple]];
    NSLog(@"average data values %@", _averageDataValuesMultiple);
    NSLog(@"average of par = %@",_averageOfPar);
    [self barChartconfigureGraph:graph];
    [self barChartconfigurePlots:graph];
    [self barChartconfigureAxes:graph]; 

}

.... }

 -(void)barChartconfigureGraph:(CPTGraphHostingView*)hostView {

CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:hostView.bounds];
graph.plotAreaFrame.masksToBorder = NO;
hostView.hostedGraph = graph;

// 2 - Configure the graph

[graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];
graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
graph.paddingBottom = 30.0f;
graph.paddingLeft  = 30.0f;
graph.paddingTop    = 0.0f;
graph.paddingRight  = 0.0f;

// 3 - Set up styles

CPTMutableTextStyle *titleStyle = [CPTMutableTextStyle textStyle];
titleStyle.color = [CPTColor whiteColor];
titleStyle.fontName = @"Helvetica-Bold";
titleStyle.fontSize = 16.0f;

// 4 - Set up title

NSString *title = @"Average Shot Compared To Par";
graph.title = title;
graph.titleTextStyle = titleStyle;
graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop;
graph.titleDisplacement = CGPointMake(0.0f, -16.0f);
// 5 - Set up plot space
CGFloat xMin = 0.0f;
CGFloat xMax = (([_averageOfPar count])+0.2);
CGFloat yMin = 0.0f;
CGFloat yMax = ([self largestShotMade]);

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xMin) length:CPTDecimalFromFloat(xMax)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin) length:CPTDecimalFromFloat(yMax)];


  }

-(void)barChartconfigurePlots:(CPTGraphHostingView*)hostView{

// 1 - Set up the three plots

_personsShotPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
_personsShotPlot.identifier = _nameComingThrough;
_parPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor whiteColor] horizontalBars:NO];
_parPlot.identifier = PAR;

// 2 - Set up line style

CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init];
barLineStyle.lineColor = [CPTColor lightGrayColor];
barLineStyle.lineWidth = 0.5;

// 3 - Add plots to graph

CPTGraph *graph = hostView.hostedGraph;
CGFloat barX = CPDBarInitialXSecondForSplotMultiple;
NSArray *plots = [NSArray arrayWithObjects: self.parPlot,self.personsShotPlot, nil];

//NSLog(@"about to enter the plot for loop");

for (CPTBarPlot *plot in plots) {
    plot.dataSource = self;
   plot.delegate = self;
    plot.barWidth = CPTDecimalFromDouble(CPDBarInitialXSecondForSplotMultiple/* + [self widthNumToAdd]*/);
    plot.barOffset = CPTDecimalFromDouble(barX);
    plot.lineStyle = barLineStyle;
    [graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];
    barX += CPDBarInitialXSecondForSplotMultiple/*+ [self widthNumToAdd]*/;
}
//[graph reloadDataIfNeeded];

} -(void)barChartconfigurePlots:(CPTGraphHostingView*)hostView{

// 1 - Set up the three plots

_personsShotPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
_personsShotPlot.identifier = _nameComingThrough;
_parPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor whiteColor] horizontalBars:NO];
_parPlot.identifier = PAR;

// 2 - Set up line style

CPTMutableLineStyle *barLineStyle = [[CPTMutableLineStyle alloc] init];
barLineStyle.lineColor = [CPTColor lightGrayColor];
barLineStyle.lineWidth = 0.5;

// 3 - Add plots to graph

CPTGraph *graph = hostView.hostedGraph;
CGFloat barX = CPDBarInitialXSecondForSplotMultiple;
NSArray *plots = [NSArray arrayWithObjects: self.parPlot,self.personsShotPlot, nil];

//NSLog(@"about to enter the plot for loop");

for (CPTBarPlot *plot in plots) {
    plot.dataSource = self;
   plot.delegate = self;
    plot.barWidth = CPTDecimalFromDouble(CPDBarInitialXSecondForSplotMultiple/* + [self widthNumToAdd]*/);
    plot.barOffset = CPTDecimalFromDouble(barX);
    plot.lineStyle = barLineStyle;
    [graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];
    barX += CPDBarInitialXSecondForSplotMultiple/*+ [self widthNumToAdd]*/;
}
//[graph reloadDataIfNeeded];

}

-(void)barChartconfigureAxes:(CPTGraphHostingView*)hostView {

// 1 - Configure styles

CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 12.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:1];

// 2 - Get the graph's axis set

CPTXYAxisSet *axisSet = (CPTXYAxisSet *) hostView.hostedGraph.axisSet;

// 3 - Configure the x-axis

axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
axisSet.xAxis.title = @"Hole Number";
axisSet.xAxis.titleTextStyle = axisTitleStyle;
axisSet.xAxis.titleOffset = 15.0f;
axisSet.xAxis.axisLineStyle = axisLineStyle;

CPTAxis *x = axisSet.xAxis;

NSMutableSet *xLabels = [NSMutableSet setWithCapacity:[_averageDataValues count]];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:[_averageDataValues count]];
NSInteger i = 0;

for (NSString *holeName in [self holeNames]) {


    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:holeName  textStyle:x.labelTextStyle];
    CGFloat location = i++;
    label.tickLocation = CPTDecimalFromCGFloat(location + .4);
    label.offset =-1;
    if (label) {
        [xLabels addObject:label];

        [xLocations addObject:[NSNumber numberWithFloat:location + .4]];
    }
}

x.axisLabels = xLabels;
x.majorTickLocations = xLocations;



// 4 - Configure the y-axis
axisSet.yAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
axisSet.yAxis.title = @"Number Of Shots";
axisSet.yAxis.titleTextStyle = axisTitleStyle;
axisSet.yAxis.titleOffset = 5.0f;
axisSet.yAxis.axisLineStyle = axisLineStyle;

[hostView.hostedGraph reloadDataIfNeeded];

}

 -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
  else if (_index == 5) return 18;//[_averageDataValuesMultiple count];

}



  -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {

else if (_index == 5) {

    if (fieldEnum == CPTBarPlotFieldBarTip) {
    if ([plot.identifier isEqual:_nameComingThrough]) {

    return [_averageDataValuesMultiple objectAtIndex:index];
        //return [NSNumber numberWithInt:5];

    } else if ([plot.identifier isEqual:PAR]) {

        return [_averageOfPar objectAtIndex:index];
        //return [NSNumber numberWithInt:2];

    }
}
}

I originally didn't want to post the code because it is a bit complicated, I was hoping that it was a bug that had been seen before!


Solution

  • Make sure your datasource provides values for the CPTBarPlotFieldBarLocation field in addition to CPTBarPlotFieldBarTip. It looks like you're returning the same location value for every bar index which puts the bars on top of each other.