开发者

Core-Plot PieChart Slice color

开发者 https://www.devze.com 2023-01-14 00:19 出处:网络
I a开发者_JAVA技巧m using core plot in one of my iPhone projects. Is it possible to change the color for a selected slice in a pie chart (using CPPieChartDataSource, CPPieChartDelegate)?Implement the

I a开发者_JAVA技巧m using core plot in one of my iPhone projects. Is it possible to change the color for a selected slice in a pie chart (using CPPieChartDataSource, CPPieChartDelegate)?


Implement the following method in your pie chart datasource:

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

CPFill can be a color, image, or gradient.


In your .h file

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

in your .m file

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

It will change PieChart Slice color. Thanks


I added this to my .m file (which is the data source file for the pie chart). The colors are ugly -- just used them to test as they are really different than the defaults. And there are only three slices in my chart, hence the hard-coded 3 colors. I found the Core Plot documentation helpful for all this. Here's the link to the fillWithColor method documentation. NOTE: You need to use CPT as a prefix now, not the old 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 if I messed up the answer entry -- this is my first ever post on StackOverflow


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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号