2010-09-02 21 views
10

Ich verwende Core Plot in einem meiner iPhone-Projekte. Ist es möglich, die Farbe für ein ausgewähltes Segment in einem Tortendiagramm zu ändern (mit CPPieChartDataSource, CPPieChartDelegate)?Core-Plot PieChart Slice Farbe

Antwort

18

Implementieren Sie die folgende Methode in Ihrer Kreisdiagramm Datenquelle:

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

CPFill kann eine Farbe, ein Bild oder Steigung sein.

+0

Ich möchte die Farbe eines bestimmten Scheibe ändern, wenn es ist ausgewählt. Ist das möglich? – random

+0

Wie füge ich ein Bild hinzu? – ravoorinandan

+5

Ich denke, es ist jetzt CPTFill und CPTPieChart statt .. – TechnocraT

5

In Ihrer .h-Datei

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

in .m Datei

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

Es wird PieChart Scheibe Farbe ändern. Danke

1

Ich fügte dieses meiner .m Datei hinzu (die die Datenquellendatei für das Kreisdiagramm ist). Die Farben sind hässlich - sie wurden nur zum Testen verwendet, da sie sich von den Standardeinstellungen unterscheiden. Und es gibt nur drei Scheiben in meiner Tabelle, daher die fest codierten 3 Farben. Ich fand die Core Plot Dokumentation hilfreich für all dies. Here's the link to the fillWithColor method documentation. HINWEIS: Sie müssen jetzt CPT als Präfix verwenden, nicht den alten 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; 
} 

Sorry, wenn ich die Antwort Eintrag vermasselt - dies ist meine erste jemals auf Stackoverflow Post

0

Swift Version:

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

}