0
votes

I am trying to add legend in my bar chart based on the examples like this

 CPTBarPlot* barPlot = [[CPTBarPlot alloc] init];
    barPlot.dataSource = self;
    barPlot.baseValue = CPTDecimalFromString(@"0");
    barPlot.barOffset = CPTDecimalFromFloat(1.0f);
    barPlot.barCornerRadius = 2.0f;
    barPlot.identifier = @"Bar Plot 2";
    [barChart addPlot:barPlot toPlotSpace:plotSpace];


    // Add legend
    CPTLegend *theLegend = [CPTLegend legendWithGraph:barChart];
    theLegend.numberOfColumns = 1;
    theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
    theLegend.borderLineStyle = [CPTLineStyle lineStyle];
    theLegend.cornerRadius = 5.0;
    CPTMutableTextStyle* legendStyle = [[CPTMutableTextStyle alloc] init];
    legendStyle.fontSize = 18.0f;

    theLegend.textStyle = legendStyle;

barChart.legend = theLegend;

barChart.legendAnchor = CPTRectAnchorRight;
barChart.legendDisplacement = CGPointMake(-50.0, 80.0);

and this is the method to set titles in the legend

 //legend title
    -(NSString *)legendTitleForBarPlot:(CPTBarPlot *)barPlot recordIndex:(NSUInteger)index
    {
        if (index == 0) {

        return @"Major";
    }

        if (index == 1) {
        return @"Minor" ;
        }
    if (index == 2) {
        return @"Normal";   }

    return [NSString stringWithFormat:@"Bar  %u", index];
}

But i can only see the one line in the rectangular box with title 'Bar Plot 2' and one black rectangular box before title. the one set in barPlot.identifier.But the desired output is to display three lines with Major,minor,normal as titles.. I feel like the method legendTitleForBarPlot is not being called, as the result is same after commenting this method as well.

This the sceen capture of the output

2
What version of Core Plot are you using? The -legendTitleForBarPlot:recordIndex: method was added between 0.9 and 1.0. Also, make sure that method is implemented in your datasource class. - Eric Skroch
Iam using coreplot 0.9. Also i'm implementing the method in the same class where numberOfRecordsForPlot: numberForPlot: dataLabelForPlot: etc are implemented. Also i am adding legend title for pie chart and it works correctly but not in bar plot. Thanks - TechnocraT
@TechnocraT How did you write the tilted text at the bottom of the bars? I am looking for that since 8 hrs. Please let m know. - Developer

2 Answers

1
votes

Try changing values with

 barChart.legendDisplacement = CGPointMake(-50.0, 80.0);

Since your x displacement is too big and I dont know the range of your graph. Also legends will be seen at all bars. So the number of records are ur legend numbers.

I haven't added legends any time but can give an advice for that.

0
votes

I apologise for necro'ing another fairly old question, and I appreciate it may well be too late to help TechnocraT.

However, I was having exactly the same problem whereby Legend titles just weren't showing up correctly for my bar chart. The initial Legend swatch would appear, with my selection of background colour, etc, but nothing to actually say what the graph lines stood for.

@Eric Skroch was entirely right (unsurprisingly) in his comment of checking that the method was implemented in the datasource class. Or rather, making sure that the plot's delegate was set correctly.

In my case I had been following the typically excellent Core Plot tutorial from Ray Wenderlich's site and this splits up the Plot, Graph and Axes configuration into separate methods. When I came to configuring my Legends, I simply misplaced the relevant code and put it before setting the graph delegate to my class (self, in my case).

In short, once I placed the Legend configuration code after the graph.delegate=self; statement, all was good.

Animal451

It's not a good answer, but it is an answer.