2010-09-02 75 views
10

我在我的一個iPhone項目中使用了核心圖。是否可以更改餅圖中選定切片的顏色(使用CPPieChartDataSource,CPPieChartDelegate)?Core-Plot PieChart Slice color

回答

18

在餅圖數據源實現以下方法:

-(CPTFill *)sliceFillForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index; 

CPFill可以是顏色,圖像,或梯度。

+0

我想,當它改變特定切片的顏色被選中。那可能嗎? – random 2010-09-03 04:06:04

+0

如何添加圖像? – ravoorinandan 2011-04-28 15:56:15

+5

我猜它現在是CPTFill和CPTPieChart .. – TechnocraT 2012-03-14 13:03:56

5

在您的.h文件中

#import "CPTPieChart.h" 
@interface YourViewController : UIViewController<CPTPlotDataSource,CPTPieChartDataSource, CPTPieChartDelegate> 
{ 
} 

在.m文件

-(CPTFill *)sliceFillForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index 
{  
    CPTFill *areaGradientFill ; 

    if (index==0) 
     return areaGradientFill= [CPTFill fillWithColor:[CPTColor orangeColor]]; 
    else if (index==1) 
     return areaGradientFill= [CPTFill fillWithColor:[CPTColor greenColor]]; 
    else if (index==2) 
     return areaGradientFill= [CPTFill fillWithColor:[CPTColor yellowColor]]; 

    return areaGradientFill; 
} 

它將改變餅圖切片的顏色。謝謝

1

我將此添加到我的.m文件(這是餅圖的數據源文件)。顏色很醜 - 只是用它們來測試,因爲它們與默認設置完全不同。我的圖表中只有三個切片,因此是硬編碼的三種顏色。我發現Core Plot文檔對所有這些都有幫助。 Here's the link to the fillWithColor method documentation。注意:您現在需要使用CPT作爲前綴,而不是舊的CP。

-(CPTFill *)sliceFillForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index; 
    { 
    CPTFill *color; 

    if (index == 0) {  
     color = [CPTFill fillWithColor:[CPTColor purpleColor]]; 

    } else if (index == 1) { 
     color = [CPTFill fillWithColor:[CPTColor blueColor]]; 


    } else { 
     color = [CPTFill fillWithColor:[CPTColor blackColor]]; 
      } 

     return color; 
} 

很抱歉,如果我搞砸了答案項 - 這是我第一次張貼在計算器上

0

斯威夫特版本:

func sliceFillForPieChart (pieChart: CPTPieChart, recordIndex: UInt) -> CPTFill { 
    switch (recordIndex+1) { 
    case 1: 
     return CPTFill(color:CPTColor.greenColor()); 
    case 2: 

     return CPTFill(color:CPTColor.redColor()); 
    default: 
     return CPTFill(color:CPTColor.orangeColor()); 
    } 

}