【发布时间】:2010-09-02 16:55:38
【问题描述】:
我在我的一个 iPhone 项目中使用核心情节。是否可以更改饼图中所选切片的颜色(使用 CPPieChartDataSource、CPPieChartDelegate)?
【问题讨论】:
我在我的一个 iPhone 项目中使用核心情节。是否可以更改饼图中所选切片的颜色(使用 CPPieChartDataSource、CPPieChartDelegate)?
【问题讨论】:
在您的饼图数据源中实现以下方法:
-(CPTFill *)sliceFillForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)index;
CPFill 可以是颜色、图像或渐变。
【讨论】:
在您的 .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 的颜色。谢谢
【讨论】:
我将此添加到我的 .m 文件(饼图的数据源文件)中。颜色很难看——只是用它们来测试,因为它们与默认值确实不同。我的图表中只有三个切片,因此是硬编码的 3 种颜色。我发现 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;
}
对不起,如果我弄乱了答案条目 - 这是我在 StackOverflow 上的第一篇帖子
【讨论】:
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());
}
}
【讨论】: