Core-Plot 饼图切片颜色

发布于 2024-09-16 22:29:57 字数 92 浏览 0 评论 0原文

我正在我的 iPhone 项目之一中使用核心图。是否可以更改饼图中选定切片的颜色(使用 CPPieChartDataSource、CPPieChartDelegate)?

I am 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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

太阳公公是暖光 2024-09-23 22:29:57

在饼图数据源中实现以下方法:

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

CPFill 可以是颜色、图像或渐变。

Implement the following method in your pie chart datasource:

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

CPFill can be a color, image, or gradient.

逆光下的微笑 2024-09-23 22:29:57

在 .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;
}

中它将更改 PieChart Slice 颜色。谢谢

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

橘和柠 2024-09-23 22:29:57

我将其添加到我的 .m 文件(这是饼图的数据源文件)。颜色很丑——只是用它们来测试,因为它们与默认值确实不同。我的图表中只有三个切片,因此硬编码了 3 种颜色。我发现 Core Plot 文档对这一切很有帮助。 这是 fillWithColor 方法文档的链接。注意:您现在需要使用 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;
 }

抱歉,如果我弄乱了答案条目——这是我在 StackOverflow 上的第一篇文章

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

野侃 2024-09-23 22:29:57

斯威夫特版本:

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文