2010-09-02 34 views
10

Tôi đang sử dụng cốt truyện cốt lõi trong một trong các dự án iPhone của mình. Có thể thay đổi màu sắc cho một lát được chọn trong biểu đồ hình tròn (sử dụng CPPieChartDataSource, CPPieChartDelegate) không?Core-Plot PieChart Slice color

Trả lời

18

Thực hiện các phương pháp sau đây trong nguồn dữ liệu biểu đồ pie của bạn:

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

CPFill có thể là một màu sắc, hình ảnh, hoặc gradient.

+0

Tôi muốn thay đổi màu sắc của một lát đặc biệt khi nó đã được chọn. Điều đó có thể không? – random

+0

cách thêm hình ảnh? – ravoorinandan

+5

Tôi đoán bây giờ CPTFill và CPTPieChart của nó thay thế .. – TechnocraT

5

Trong file .h của bạn

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

trong file .m bạn

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

Nó sẽ thay đổi màu sắc PieChart Slice. Cảm ơn

1

Tôi đã thêm tệp này vào tệp .m của tôi (là tệp nguồn dữ liệu cho biểu đồ hình tròn). Các màu sắc xấu xí - chỉ cần sử dụng chúng để kiểm tra vì chúng thực sự khác với các giá trị mặc định. Và chỉ có ba lát trong biểu đồ của tôi, do đó có 3 màu được mã hóa cứng. Tôi thấy tài liệu Core Plot hữu ích cho tất cả điều này. Here's the link to the fillWithColor method documentation. LƯU Ý: Bạn cần sử dụng CPT làm tiền tố ngay bây giờ, không phải CP cũ.

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

Xin lỗi nếu tôi sai lầm mục câu trả lời - đây là đầu tiên của tôi bao giờ đăng bài trên StackOverflow

0

phiên bản Swift:

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

}