Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
956 views
in Technique[技术] by (71.8m points)

iphone - Problems with legend in Core-Plot (pieChart)

I have a problem drawing the legend of a pieChart with Core-Plot, because the name of each element of the chart in the legend is always the identifier of the CPTPieChart. Can someone help me? Thanks.

This is the source code:

-(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);
}

So in the legend I have always "Pie Chart 2".

PS: Sorry because of my poor english.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to add this method to your datasource:

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

It will be called for each index (corresponding to each pie slice). Simply return the correct title string for each one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...