Проблемы с легендой в Core-Plot (pieChart)

У меня проблема с рисованием легенды круговой диаграммы с помощью Core-Plot, потому что имя каждого элемента диаграммы в легенде всегда является идентификатором CPTPieChart. Кто-нибудь может мне помочь? Спасибо.

Это исходный код:

-(void)constructPieChart {
    // Create pieChart from theme
    //[pieGraph applyTheme:theme];
    pieChartView.hostedGraph = pieGraph;
    pieGraph.plotAreaFrame.masksToBorder = YES;

    pieGraph.paddingLeft = 0;
    pieGraph.paddingTop = 20.0;
    pieGraph.paddingRight = 0;
    pieGraph.paddingBottom = 60.0;

    pieGraph.axisSet = nil;

    // Prepare a radial overlay gradient for shading/gloss
    CPTGradient *overlayGradient = [[[CPTGradient alloc] init] autorelease];
    overlayGradient.gradientType = CPTGradientTypeRadial;
    overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.0] atPosition:0.0];
    overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.3] atPosition:0.9];
    overlayGradient = [overlayGradient addColorStop:[[CPTColor blackColor] colorWithAlphaComponent:0.7] atPosition:1.0];

    // Add pie chart
    piePlot = [[CPTPieChart alloc] init];
    piePlot.dataSource = self;
    piePlot.delegate = self;
    piePlot.pieRadius = 80.0;
    piePlot.identifier = @"Pie Chart 2";
    piePlot.startAngle = M_PI_2;
    piePlot.sliceDirection = CPTPieDirectionClockwise;
    piePlot.borderLineStyle = [CPTLineStyle lineStyle];
    //piePlot.sliceLabelOffset = 5.0;
    piePlot.overlayFill = [CPTFill fillWithGradient:overlayGradient];

    [pieGraph addPlot:piePlot];

    pieGraph.title=@"GRAFICA SECTORES";

    [piePlot release];

    // Add some initial data
    NSMutableArray *contentArray = [NSMutableArray arrayWithObjects:
                                    [NSNumber numberWithDouble:20.0], 
                                    [NSNumber numberWithDouble:40.0],
                                    [NSNumber numberWithDouble:30.0], 
                                    [NSNumber numberWithDouble:23], 
                                    [NSNumber numberWithDouble:60.0], 
                                    nil];
    self.dataForChart = contentArray;


    // Add legend
    CPTLegend *theLegend = [CPTLegend legendWithGraph:pieGraph];
    theLegend.numberOfColumns = 2;
    theLegend.fill = [CPTFill fillWithColor:[CPTColor whiteColor]];
    theLegend.borderLineStyle = [CPTLineStyle lineStyle];
    theLegend.cornerRadius = 5.0;

    pieGraph.legend = theLegend;

    pieGraph.legendAnchor = CPTRectAnchorBottom;
    pieGraph.legendDisplacement = CGPointMake(0.0, 30.0);
}

Так что в легенде у меня всегда "Круговая диаграмма 2".

PS: извините из-за моего плохого английского.


person sharnak82    schedule 31.08.2011    source источник


Ответы (1)


Вам нужно добавить этот метод в ваш источник данных:

-(NSString *)legendTitleForPieChart:(CPTPieChart *)pieChart
                        recordIndex:(NSUInteger)index;

Он будет вызываться для каждого индекса (соответствующего каждому сектору). Просто верните правильную строку заголовка для каждого из них.

person Eric Skroch    schedule 31.08.2011
comment
@ mm24 Этому вопросу четыре года. Если у вас возникли проблемы, задайте новый вопрос с подробностями вашей ситуации. - person Eric Skroch; 26.08.2015