【问题标题】:iPhone : How to change gradient color for negative values in Core Plot?iPhone:如何在 Core Plot 中更改负值的渐变颜色?
【发布时间】:2011-02-25 10:11:09
【问题描述】:

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

我想要渐变色如下:

  • 将正值设为绿色

  • 将负值设为红色

我该怎么做?

【问题讨论】:

  • 您可能需要标准化您的值。据我所知,CPGradient 只接受CGFloat 0 - 1。因此,如果您的值范围从 -1024 到 +1024,则需要添加偏移量并除以总范围。这会给你一个0-1的数字。我无法确定答案,因为我从未使用过 CorePlot。
  • @Stephen Furlani:感谢您的意见。我不清楚你所说的。能详细解释一下吗
  • 您知道,无论是提前还是通过查看您的数据集,所有值 n 都是这样的 j ≤ n ≤ k。然后,您可以得到梯度值(n - j) / (k - j),它将在[0-1] 范围内。这在jk 之间进行线性插值。

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


【解决方案1】:

只要实现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;
}

希望能帮到你:)

【讨论】:

  • 我不确定为什么这个答案被标记为已接受,因为问题是关于散点图的。当我在使用散点图时实现-barFillForBarPlot:recordIndex: 时,永远不会调用该方法。这是如何解决问题的?
【解决方案2】:

好吧,我不知道它是否太漂亮了,但我想你可以用相同的数据源制作两个单独的图,比如说 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 文档,也没有工作配置来测试它,所以语法或其他东西可能会关闭,但是我认为一般概念应该有效。

干杯

【讨论】:

    猜你喜欢
    • 2017-10-07
    • 2021-03-27
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2012-07-10
    相关资源
    最近更新 更多