【问题标题】:Core-Plot PieChart Slice colorCore-Plot PieChart 切片颜色
【发布时间】:2010-09-02 16:55:38
【问题描述】:

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

【问题讨论】:

    标签: iphone core-plot


    【解决方案1】:

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

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

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

    【讨论】:

    • 我想在选中特定切片时更改它的颜色。这可能吗?
    【解决方案2】:

    在您的 .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 的颜色。谢谢

    【讨论】:

      【解决方案3】:

      我将此添加到我的 .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 上的第一篇帖子

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2012-12-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多