iPhone:如何在Core Plot中更改负值的渐变颜色?

时间:2011-02-25 10:11:09

标签: iphone objective-c cocoa-touch ios4 core-plot

如何在iPhone的Core-Plot中的渐变散点图中更改负值的区域渐变颜色?

我想要渐变颜色如下:

  • 正值为绿色

  • 负值为红色

我该怎么做?

2 个答案:

答案 0 :(得分:3)

只需实现CPBarPlotDataSource的以下方法即可:

- (CPFill *)barFillForBarPlot:(CPBarPlot *)barPlot recordIndex:(NSUInteger)index
{
    if (barPlot.identifier == @"profit") {
        id item = [self.profits objectAtIndex:index];
        double profit = [[item objectForKey:@"profit"] doubleValue];

        if (profit < 0.0) {
            return [CPFill fillWithGradient:[CPGradient gradientWithBeginningColor:[CPColor redColor] endingColor:[CPColor blackColor]]];
        }
    }

    return nil;
}

希望能提供帮助:)

答案 1 :(得分:2)

好吧,我不知道它是否太漂亮了,但我想你可以用相同的dataSource做两个单独的图,让我们说positivePlot和negativePlot。

CPScatterPlot *positivePlot = [[[CPScatterPlot alloc] init] autorelease];
positivePlot.identifier = @"PositivePlot";
positivePlot.dataSource = self;
[graph addPlot:positivePlot];

CPScatterPlot *negativevePlot = [[[CPScatterPlot alloc] init] autorelease];
negativevePlot.identifier = @"NegativePlot";
negativePlot.dataSource = self;
[graph addPlot:negativePlot];

现在,如果正确配置plotSpaces,您可以分离正值和负值并正确配置每个图,包括区域渐变:

CPXYPlotSpace *positivePlotSpace = [graph newPlotSpace];
positivePlotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
                                             length:CPDecimalFromFloat(100)];
positivePlotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
                                             length:CPDecimalFromFloat(100)];

CPXYPlotSpace *negativevePlotSpace = [graph newPlotSpace];
negativePlotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-100)
                                             length:CPDecimalFromFloat(100)];
negativePlotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
                                             length:CPDecimalFromFloat(100)];


// Plots configuration


//POSITIVE VALUES
positivePlot.plotSpace = positivePlotSpace;

// Green for positive
CPColor *areaColor = [CPColor colorWithComponentRed:0.0 
                                            green:1.0
                                             blue:0.0
                                            alpha:1.0];
CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor 
                                                    endingColor:[CPColor clearColor]];
areaGradient.angle = -90.0f;
CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient];
positivePlot.areaFill = areaGradientFill;


//NEGATIVE VALUES
negativePlot.plotSpace = negativePlotSpace;

// Red for negative
areaColor = [CPColor colorWithComponentRed:1.0 
                                     green:0.0
                                      blue:0.0
                                     alpha:1.0];
areaGradient = [CPGradient gradientWithBeginningColor:areaColor 
                                          endingColor:[CPColor clearColor]];
areaGradient.angle = -90.0f;
areaGradientFill = [CPFill fillWithGradient:areaGradient];
negativePlot.areaFill = areaGradientFill;

注意:这是我的头脑,因为我没有这里的Core-Plot文档,或者是一个工作配置来测试它,所以语法或某些东西可能会关闭,但是我认为一般概念应该有效。

干杯