2011-08-31 79 views
2

說明問題我有個問題與繪圖芯劇情一個餅圖的圖例,因爲在圖例中的圖表中的每個元素的名稱是總是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]; 

    [email protected]"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:很抱歉,因爲我的英語不好。

回答

3

您需要將此方法添加到您的數據源:

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

它將被稱爲每個索引(對應於每一圓形切片)。只需爲每個標題返回正確的標題字符串。

+0

謝謝。它工作正常! – sharnak82

+0

如何確保調用此方法?它不會被我呼籲.. – mm24

+0

@ mm24這個問題已經四年了。如果遇到問題,請向您提供有關您的具體情況的新問題。 –