Gyanendra
Gyanendra

Reputation: 361

Label of Bars are not showing on top of bar using core plot

I am able to draw a stacked bar graph with the help of core plot. My problem is i am not able to place the label on top of stacked bar graph. To create bar i have done following things

enter image description here

for (NSString *set in [[barChartidentifiers allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]) {
    CPTBarPlot *plot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor blueColor] horizontalBars:NO];
    plot.lineStyle = barLineStyle;
    CGColorRef color = ((UIColor *)[barChartidentifiers objectForKey:set]).CGColor;
    plot.fill  = [CPTFill fillWithColor:[CPTColor colorWithCGColor:color]];
    if (firstPlot) {
        plot.barBasesVary = NO;
        firstPlot  = NO;
    } else {
        plot.barBasesVary = YES;
    }
    plot.barWidth = CPTDecimalFromFloat(0.5);
    plot.barsAreHorizontal  = NO;
    plot.labelTextStyle = [CPTTextStyle textStyle];
    plot.dataSource = self;
    plot.identifier = set;
    [graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];
}

and below is the code i am writing in

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
    NSNumber *num;
    if (fieldEnum == 0) {
        num = [NSNumber numberWithInt:index];
    }

    else {
        double offset = 0;
        if (((CPTBarPlot *)plot).barBasesVary) {
            for (NSString *set in [[barChartidentifiers allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]) {
                if ([plot.identifier isEqual:set]) {
                    break;
                }
                offset += [[[data objectForKey:[dates objectAtIndex:index]] objectForKey:set] doubleValue];


            }
        }

        //Y Value
        if (fieldEnum == 1) {
            num =[NSNumber numberWithDouble:[[[data objectForKey:[dates objectAtIndex:index]] objectForKey:plot.identifier] doubleValue] + offset];
        }

        //Offset for stacked bar
        else {
            num =[NSNumber numberWithDouble:offset];
        }
    }

    //NSLog(@"%@ - %d - %d - %f", plot.identifier, index, fieldEnum, num);

    return num;
}

Upvotes: 0

Views: 302

Answers (1)

Eric Skroch
Eric Skroch

Reputation: 27381

You might be able to adjust the labelOffset of each plot. Try a negative value to put the label over its plot rather than getting stuck under another plot.

Another thing to try would be to add the plots in the reverse order so lower plots and their labels are on top of the ones above. In your example, the yellow plot would be first, green second, etc.

If all else fails, make your own labels using plot space annotations. This would give more flexibility in positioning the labels, too. For example, you could put the labels next to the bars instead of on top.

Upvotes: 1

Related Questions